-
Notifications
You must be signed in to change notification settings - Fork 5.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Use interface rather than closure in *ctx
to remove memory allocation
#49277
Comments
except HandleError in your PR, which other places or allocation should we use this way instead? can you also list them so others can contribute? btw, in this #49280, can you show a detailed profile or others to show that the unnecessary allocation is avoided or the gc has less work or the performance increased in some benchmarks? |
Converting a struct method to raw function type, and returning it out will always make an avoidable heap allocation. It can be easily optimized by using an interface. However, I'm not sure whether this optimization is valuable for most scenarios. For But for other scenarios, it's hard to say. To avoid similar issues in the future, we can analyze the cases in which a struct method is assigned to a
Nice suggestion. I attach the profile flamegraph of |
Enhancement
Using function pointer (actually a closure of struct method) will generate unexpected memory allocation, and harm the speed. Here is a little example to show the mechanism:
You can explore these codes on https://godbolt.org/z/r19b1fz1c. You can find that the
func (n *NoAlloc) Slow()
will callruntime.newobject
. Now.ErrCtx()
uses the similar pattern, and will make theHandleError
very slow. We can solve it by passing the interface.The text was updated successfully, but these errors were encountered: