Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 9 additions & 8 deletions src/Microsoft.TestPlatform.Filter.Source/FastFilter.cs
Original file line number Diff line number Diff line change
Expand Up @@ -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();

Copy link
Copy Markdown
Member Author

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:

return FilterProperties.Keys.All(name => properties.Contains(name))
    ? null
    : FilterProperties.Keys.Where(name => !properties.Contains(name)).ToArray();

had no allocation in the success path (all properties valid): .All() iterates and returns true without calling .ToArray().

The new code:

string[] invalid = FilterProperties.Keys.Where(name => !properties.Contains(name)).ToArray();
return invalid.Length == 0 ? null : invalid;

always calls .ToArray(), allocating an empty string[0] in the success path before immediately discarding it.

Since ValidForProperties is 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.

return invalid.Length == 0 ? null : invalid;
Comment on lines +71 to +72

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: [Performance] On the happy path (all properties valid) the old All() short-circuited and returned null without any allocation. The new Where().ToArray() always runs to completion — on .NET 6+ this returns Array.Empty<string>() (no heap cost), but on net462 Buffer<T>.ToArray() emits new string[0].

ValidForProperties is cold (once per test source), so the practical impact is zero. But since the stated goal of the PR is eliminating redundant work, worth being aware that this path trades a short-circuit win for avoiding the double-pass in the unhappy case.

}

internal bool Evaluate(Func<string, object?> propertyValueProvider)
Expand All @@ -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)
{
Expand All @@ -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;

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

[Performance] Any(lambda) still allocates a Closure object per outer-loop iteration that reaches this branch. The closure previously captured FilterProperties + name (two fields); it now captures only filterValues (one field)—smaller, but still a heap allocation on every multi-value test evaluation.

With filterValues already a local, a foreach eliminates the closure entirely:

Suggested change
matched = values?.Any(result => result != null && filterValues.Contains(result)) == true;
matched = false;
if (values != null)
{
foreach (var result in values)
{
if (result != null && filterValues.Contains(result))
{
matched = true;
break;
}
}
}

Also removes the == true nullable-bool coercion, which is defensive but unreachable here — values is always non-null in this branch (multiValues was just proven { Length: > 0 }, and Select on a non-null source returns a non-null iterator).

}
else if (hasNoneFilter)
{
Expand Down
Loading