From f31db22102bde44c6ee17bc756bdeb109855acea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Micha=C5=82=20Muska=C5=82a?= Date: Wed, 9 May 2018 19:41:24 +0200 Subject: 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. --- lib/stdlib/test/lists_SUITE.erl | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) (limited to 'lib') diff --git a/lib/stdlib/test/lists_SUITE.erl b/lib/stdlib/test/lists_SUITE.erl index 837ab4e97e..af94fc79bc 100644 --- a/lib/stdlib/test/lists_SUITE.erl +++ b/lib/stdlib/test/lists_SUITE.erl @@ -1679,7 +1679,7 @@ make_fun() -> receive {Pid, Fun} -> Fun end. make_fun(Pid) -> - Pid ! {self(), fun make_fun/1}. + Pid ! {self(), fun (X) -> {X, Pid} end}. fun_pid(Fun) -> erlang:fun_info(Fun, pid). -- cgit v1.2.3