Skip to content

Commit b51fd94

Browse files
committed
Perform more loading on the client if possible
1 parent d9e84b0 commit b51fd94

File tree

2 files changed

+33
-13
lines changed

2 files changed

+33
-13
lines changed

lib/elixir/lib/kernel/parallel_compiler.ex

+18-6
Original file line numberDiff line numberDiff line change
@@ -441,10 +441,10 @@ defmodule Kernel.ParallelCompiler do
441441
:erlang.put(:elixir_compiler_file, file)
442442

443443
try do
444-
case output do
445-
{:compile, _} -> compile_file(file, dest, false, parent)
446-
:compile -> compile_file(file, dest, true, parent)
447-
:require -> require_file(file, parent)
444+
if output == :require do
445+
require_file(file, parent)
446+
else
447+
compile_file(file, dest, parent)
448448
end
449449
catch
450450
kind, reason ->
@@ -549,9 +549,9 @@ defmodule Kernel.ParallelCompiler do
549549
wait_for_messages([], spawned, waiting, files, result, warnings, errors, state)
550550
end
551551

552-
defp compile_file(file, path, force_load?, parent) do
552+
defp compile_file(file, path, parent) do
553553
:erlang.process_flag(:error_handler, Kernel.ErrorHandler)
554-
:erlang.put(:elixir_compiler_dest, {path, force_load?})
554+
:erlang.put(:elixir_compiler_dest, path)
555555
:elixir_compiler.file(file, &each_file(&1, &2, parent))
556556
end
557557

@@ -652,6 +652,18 @@ defmodule Kernel.ParallelCompiler do
652652
state
653653
)
654654

655+
{:load_module?, child, ref, module} ->
656+
# If compiling files to disk, we only load the module
657+
# if other modules are waiting for it.
658+
load? =
659+
case state.output do
660+
{:compile, _} -> match?(%{{:module, ^module} => [_ | _]}, result)
661+
_ -> true
662+
end
663+
664+
send(child, {ref, load?})
665+
spawn_workers(queue, spawned, waiting, files, result, warnings, errors, state)
666+
655667
{:module_available, child, ref, file, module, binary, loaded?} ->
656668
state.each_module.(file, module, binary)
657669

lib/elixir/src/elixir_module.erl

+15-7
Original file line numberDiff line numberDiff line change
@@ -159,7 +159,7 @@ compile(Meta, Module, ModuleAsCharlist, Block, Vars, Prune, E) ->
159159
put_compiler_modules([Module | CompilerModules]),
160160
{Result, ModuleE, CallbackE} = eval_form(Line, Module, DataBag, Block, Vars, Prune, E),
161161
CheckerInfo = checker_info(),
162-
{BeamLocation, Forceload} = beam_location(ModuleAsCharlist),
162+
BeamLocation = beam_location(ModuleAsCharlist),
163163

164164
{Binary, PersistedAttributes, Autoload} =
165165
elixir_erl_compiler:spawn(fun() ->
@@ -219,7 +219,7 @@ compile(Meta, Module, ModuleAsCharlist, Block, Vars, Prune, E) ->
219219

220220
compile_error_if_tainted(DataSet, E),
221221
Binary = elixir_erl:compile(ModuleMap),
222-
Autoload = Forceload or proplists:get_value(autoload, CompileOpts, false),
222+
Autoload = proplists:get_value(autoload, CompileOpts, false) or load_module(Module),
223223
spawn_parallel_checker(CheckerInfo, Module, ModuleMap, BeamLocation),
224224
{Binary, PersistedAttributes, Autoload}
225225
end),
@@ -550,12 +550,10 @@ bag_lookup_element(Table, Name, Pos) ->
550550

551551
beam_location(ModuleAsCharlist) ->
552552
case get(elixir_compiler_dest) of
553-
{Dest, ForceLoad} when is_binary(Dest) ->
554-
BeamLocation =
555-
filename:join(elixir_utils:characters_to_list(Dest), ModuleAsCharlist ++ ".beam"),
556-
{BeamLocation, ForceLoad};
553+
Dest when is_binary(Dest) ->
554+
filename:join(elixir_utils:characters_to_list(Dest), ModuleAsCharlist ++ ".beam");
557555
_ ->
558-
{"", true}
556+
""
559557
end.
560558

561559
%% Integration with elixir_compiler that makes the module available
@@ -593,6 +591,16 @@ make_module_available(Module, Binary, Loaded) ->
593591
receive {Ref, ack} -> ok end
594592
end.
595593

594+
load_module(Module) ->
595+
case get(elixir_compiler_info) of
596+
undefined ->
597+
true;
598+
{PID, _} ->
599+
Ref = make_ref(),
600+
PID ! {'load_module?', self(), Ref, Module},
601+
receive {Ref, Boolean} -> Boolean end
602+
end.
603+
596604
%% Error handling and helpers.
597605

598606
%% We've reached the elixir_module or eval internals, skip it with the rest

0 commit comments

Comments
 (0)