otp
otp copied to clipboard
stdlib: optimize gen_server by caching callback functions
Cache callback functions handle_call, handle_cast and handle_info in a tuple when initializing the gen_server. This "should" give us a minor speed up by skipping lookups in export entry.
CT Test Results
2 files 86 suites 33m 28s :stopwatch: 1 783 tests 1 735 :heavy_check_mark: 48 :zzz: 0 :x: 2 065 runs 2 015 :heavy_check_mark: 50 :zzz: 0 :x:
Results for commit f0874ffb.
:recycle: This comment has been updated with latest results.
To speed up review, make sure that you have read Contributing to Erlang/OTP and that all checks pass.
See the TESTING and DEVELOPMENT HowTo guides for details about how to run test locally.
Artifacts
// Erlang/OTP Github Action Bot
Does it keep compatibility with existing hot code loading semantic?
Previously, executing code:load
for module implementing gen_server
behaviour resulted in an immediate change.
Does it keep compatibility with existing hot code loading semantic? Previously, executing
code:load
for module implementinggen_server
behaviour resulted in an immediate change.
Yes, the funs refer to the MFAs and not the currently loaded code.
So Mod:handle_cast(Request, State)
is, after HandleCast = fun Mod:handle_cast/2
slower than HandleCast(Request, State)
??
Is it the fun()
creation time that it is worth the work to avoid?
Because the call will go through the export entry anyway...
So
Mod:handle_cast(Request, State)
is, afterHandleCast = fun Mod:handle_cast/2
slower thanHandleCast(Request, State)
??
Yes.
Is it the
fun()
creation time that it is worth the work to avoid? Because the call will go through the export entry anyway...
We want to avoid having to look up the export entry from MFA on every call. Remote funs do that once on creation, but apply/3
(which Mod:handle_cast/2
is syntactic sugar for) has to do that every time.
Here's a small benchmark demonstrating the difference.
Here's a small benchmark demonstrating the difference.
Should this be mentioned in the efficiency guide?
Sure, feel free to make a PR. :)