-
Notifications
You must be signed in to change notification settings - Fork 1.4k
Streamline BuildGlobResultFromIncludeItem #12178
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
base: main
Are you sure you want to change the base?
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.
Pull Request Overview
This PR optimizes the BuildGlobResultFromIncludeItem
method to reduce memory allocations by replacing LINQ operations with more efficient manual iteration and array initialization patterns.
- Replaces LINQ
Where().ToArray()
with manual foreach loop and lazy list initialization - Replaces LINQ
Select().ToImmutableArray()
with direct array allocation and population - Maintains the same filtering logic while improving performance characteristics
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; | ||
} | ||
|
||
var includeGlob = CompositeGlob.Create(includeGobFragmentsList.Select(f => f.ToMSBuildGlob())); |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; | ||
} | ||
|
||
var includeGlob = CompositeGlob.Create(includeGobFragmentsList.Select(f => f.ToMSBuildGlob())); |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; | ||
} | ||
|
||
var includeGlob = CompositeGlob.Create(includeGobFragmentsList.Select(f => f.ToMSBuildGlob())); |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
List<ItemSpecFragment> includeGobFragmentsList = null; | ||
foreach (ItemSpecFragment fragment in includeItemspec.Fragments) | ||
{ | ||
if (fragment is GlobFragment && fragment.TextFragment.AsSpan().IndexOfAny(s_invalidGlobChars) < 0) | ||
{ | ||
includeGobFragmentsList ??= new List<ItemSpecFragment>(includeItemspec.Fragments.Count); | ||
includeGobFragmentsList.Add(fragment); | ||
} | ||
} | ||
|
||
if (includeGobFragmentsList == null || includeGobFragmentsList.Count == 0) | ||
{ | ||
return null; | ||
} | ||
|
||
ImmutableArray<string> includeGlobStrings = includeGlobFragments.Select(f => f.TextFragment).ToImmutableArray(); | ||
var includeGlob = CompositeGlob.Create(includeGlobFragments.Select(f => f.ToMSBuildGlob())); | ||
string[] includeGlobStrings = new string[includeGobFragmentsList.Count]; | ||
for (int i = 0; i < includeGlobStrings.Length; ++i) | ||
{ | ||
includeGlobStrings[i] = includeGobFragmentsList[i].TextFragment; | ||
} | ||
|
||
var includeGlob = CompositeGlob.Create(includeGobFragmentsList.Select(f => f.ToMSBuildGlob())); |
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.
Variable name contains a typo: 'includeGobFragmentsList' should be 'includeGlobFragmentsList' (missing 'l' in 'Glob')
Copilot uses AI. Check for mistakes.
Fixes #
Context
There are some easy allocation wins in
BuildGlobResultFromIncludeItem()
, and the highlighted lines can either be reduced or removed:Here is the method after changes:
Changes Made
Testing
Notes