Join GitHub today
GitHub is home to over 40 million developers working together to host and review code, manage projects, and build software together.Sign up
cmd/compile: method expressions in json.scanner are very slow #26273
Note how the calls end up being of the form
I was trying to clean all of this up by replacing the field with
However, this produced a massive performance degradation, presumably because of the tons of extra allocations:
I don't know if this method expression trickery confuses the escape analysis, or what exactly it is. I have tried to reproduce this with smaller, self-contained benchmarks without
I'll upload a sample CL with the changes, so that others can reproduce the numbers too.
My guess is that every time a method expression is stored in a function pointer a new lambda w/ capture is allocated. i.e:
is translated to:
which would explain the huge increase in allocations
Yes, you are probably right. Though I couldn't replicate it with isolated benchmarks (global func variables vs method expression variables), so I wasn't sure if it was the cause.
Still, ignoring the details of the compiler, it would be a bit of a shame if method expressions were more expensive by design. I'd hope that either the slowness or allocations could be minimised.
I doubt there's a way to avoid allocations when doing what is essentially a variable capture. A method pointer is necessarily "fatter" than a straight-up function pointer has it includes an extra pointer to the instance, otherwise you're back to the old code where you have to manually provide the instance pointer at every call site.
One possible cleanup would be to make
which should presumably be inlined, avoiding any slowdown, and allow all call sites to be cleaner. It's not clear to me that it's worthwhile though...