Skip to content

Commit

Permalink
Merge pull request #4073 from esl/hooks/apply_known_arity
Browse files Browse the repository at this point in the history
erlang:apply is faster if number of params is known at compile time
  • Loading branch information
arcusfelis authored Aug 7, 2023
2 parents b27f3bd + c47348f commit 4433414
Show file tree
Hide file tree
Showing 3 changed files with 21 additions and 12 deletions.
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]).
?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

0 comments on commit 4433414

Please sign in to comment.