Closed RReverser closed 3 weeks ago
How did you even find this?
It started from digging into why iterators specifically are so slow, making some optimisations, being weirded out by not noticing any improvement, and eventually ended with a "scientific guess" that if it all takes roughly constant amount of time, GC must be involved somehow. And voila, indeed it was.
And to be clear, is it removing the finalizer, or adding the
sealed
which has this effect?
It's removing the finalizer.
sealed
was added after the fact, because after removing the finalizer I also removed GC.SuppressFinalize(this);
- which is no longer necessary - and then linter would warn about subclasses potentially adding their own finalizers. So I made the class sealed
just to tell the linter it's okay and there will be no subclasses.
Description of Changes
Clickbait title, but there is no other way to say this. It must be the weirdest way I optimised anything yet.
I was suspicious that a lot of benchmarks, in particular those that deal with iteration - which we have for C# as of recently - take about the same amount of time. After digging in for a bit, I narrowed it down to merely presence of this destructor. Looks like the way destructors are implemented for the Wasm target somehow deoptimises the execution to the point where simply removing it speeds up benchmarks by 15x or more.
We don't really need it anyway, it was more of a nice-to-have in case user does something weird, since regular iterator usage like
foreach
always callsDispose()
correctly.Further investigation and an upstream issue to .NET coming up, but meanwhile here are benchmark differences before/after:
API and ABI breaking changes
If this is an API or ABI breaking change, please apply the corresponding GitHub label.
Expected complexity level and risk
How complicated do you think these changes are? Grade on a scale from 1 to 5, where 1 is a trivial change, and 5 is a deep-reaching and complex change.
This complexity rating applies not only to the complexity apparent in the diff, but also to its interactions with existing and future code.
If you answered more than a 2, explain what is complex about the PR, and what other components it interacts with in potentially concerning ways.
Testing
Describe any testing you've done, and any testing you'd like your reviewers to do, so that you're confident that all the changes work as expected!