otp icon indicating copy to clipboard operation
otp copied to clipboard

stdlib: optimize gen_server by caching callback functions

Open frazze-jobb opened this issue 2 years ago • 8 comments

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.

frazze-jobb avatar Mar 28 '22 12:03 frazze-jobb

CLA assistant check
All committers have signed the CLA.

CLAassistant avatar Mar 28 '22 12:03 CLAassistant

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

github-actions[bot] avatar Mar 28 '22 12:03 github-actions[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.

max-au avatar Mar 29 '22 03:03 max-au

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.

Yes, the funs refer to the MFAs and not the currently loaded code.

jhogberg avatar Mar 29 '22 09:03 jhogberg

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...

RaimoNiskanen avatar Mar 31 '22 16:03 RaimoNiskanen

So Mod:handle_cast(Request, State) is, after HandleCast = fun Mod:handle_cast/2 slower than HandleCast(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.

jhogberg avatar Apr 01 '22 11:04 jhogberg

Here's a small benchmark demonstrating the difference.

Should this be mentioned in the efficiency guide?

essen avatar May 05 '22 09:05 essen

Sure, feel free to make a PR. :)

jhogberg avatar May 05 '22 12:05 jhogberg