LogoopenSUSE Build Service > Projects
Sign Up | Log In

View File 0139-Short-circuit-code-ensure_loaded-for-already-loaded-.patch of Package erlang (Project home:Ledest:erlang:20)

From 3e0cecc541fc9cd85b4c5878e58a7ec4a3142368 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Micha=C5=82=20Muska=C5=82a?= <michal@muskala.eu>
Date: Wed, 7 Mar 2018 10:56:23 +0100
Subject: [PATCH] Short-circuit code:ensure_loaded for already-loaded modules

This checks if the module is already loaded using erlang:module_loaded
before calling the code server. This should improve performance of the
call significantly since the case where module is already loaded is the
common one.

The change shouldn't cause any problems. It's worth noting that
code:ensure_modules_loaded already does a similar check.
---
 lib/kernel/src/code.erl | 7 +++++--
 1 file changed, 5 insertions(+), 2 deletions(-)

diff --git a/lib/kernel/src/code.erl b/lib/kernel/src/code.erl
index 9969021a6c..f143a49d2f 100644
--- a/lib/kernel/src/code.erl
+++ b/lib/kernel/src/code.erl
@@ -149,8 +149,11 @@ load_file(Mod) when is_atom(Mod) ->
 -spec ensure_loaded(Module) -> {module, Module} | {error, What} when
       Module :: module(),
       What :: embedded | badfile | nofile | on_load_failure.
-ensure_loaded(Mod) when is_atom(Mod) -> 
-    call({ensure_loaded,Mod}).
+ensure_loaded(Mod) when is_atom(Mod) ->
+    case erlang:module_loaded(Mod) of
+        true -> {module, Mod};
+        false -> call({ensure_loaded,Mod})
+    end.
 
 %% XXX File as an atom is allowed only for backwards compatibility.
 -spec load_abs(Filename) -> load_ret() when
-- 
2.16.3