perf: manually iterate OfType - #4513
Conversation
43d1b95 to
ed890afCompareIs the outlined allocation issue because of // Implementation ApublicstaticIEnumerable<TUp>OfTypeSlim<TUp>(thisIEnumerablesource){returnsource.Cast<object>().Where(static s =>sisTUp).Cast<TUp>();}// Implementation BpublicstaticIEnumerable<TUp>OfTypeSlim<TUp>(thisIEnumerablesource){foreach(varvalueinsource){if(valueisTUpup)yieldreturnup;}}I highly doubt the performance will be still very bad after these changes |
thomhurst
commented
Jan 19, 2026
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. |
TimothyMakkison
commented
Jan 19, 2026
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. |
Rekkonnect
commented
Jan 19, 2026
It seems that the biggest offender is the definite allocation even in cases where the attribute isn't present, i.e. |
thomhurst
commented
Jan 19, 2026
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! |
TimothyMakkison
commented
Jan 19, 2026
SkipAttribute manuallyOfTypeed890af to
943da0eCompareUh oh!
There was an error while loading. Please reload this page.


Manually iterate collection to avoid allocating
OfTypeBefore
After