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

Conversation

NelsonVides
Copy link
Collaborator

@NelsonVides NelsonVides commented Aug 1, 2023

According to the erlang's efficiency guide, at https://www.erlang.org/doc/efficiency_guide/functions#function-calls

This is a rough hierarchy of the performance of the different types of function calls:

  • Calls to local or external functions (foo(), m:foo()) are the fastest calls.
  • Calling or applying a fun (Fun(), apply(Fun, [])) is just a little slower than external calls.
  • Applying an exported function (Mod:Name(), apply(Mod, Name, [])) where the number of arguments is known at compile time is next.
  • Applying an exported function (apply(Mod, Name, Args)) where the number of arguments is not known at compile time is the least efficient.

So giving information to the hook engine about the actual number of parameters, makes the call fall into the second category instead of the fourth. In local micro-benchmarking using benchee, it was around 2% faster.

@mongoose-im

This comment was marked as outdated.

@codecov
Copy link

codecov bot commented Aug 1, 2023

Codecov Report

Patch coverage: 100.00% and no project coverage change.

Comparison is base (6cef853) 83.88% compared to head (c47348f) 83.88%.
Report is 11 commits behind head on master.

Additional details and impacted files
@@           Coverage Diff           @@
##           master    #4073   +/-   ##
=======================================
  Coverage   83.88%   83.88%           
=======================================
  Files         526      526           
  Lines       33181    33181           
=======================================
  Hits        27834    27834           
  Misses       5347     5347           
Files Changed Coverage Δ
src/gen_hook.erl 86.90% <100.00%> (ø)
src/safely.erl 100.00% <100.00%> (ø)

... and 9 files with indirect coverage changes

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -227,7 +227,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.

@@ -31,6 +31,13 @@

-define(MONGOOSE_URI, <<"https://www.erlang-solutions.com/products/mongooseim.html">>).

-define(APPLY_SAFELY(F),
Copy link
Contributor

@arcusfelis arcusfelis Aug 6, 2023

Choose a reason for hiding this comment

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

maybe not mongoose.hrl, could be apply_sefely.hrl...

Also, it is not APPLY_SAFELY(F) anymore, it is catch_v2 :D So, maybe some better name.

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 could happily create such header and simply use the header from both source and test and kill the file.

When it comes to naming, oh naming is hard and it has always been called safely so, dunno :D

Copy link
Contributor

@arcusfelis arcusfelis Aug 6, 2023

Choose a reason for hiding this comment

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

because it was doing erlang:apply(F). Now it does F.

But yea, I would be always think it should be F(), when I would read it. And the arg should be a fun(). So, maybe renaming F is good idea.

Maybe better, idk xdd:
-define(SAFELY(Code), try Code .... end).

or
oh god MATCH_EXCEPTIONS (maybe no) xdd

And put it in safely.hrl so everything is named safely.

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Done 😃

Copy link
Contributor

@arcusfelis arcusfelis left a comment

Choose a reason for hiding this comment

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

looks fine

@mongoose-im
Copy link
Collaborator

mongoose-im commented Aug 6, 2023

elasticsearch_and_cassandra_25 / elasticsearch_and_cassandra_mnesia / c47348f
Reports root/ big
OK: 369 / Failed: 0 / User-skipped: 38 / Auto-skipped: 0


small_tests_24 / small_tests / c47348f
Reports root / small


small_tests_25 / small_tests / c47348f
Reports root / small


small_tests_25_arm64 / small_tests / c47348f
Reports root / small


ldap_mnesia_24 / ldap_mnesia / c47348f
Reports root/ big
OK: 2258 / Failed: 0 / User-skipped: 827 / Auto-skipped: 0


dynamic_domains_pgsql_mnesia_24 / pgsql_mnesia / c47348f
Reports root/ big
OK: 4221 / Failed: 0 / User-skipped: 82 / Auto-skipped: 0


ldap_mnesia_25 / ldap_mnesia / c47348f
Reports root/ big
OK: 2258 / Failed: 0 / User-skipped: 827 / Auto-skipped: 0


dynamic_domains_pgsql_mnesia_25 / pgsql_mnesia / c47348f
Reports root/ big
OK: 4221 / Failed: 0 / User-skipped: 82 / Auto-skipped: 0


dynamic_domains_mysql_redis_25 / mysql_redis / c47348f
Reports root/ big
OK: 4195 / Failed: 0 / User-skipped: 108 / Auto-skipped: 0


pgsql_mnesia_24 / pgsql_mnesia / c47348f
Reports root/ big
OK: 4604 / Failed: 0 / User-skipped: 89 / Auto-skipped: 0


dynamic_domains_mssql_mnesia_25 / odbc_mssql_mnesia / c47348f
Reports root/ big
OK: 4218 / Failed: 0 / User-skipped: 85 / Auto-skipped: 0

muc_light_http_api_SUITE:end_per_suite
{error,
 {{unregistering_failed,
   {amount,1},
   {unregistered_items,
  [{{<<"_send_message_to_room_2576">>,
     [{escalus_event_mgr,<0.3074.2>},
    {tc_name,send_message_to_room},
    {escalus_cleaner,<0.3073.2>},
    {watchdog,<0.3072.2>},
    {preset,"odbc_mssql_mnesia"},
    {mim_data_dir,
     "/home/circleci/project/big_tests/tests/muc_light_http_api_SUITE_data"},
    {tc_logfile,
     "https://circleci-mim-results.s3.eu-central-1.amazonaws.com/PR/4073/184573/odbc_mssql_mnesia.25.3-amd64/big/ct_run.test%408f02a02f5641.2023-08-06_14.22.57/big_tests.tests.muc_light_http_api_SUITE.logs/run.2023-08-06_14.35.47/muc_light_http_api_suite.send_message_to_room.146947.html"},
    {tc_group_properties,[{name,positive},parallel]},
    {tc_group_path,[]},
    {data_dir,
     "/home/circleci/project/big_tests/_build/default/lib/mongoose_tests/ebin/muc_light_http_api_SUITE_data/"},
    {priv_dir,
     "https://circleci-mim-results.s3.eu-central-1.amazonaws.com/PR/4073/184573/odbc_mssql_mnesia.25.3-amd64/big/ct_run.test%408f02a02f5641.2023-08-06_14.22.57/big_tests.tests.muc_light_http_api_SUITE.logs/run.2023-08-06_14.35.47/log_private/"},
    {{saved_modules,mongooseim@localhost,<<"test type">>},
     #{mod_adhoc => #{iqdisc => one_queue,report_commands_node => false},
       mod_amp => #{},
       mod_bosh =>
      #{backend => mnesia,inactivity => 30,max_pause => 120,
        max_wait => infinity,server_acks => false},
       mod_cache_users =>
      #{number_of_segments => 5,strategy => fifo,time_to_live => 2},
       mod_carboncopy => #{iqdisc => no_queue},
       mod_disco =>
      #{extra_domains => [],iq...

Report log


internal_mnesia_25 / internal_mnesia / c47348f
Reports root/ big
OK: 2403 / Failed: 1 / User-skipped: 681 / Auto-skipped: 0

mod_global_distrib_SUITE:hosts_refresher:test_host_refreshing
{error,
  {{trees_for_connections_present,true,[{times,100,false}],ok},
   [{mongoose_helper,do_wait_until,2,
      [{file,"/home/circleci/project/big_tests/tests/mongoose_helper.erl"},
       {line,357}]},
    {mod_global_distrib_SUITE,test_host_refreshing,1,
      [{file,
         "/home/circleci/project/big_tests/tests/mod_global_distrib_SUITE.erl"},
       {line,384}]},
    {test_server,ts_tc,3,[{file,"test_server.erl"},{line,1782}]},
    {test_server,run_test_case_eval1,6,
      [{file,"test_server.erl"},{line,1291}]},
    {test_server,run_test_case_eval,9,
      [{file,"test_server.erl"},{line,1223}]}]}}

Report log


pgsql_mnesia_25 / pgsql_mnesia / c47348f
Reports root/ big
OK: 4604 / Failed: 0 / User-skipped: 89 / Auto-skipped: 0


mssql_mnesia_25 / odbc_mssql_mnesia / c47348f
Reports root/ big
OK: 4601 / Failed: 0 / User-skipped: 92 / Auto-skipped: 0


mysql_redis_25 / mysql_redis / c47348f
Reports root/ big
OK: 4590 / Failed: 0 / User-skipped: 103 / Auto-skipped: 0


internal_mnesia_25 / internal_mnesia / c47348f
Reports root/ big
OK: 2404 / Failed: 0 / User-skipped: 681 / Auto-skipped: 0

@arcusfelis arcusfelis marked this pull request as ready for review August 7, 2023 07:29
@arcusfelis arcusfelis merged commit 4433414 into master Aug 7, 2023
4 checks passed
@arcusfelis arcusfelis deleted the hooks/apply_known_arity branch August 7, 2023 07:29
@chrzaszcz
Copy link
Member

chrzaszcz commented Aug 7, 2023

@NelsonVides please add missing PR descrition - it's not clear what the motivation for this change is.

@NelsonVides
Copy link
Collaborator Author

@NelsonVides please add missing PR descrition

I know, Mikhail merged slightly faster than I thought. Writing a description now 🙏🏽

@NelsonVides
Copy link
Collaborator Author

@NelsonVides please add missing PR descrition - it's not clear what the motivation for this change is.

Now, description added 🙂

@chrzaszcz chrzaszcz added this to the 6.2.0 milestone Dec 11, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants