Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Erarndt
Copy link
Contributor

@Erarndt Erarndt commented Jul 15, 2025

Fixes #

Context

There are some easy allocation wins in BuildGlobResultFromIncludeItem(), and the highlighted lines can either be reduced or removed:

image

Here is the method after changes:

image

Changes Made

Testing

Notes

@Copilot Copilot AI review requested due to automatic review settings July 15, 2025 21:55
Copy link
Contributor

@Copilot Copilot AI left a 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

Comment on lines +2627 to +2645
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;
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2645
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;
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2645
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;
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2648
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()));
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2648
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()));
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2648
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()));
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Comment on lines +2627 to +2648
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()));
Copy link
Preview

Copilot AI Jul 15, 2025

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant