From ad2962278f347a5d0341c0d5d11cbb71c83eb35b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Bj=C3=B6rn=20Gustavsson?= Date: Thu, 17 Nov 2011 20:25:48 +0100 Subject: Avoid slow code loading of BEAM code in a hipe-enabled emulator On my Linux computer, building the entire Erlang/OTP system with hipe disabled took about 8 minutes. With hipe enabled, but without any native code, the build took about 23 minutes, i.e. more than 3 times slower. (The computer has 4 cores, and I used 'make -j6'.) On my eight-core Mac (running 'make -j10') there was only a slight slowdown when hipe was enabled. The culprit is hipe_unified_loader:post_beam_load/1, which will be called every time a module is loaded (even if the module contains no native code). If post_beam_load/1 is called in a hipe-enabled emulator, it will block multi-scheduling, even if no work needs to be done. Apparently the cost for blocking multi-scheduling can vary greatly, depending on the operating system and system load. As a quick and conservative fix, don't call post_beam_load/1 unless some native code has been previously loaded. --- lib/kernel/src/code_server.erl | 33 ++++++++++++++++++++++++++------- 1 file changed, 26 insertions(+), 7 deletions(-) (limited to 'lib/kernel/src/code_server.erl') diff --git a/lib/kernel/src/code_server.erl b/lib/kernel/src/code_server.erl index e3d22e7999..32a12e2b52 100644 --- a/lib/kernel/src/code_server.erl +++ b/lib/kernel/src/code_server.erl @@ -32,6 +32,8 @@ -import(lists, [foreach/2]). +-define(ANY_NATIVE_CODE_LOADED, any_native_code_loaded). + -record(state, {supervisor, root, path, @@ -97,6 +99,8 @@ init(Ref, Parent, [Root,Mode0]) -> State0 end, + put(?ANY_NATIVE_CODE_LOADED, false), + Parent ! {Ref,{ok,self()}}, loop(State#state{supervisor = Parent}). @@ -1278,20 +1282,35 @@ load_native_code(Mod, Bin) -> %% Therefore we must test for that the loader modules are available %% before trying to to load native code. case erlang:module_loaded(hipe_unified_loader) of - false -> no_native; - true -> hipe_unified_loader:load_native_code(Mod, Bin) + false -> + no_native; + true -> + Result = hipe_unified_loader:load_native_code(Mod, Bin), + case Result of + {module,_} -> + put(?ANY_NATIVE_CODE_LOADED, true); + _ -> + ok + end, + Result end. hipe_result_to_status(Result) -> case Result of - {module,_} -> Result; - _ -> {error,Result} + {module,_} -> + put(?ANY_NATIVE_CODE_LOADED, true), + Result; + _ -> + {error,Result} end. post_beam_load(Mod) -> - case erlang:module_loaded(hipe_unified_loader) of - false -> ok; - true -> hipe_unified_loader:post_beam_load(Mod) + %% post_beam_load/1 can potentially be very expensive because it + %% blocks multi-scheduling; thus we want to avoid the call if we + %% know that it is not needed. + case get(?ANY_NATIVE_CODE_LOADED) of + true -> hipe_unified_loader:post_beam_load(Mod); + false -> ok end. int_list([H|T]) when is_integer(H) -> int_list(T); -- cgit v1.2.3