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

erlang:apply is faster if number of params is known at compile time #4073

Merged
merged 2 commits into from
Aug 7, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 11 additions & 0 deletions include/safely.hrl
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
-ifndef(SAFELY).
-define(SAFELY, true).

-define(SAFELY(F),
try F catch
error:R:S -> {exception, #{class => error, reason => R, stacktrace => S}};
throw:R -> {exception, #{class => throw, reason => R}};
exit:R:S -> {exception, #{class => exit, reason => R, stacktrace => S}}
end).

-endif.
3 changes: 2 additions & 1 deletion src/gen_hook.erl
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@

-ignore_xref([start_link/0, add_handlers/1, delete_handlers/1]).

-include("safely.hrl").
-include("mongoose.hrl").

-type hook_name() :: atom().
Expand Down Expand Up @@ -227,7 +228,7 @@ run_hook([Handler | Ls], Acc, Params, Key) ->
hook_fn_ret() | safely:exception().
apply_hook_function(#hook_handler{hook_fn = HookFn, extra = Extra},
Acc, Params) ->
safely:apply(HookFn, [Acc, Params, Extra]).
Copy link
Contributor

Choose a reason for hiding this comment

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

It was the only place we've used safely:apply function btw :)

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

I know, I could try to get rid of this module but there's an entire test-suite testing properties of this. So now this is scoped to test compilation only.

?SAFELY(HookFn(Acc, Params, Extra)).

error_running_hook(Info, Handler, Acc, Params, Key) ->
?LOG_ERROR(Info#{what => hook_failed,
Expand Down
19 changes: 8 additions & 11 deletions src/safely.erl
Original file line number Diff line number Diff line change
Expand Up @@ -7,12 +7,14 @@
%% If it is an error, we want the stacktrace, if not, we don't.
%% For more information, see usage in test/safely_SUITE.erl

-include("safely.hrl").
-include_lib("kernel/include/logger.hrl").

-ifdef(TEST).
-export([apply/2, apply/3]).
-endif.
-export([apply_and_log/3, apply_and_log/4]).
-compile({no_auto_import, [apply/2, apply/3]}).
-ignore_xref([apply/3, apply_and_log/4]).
-ignore_xref([apply_and_log/4]).

-type error_class() :: error | exit | throw.
-type error_info() :: #{class => error_class(), reason => term(), stacktrace => [term()]}.
Expand All @@ -35,20 +37,15 @@
{exception, Info}
end).

-define(MATCH_EXCEPTIONS(F),
try F catch
error:R:S -> {exception, #{class => error, reason => R, stacktrace => S}};
throw:R -> {exception, #{class => throw, reason => R}};
exit:R:S -> {exception, #{class => exit, reason => R, stacktrace => S}}
end).

-ifdef(TEST).
-spec apply(fun((...) -> A), [term()]) -> A | exception().
apply(Function, Args) when is_function(Function), is_list(Args) ->
?MATCH_EXCEPTIONS(erlang:apply(Function, Args)).
?SAFELY(erlang:apply(Function, Args)).

-spec apply(atom(), atom(), [term()]) -> term() | exception().
apply(Module, Function, Args) when is_atom(Function), is_list(Args) ->
?MATCH_EXCEPTIONS(erlang:apply(Module, Function, Args)).
?SAFELY(erlang:apply(Module, Function, Args)).
-endif.

-spec apply_and_log(fun((...) -> A), [term()], map()) -> A | exception().
apply_and_log(Function, Args, Context)
Expand Down