Skip to content
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

stdlib: optimise gen_statem by caching callback function #7419

Conversation

NelsonVides
Copy link
Contributor

@NelsonVides NelsonVides commented Jun 19, 2023

Inspired by #5831 (f0874ff), we want to cache Mod:handle_event/4 when such is the configured callback mode, to make calling this function faster for state machines.


I'm not sure of the names of the newly defined types though, better names are welcomed. The idea is to cache the callback together with the statem params where we store the callback mode.

Inspired by f0874ff, we want to cache
`Mod:handle_event/4` when such is the configured callback mode, to make calling
this function faster for state machines.
@github-actions
Copy link
Contributor

github-actions bot commented Jun 19, 2023

CT Test Results

       2 files       89 suites   40m 20s ⏱️
1 901 tests 1 852 ✔️ 48 💤 1
2 196 runs  2 145 ✔️ 50 💤 1

For more details on these failures, see this check.

Results for commit 07eff3b.

♻️ 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

@@ -1375,8 +1382,8 @@ loop_state_callback(
case CallbackMode of
state_functions ->
Module:State(Type, Content, Data);
handle_event_function ->
Module:handle_event(Type, Content, State, Data)
{handle_event_function, HandleEvent} ->
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

As there's only two options and any errors to that effect should've been caught before we got here, can't we skip the wrapping tuple for HandleEvent and just call it directly?

@RaimoNiskanen ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

More than happy to do it that way ofc, just let me know if I should – asking because Raimo's been tagged so maybe we want his opinion first (?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

That is a good idea

@IngelaAndin IngelaAndin added the team:PS Assigned to OTP team PS label Jun 20, 2023
Implement not wrapping the fun() for handle_event_function.

Fix the handle_event_fun() type.  The suggestion's type
was that the handle_event_fun() should be either
a state enter fun() or a handle event fun(),
but it should be a fun() that is both.

Revert the callback_mode/1 type checking function and introduce
params_callback_mode/2 to do the conversion into the cached value.
By that most of callback_mode_result/6 could be reverted,
only the finishing #params{} construction needs a change.
@RaimoNiskanen
Copy link
Contributor

I had opinions, and the most explicit way to convey them was to rework your suggestion ;-)
See the commit comment in 07eff3b.

This resulted in a smaller diff.

What do you think?

@RaimoNiskanen RaimoNiskanen added the testing currently being tested, tag is used by OTP internal CI label Jun 21, 2023
@NelsonVides
Copy link
Contributor Author

That looks very neat to me, thanks for the suggestions and the notes :)

@RaimoNiskanen RaimoNiskanen merged commit 941a3dc into erlang:master Jul 3, 2023
@RaimoNiskanen
Copy link
Contributor

Thank you for your pull request!

@NelsonVides NelsonVides deleted the stdlib/optimise_gen_statem_handle_event_function branch July 16, 2023 16:05
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
team:PS Assigned to OTP team PS testing currently being tested, tag is used by OTP internal CI
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants