perf: manually iterate OfType#4513
Conversation
43d1b95 to
ed890af
Compare
|
Is the outlined allocation issue because of // Implementation A
public static IEnumerable<TUp> OfTypeSlim<TUp>(this IEnumerable source)
{
return source
.Cast<object>()
.Where(static s => s is TUp)
.Cast<TUp>();
}
// Implementation B
public static IEnumerable<TUp> OfTypeSlim<TUp>(this IEnumerable source)
{
foreach (var value in source)
{
if (value is TUp up)
yield return up;
}
}I highly doubt the performance will be still very bad after these changes |
|
I think there's quite a lot of places where we want to check for certain attributes, and so iterate through them all to find one. I was thinking of a new custom class for well-known types (I*EventReceiver, IClassConstructor, ITestExecutor, etc.). When tests are first constructed/discovered, do a one-time iteration over the attributes. For each attribute, check if it is a certain type and store it as a property in our custom class. Should make lots of enumerations and lookups redundant then. |
I suspect this would be pretty much the same or worse as the compiler will still have to create a state machine iterator. I modified your code slightly so it will compile, but this is what it looks like. The beauty of |
Might be worth looking at, I do wonder how beneficial this will be as most methods only have 1 or 2 attribtues on them and iterating them is pretty fast, garbage non with standing. |
|
It seems that the biggest offender is the definite allocation even in cases where the attribute isn't present, i.e. |
Well if we're iterating anyway, this just moves it to one time in theory, and eliminates all of these further .OfType<> checks/eliminations that we're doing now I guess! |
SkipAttribute manuallyOfType
ed890af to
943da0e
Compare


Manually iterate collection to avoid allocating
OfTypeBefore
After