-
Notifications
You must be signed in to change notification settings - Fork 357
[efficiency-improver] perf: avoid redundant dictionary lookups in FastFilter.Evaluate #16139
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
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
|
|
@@ -68,9 +68,8 @@ internal FastFilter(Dictionary<string, ISet<string>> filterProperties, Operation | |||||||||||||||||||||||||||
| return null; | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| return FilterProperties.Keys.All(name => properties.Contains(name)) | ||||||||||||||||||||||||||||
| ? null | ||||||||||||||||||||||||||||
| : FilterProperties.Keys.Where(name => !properties.Contains(name)).ToArray(); | ||||||||||||||||||||||||||||
| string[] invalid = FilterProperties.Keys.Where(name => !properties.Contains(name)).ToArray(); | ||||||||||||||||||||||||||||
| return invalid.Length == 0 ? null : invalid; | ||||||||||||||||||||||||||||
|
Comment on lines
+71
to
+72
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. nit: [Performance] On the happy path (all properties valid) the old
|
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| internal bool Evaluate(Func<string, object?> propertyValueProvider) | ||||||||||||||||||||||||||||
|
|
@@ -80,13 +79,15 @@ internal bool Evaluate(Func<string, object?> propertyValueProvider) | |||||||||||||||||||||||||||
| #endif | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| bool matched = false; | ||||||||||||||||||||||||||||
| foreach (var name in FilterProperties.Keys) | ||||||||||||||||||||||||||||
| foreach (var kvp in FilterProperties) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| var filterValues = kvp.Value; | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // Reserved keyword: "None" matches tests with no value for this property (uncategorized). | ||||||||||||||||||||||||||||
| bool hasNoneFilter = FilterProperties[name].Contains(Condition.NoneFilterValue); | ||||||||||||||||||||||||||||
| bool hasNoneFilter = filterValues.Contains(Condition.NoneFilterValue); | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
| // If there is no value corresponding to given name, treat it as unmatched unless filtering for "None". | ||||||||||||||||||||||||||||
| if (!TryGetPropertyValue(name, propertyValueProvider, out var singleValue, out var multiValues)) | ||||||||||||||||||||||||||||
| if (!TryGetPropertyValue(kvp.Key, propertyValueProvider, out var singleValue, out var multiValues)) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| if (hasNoneFilter) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
|
|
@@ -100,12 +101,12 @@ internal bool Evaluate(Func<string, object?> propertyValueProvider) | |||||||||||||||||||||||||||
| if (singleValue != null) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| var value = PropertyValueRegex == null ? singleValue : ApplyRegex(singleValue); | ||||||||||||||||||||||||||||
| matched = value != null && FilterProperties[name].Contains(value); | ||||||||||||||||||||||||||||
| matched = value != null && filterValues.Contains(value); | ||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| else if (multiValues is { Length: > 0 }) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
| var values = PropertyValueRegex == null ? multiValues : multiValues?.Select(value => ApplyRegex(value)); | ||||||||||||||||||||||||||||
| matched = values?.Any(result => result != null && FilterProperties[name].Contains(result)) == true; | ||||||||||||||||||||||||||||
| matched = values?.Any(result => result != null && filterValues.Contains(result)) == true; | ||||||||||||||||||||||||||||
|
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. [Performance] With
Suggested change
Also removes the |
||||||||||||||||||||||||||||
| } | ||||||||||||||||||||||||||||
| else if (hasNoneFilter) | ||||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||||
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.
[Performance & Allocations] Minor allocation tradeoff worth noting.
The previous code:
had no allocation in the success path (all properties valid):
.All()iterates and returnstruewithout calling.ToArray().The new code:
always calls
.ToArray(), allocating an emptystring[0]in the success path before immediately discarding it.Since
ValidForPropertiesis called once per test source (not per test case), the practical impact is zero. But the PR description frames this as a pure "single-pass optimization" — it is single-pass, but it trades the double-scan in the rare failure path for an allocation in the common success path. The PR's energy-efficiency claim for this method should be noted with that nuance.Not blocking — just flagging for accuracy against the PR description.