You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
### Fixes
Many allocations due to implicit object creation to capture function
pointer + instance.
### Context
Here's 72MB / 1.19M `Func<IEnumerable<KeyValuePair<string, string>>`
objects being supposedly allocated by `Utilities.CastItemsOnByOne`:

In reality, the allocation is actually happening in the `ItemData`
constructor:
```cs
public readonly struct ItemData
{
private readonly Func<IEnumerable<KeyValuePair<string, string>>> _enumerateMetadata;
public ItemData(string type, object value)
{
Type = type;
Value = value;
if (value is IItemData dt)
{
EvaluatedInclude = dt.EvaluatedInclude;
_enumerateMetadata = dt.EnumerateMetadata;
}
else if (value is ITaskItem ti)
{
EvaluatedInclude = ti.ItemSpec;
_enumerateMetadata = ti.EnumerateMetadata;
}
else
{
EvaluatedInclude = value.ToString() ?? string.Empty;
_enumerateMetadata = () => [];
}
}
}
```
Referencing an instance method implicitly has to create an object since
you need to store both a pointer to the target method that lives with
the class definition + a pointer to the source instance to pass the
method at runtime.
You can see this is exactly what the IL shows as well:

### Changes Made
Since the struct already has a reference to the target in `Value`, we
can directly call the function on-demand when needed without the
allocation.
0 commit comments