aboutsummaryrefslogtreecommitdiffstats
path: root/erts/emulator/test/code_SUITE_data/literals.erl
diff options
context:
space:
mode:
authorMichał Muskała <[email protected]>2018-05-09 19:41:24 +0200
committerMichał Muskała <[email protected]>2018-07-17 13:58:27 +0200
commitf31db22102bde44c6ee17bc756bdeb109855acea (patch)
tree3342059d448e5d6af69b96b2bd2d0eaf84ffc755 /erts/emulator/test/code_SUITE_data/literals.erl
parent9ae2044073e6433030ce30756658b103ce67c3c1 (diff)
downloadotp-f31db22102bde44c6ee17bc756bdeb109855acea.tar.gz
otp-f31db22102bde44c6ee17bc756bdeb109855acea.tar.bz2
otp-f31db22102bde44c6ee17bc756bdeb109855acea.zip
Optimise creation of anonymous functions
This introduces a similar optimisation for normal funs to what was introduced for external funs in #1725. It is possible to allocate the fun as a literal, if it does not capture the environment (i.e. it does not close over any variables). Unfortunately it's not possible to do this in the compiler due to problems with representation of such functions in the `.beam` files. Fortunately, we can do this in the loader. Simple evaluation shows that functions that don't capture the enviornment consistute over 60% of all funs in the source code of Erlang/OTP itself. The only downside is that we lose a meningful value in the `pid` field of the fun. The goal of this field, beyond debugging, was to be able to identify the original node of a function. To be able to still do this, the functions that are created in the loader are assigned the init pid as the creator. To solve issues with staryp, initially set the `erts_init_process_id` to `ERTS_INVALID_PID` and skip the described optimisation if the value is still uninitialised.
Diffstat (limited to 'erts/emulator/test/code_SUITE_data/literals.erl')
-rw-r--r--erts/emulator/test/code_SUITE_data/literals.erl8
1 files changed, 7 insertions, 1 deletions
diff --git a/erts/emulator/test/code_SUITE_data/literals.erl b/erts/emulator/test/code_SUITE_data/literals.erl
index 7c3b0ebe73..13c8b412b0 100644
--- a/erts/emulator/test/code_SUITE_data/literals.erl
+++ b/erts/emulator/test/code_SUITE_data/literals.erl
@@ -19,7 +19,8 @@
%%
-module(literals).
--export([a/0,b/0,huge_bignum/0,binary/0,unused_binaries/0,bits/0]).
+-export([a/0,b/0,huge_bignum/0,funs/0,
+ binary/0,unused_binaries/0,bits/0]).
-export([msg1/0,msg2/0,msg3/0,msg4/0,msg5/0]).
a() ->
@@ -108,3 +109,8 @@ msg2() -> {"hello","world"}.
msg3() -> <<"halloj">>.
msg4() -> #{ 1=> "hello", b => "world"}.
msg5() -> {1,2,3,4,5,6}.
+
+funs() ->
+ %% Literal funs (in a non-literal list).
+ [fun ?MODULE:a/0,
+ fun() -> ok end]. %No environment.