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

Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 23 additions & 16 deletions lib/stdlib/src/gen_statem.erl
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,13 @@

-type callback_mode_result() ::
callback_mode() | [callback_mode() | state_enter()].

-type handle_event_enter_fun() :: fun(('enter', OldState :: state(), CurrentState, data()) ->
state_enter_result(CurrentState)).
-type handle_event_event_fun() :: fun((event_type(), event_content(), state(), data()) ->
event_handler_result(state())).
-type handle_event_fun() :: handle_event_enter_fun() | handle_event_event_fun().
-type callback_mode_internal() :: 'state_functions' | {'handle_event_function', handle_event_fun()}.
-type callback_mode() :: 'state_functions' | 'handle_event_function'.
-type state_enter() :: 'state_enter'.

Expand Down Expand Up @@ -415,13 +422,13 @@
%% - return true if the value is of the type, false otherwise
-compile(
{inline,
[callback_mode/1, state_enter/1,
[callback_mode_internal/2, state_enter/1,
event_type/1, from/1, timeout_event_type/1]}).
%%
callback_mode(CallbackMode) ->
callback_mode_internal(CallbackMode, CallbackModule) ->
case CallbackMode of
state_functions -> true;
handle_event_function -> true;
state_functions -> {true, state_functions};
handle_event_function -> {true, {handle_event_function, fun CallbackModule:handle_event/4}};
_ -> false
end.
%%
Expand Down Expand Up @@ -492,7 +499,7 @@ event_type(Type) ->
end).

-record(params,
{callback_mode = state_functions :: callback_mode(),
{callback_mode = state_functions :: callback_mode_internal(),
state_enter = false :: boolean(),
parent :: pid(),
modules = [?MODULE] :: nonempty_list(module()),
Expand Down Expand Up @@ -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

HandleEvent(Type, Content, State, Data)
end
of
Result ->
Expand Down Expand Up @@ -2471,19 +2478,19 @@ callback_mode_result(P, Modules, CallbackModeResult) ->
listify(CallbackModeResult), undefined, false).
%%
callback_mode_result(
P, Modules, CallbackModeResult,
[H|T], CallbackMode, StateEnter) ->
case callback_mode(H) of
true ->
P, [Module | _] = Modules, CallbackModeResult,
[H|T], CurrentCallbackMode, StateEnter) ->
case callback_mode_internal(H, Module) of
{true, InternalCallbackMode} ->
callback_mode_result(
P, Modules, CallbackModeResult,
T, H, StateEnter);
T, InternalCallbackMode, StateEnter);
false ->
case state_enter(H) of
true ->
callback_mode_result(
P, Modules, CallbackModeResult,
T, CallbackMode, true);
T, CurrentCallbackMode, true);
false ->
{error,
{bad_return_from_callback_mode, CallbackModeResult},
Expand All @@ -2492,16 +2499,16 @@ callback_mode_result(
end;
callback_mode_result(
P, Modules, CallbackModeResult,
[], CallbackMode, StateEnter) ->
[], CurrentCallbackMode, StateEnter) ->
if
CallbackMode =:= undefined ->
CurrentCallbackMode =:= undefined ->
{error,
{bad_return_from_callback_mode, CallbackModeResult},
?STACKTRACE()};
true ->
P#params{
modules = Modules,
callback_mode = CallbackMode,
callback_mode = CurrentCallbackMode,
state_enter = StateEnter}
end.

Expand Down