-
Notifications
You must be signed in to change notification settings - Fork 41
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Improve SelectVariant performance #187
Improve SelectVariant performance #187
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for your contribution!
I have been looking into this PR and wondered why tests would pass when the weight was no longer calculated. I tried running dotnet test
locally and found many failing tests.
Long story short tests were not being ran automatically on PR's. I made this PR to fix that #188 . Can you please rebase against the main branch so that the tests are running on this PR?
A three changes were made to improve performance: - totalWeight was not needed, per the docs this will always be 100%. This removes the Sum call which loops over the list of variants. - The FirstOrDefault call was removed, this allows for us to loop over the list of variants only once. - We check if there are any overrides before calling Any.
c755557
to
db99389
Compare
@gardleopard thanks for catching the failing tests. I should have caught that they were not running. I've pushed a fix for most of the tests but there are still 21 failing, all from If I look at the variants from these tests I see that in many cases the weights don't add up to 100 (for example Feature.Variants.B pictured below): From what I understand from reading the documentation on variant weights this is not a valid state for variants to be in. The way to fix the tests then is to update the variant weights in the test data. Do you agree? I don't normally think that changing test data to fix tests is the right call but I believe the old code was catering for a situation that doesn't exist in production and so some of the tests do need to be updated. |
Thank you for the contribution @nathanmascitelli. We'll need to have a look at how this will affect distribution calculation. The client spec tests are common for all SDKs and a way for us to ensure identical behaviour across SDKs and platforms, so we should look at why they fail here and if there are performant improvements that can be made that ensure the SDK conforms to the spec |
OK that makes sense. I'm just going off what I see in the docs, there may obviouslly be some context or history that I'm missing. The big thing with this change was to just reduce the number of times we loop over the list of variants. In the worst case scenario on main we could loop over the whole list three times (Sum, override check, target check). My change is an attempt to get this down to just once. If we have to add back the Sum that would still be an improvement. |
Ok great! I think we might have to keep it for consistency and historical reasons, but I'll have a look at it! |
I think we have to remove the weight part of this PR, then we can merge and ship a new version. There are backwards compatibility/historical and general conformity among all SDKs reasons for keeping things as they are in that regard. |
73b32b2
to
0d0222b
Compare
@daveleek added the weight back. |
Great! Thank you @nathanmascitelli! |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Looks good @nathanmascitelli! Thank you for your contribution!
foreach (var variantDefinition in variantDefinitions) | ||
{ | ||
if (variantDefinition.Weight != 0) | ||
{ | ||
if (variantDefinition.Overrides.Count > 0 && variantDefinition.Overrides.Any(OverrideMatchesContext(context))) | ||
{ | ||
result = variantDefinition.ToVariant(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
why not just return instead of breaking here?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@gardleopard no real reason, I tend to try and keep the number of return statements from a method as low as possible but thats a 'me thing', theres no technical reason for it. I can change it if you'd like.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM
Description
In continuing to look for performance hotspots after the latest release,
VariantUtils.SelectVariant
was identified as an area for improvement.In the PR three changes were made to improve performance:
Type of change
How Has This Been Tested?
Using
BenchmarkerDotNet
the following benchmarks were run:These produced the following results:
Benchmarks with no VariantOverride
Benchmarks with a VariantOverride
In both cases the new code uses less CPU and memory.