Skip to content

Commit 572726c

Browse files
author
José Valim
committed
Remove unecessary apply optimization
1 parent 1885841 commit 572726c

File tree

4 files changed

+29
-58
lines changed

4 files changed

+29
-58
lines changed

lib/elixir/lib/kernel.ex

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -354,28 +354,34 @@ defmodule Kernel do
354354

355355
@doc """
356356
Invokes the given `fun` with the array of arguments `args`.
357+
Inlines to `:erlang.apply/2`.
357358
358359
## Examples
359360
360361
iex> apply(fn x -> x * 2 end, [2])
361362
4
362363
363364
"""
364-
def apply(fun, args) do
365-
:erlang.apply(fun, args)
365+
defmacro apply(fun, args) do
366+
quote do
367+
:erlang.apply(unquote(fun), unquote(args))
368+
end
366369
end
367370

368371
@doc """
369372
Invokes the given `fun` from `module` with the array of arguments `args`.
373+
Inlines to `:erlang.apply/3`.
370374
371375
## Examples
372376
373377
iex> apply(Enum, :reverse, [[1, 2, 3]])
374378
[3,2,1]
375379
376380
"""
377-
def apply(module, fun, args) do
378-
:erlang.apply(module, fun, args)
381+
defmacro apply(module, fun, args) do
382+
quote do
383+
:erlang.apply(unquote(module), unquote(fun), unquote(args))
384+
end
379385
end
380386

381387
@doc """

lib/elixir/src/elixir_dispatch.erl

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -398,6 +398,8 @@ in_elixir_macros() ->
398398
in_erlang_functions() ->
399399
[
400400
{ abs, 1 },
401+
{ apply, 2 },
402+
{ apply, 3 },
401403
{ atom_to_list, 1 },
402404
{ binary_part, 3 },
403405
{ binary_to_float, 1 },
@@ -491,8 +493,6 @@ in_erlang_macros() ->
491493
{'>=',2},
492494
{'@',1},
493495
{'and',2},
494-
{apply,2},
495-
{apply,3},
496496
{'case',2},
497497
{def,1},
498498
{def,2},

lib/elixir/src/elixir_macros.erl

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
%% but do not since they need to be implemented in Erlang.
33
-module(elixir_macros).
44
-export([translate/2]).
5-
-import(elixir_translator, [translate_each/2, translate_args/2, translate_apply/7]).
5+
-import(elixir_translator, [translate_each/2]).
66
-import(elixir_scope, [umergec/2, umergea/2]).
77
-import(elixir_errors, [compile_error/3, syntax_error/3, syntax_error/4,
88
assert_no_function_scope/3, assert_module_scope/3, assert_no_match_or_guard_scope/3]).
@@ -184,17 +184,6 @@ translate({Kind, Meta, [Call, Expr]}, S) when ?defs(Kind) ->
184184
(not QC#elixir_quote.unquoted) andalso (not QE#elixir_quote.unquoted),
185185
{ elixir_def:wrap_definition(Kind, Meta, TCall, TExpr, CheckClauses, SE), SE };
186186

187-
%% Apply - Optimize apply by checking what doesn't need to be dispatched dynamically
188-
189-
translate({ apply, Meta, [Left, Right, Args] }, S) when is_list(Args) ->
190-
{ TLeft, SL } = translate_each(Left, S),
191-
{ TRight, SR } = translate_each(Right, umergec(S, SL)),
192-
translate_apply(Meta, TLeft, TRight, Args, S, SL, SR);
193-
194-
translate({ apply, Meta, Args }, S) ->
195-
{ TArgs, NS } = translate_args(Args, S),
196-
{ ?wrap_call(?line(Meta), erlang, apply, TArgs), NS };
197-
198187
translate({ Name, Meta, Args }, S) ->
199188
syntax_error(Meta, S#elixir_scope.file,
200189
"invalid arguments for macro ~ts/~B", [Name, length(Args)]).

lib/elixir/src/elixir_translator.erl

Lines changed: 16 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,7 @@
22
%% overriden are defined in this file.
33
-module(elixir_translator).
44
-export([forms/4, 'forms!'/4]).
5-
-export([translate/2, translate_each/2, translate_arg/2,
6-
translate_args/2, translate_apply/7]).
5+
-export([translate/2, translate_each/2, translate_arg/2, translate_args/2]).
76
-import(elixir_scope, [umergev/2, umergec/2, umergea/2]).
87
-import(elixir_errors, [syntax_error/3, syntax_error/4,
98
compile_error/3, compile_error/4,
@@ -386,9 +385,22 @@ translate_each({ Atom, Meta, Args }, S) when is_atom(Atom), is_list(Meta), is_li
386385
translate_each({ { '.', _, [Left, Right] }, Meta, Args }, S)
387386
when (is_tuple(Left) orelse is_atom(Left)), is_atom(Right), is_list(Meta), is_list(Args) ->
388387
{ TLeft, SL } = translate_each(Left, S),
389-
390388
{ TRight, SR } = translate_each(Right, umergec(S, SL)),
391-
Callback = fun() -> translate_apply(Meta, TLeft, TRight, Args, S, SL, SR) end,
389+
390+
Callback = fun() ->
391+
case TLeft of
392+
{ atom, _, Receiver } ->
393+
Tuple = { element(3, TRight), length(Args) },
394+
elixir_lexical:record_remote(Receiver, S#elixir_scope.lexical_tracker),
395+
elixir_tracker:record_remote(Tuple, Receiver, S#elixir_scope.module, S#elixir_scope.function);
396+
_ ->
397+
ok
398+
end,
399+
400+
Line = ?line(Meta),
401+
{ TArgs, SA } = translate_args(Args, umergec(S, SR)),
402+
{ { call, Line, { remote, Line, TLeft, TRight }, TArgs }, umergev(SL, umergev(SR, SA)) }
403+
end,
392404

393405
case TLeft of
394406
{ atom, _, Receiver } ->
@@ -584,42 +596,6 @@ translate_args(Args, S) ->
584596
{ TArgs, { SC, SV } } = lists:mapfoldl(fun translate_arg/2, {S, S}, Args),
585597
{ TArgs, umergea(SV, SC) }.
586598

587-
%% Translate apply
588-
%% Used by both apply and external function invocation macros.
589-
590-
translate_apply(Meta, TLeft, TRight, Args, S, SL, SR) ->
591-
Line = ?line(Meta),
592-
593-
Optimize = case (Args == []) orelse lists:last(Args) of
594-
{ '|', _, _ } -> false;
595-
_ ->
596-
case TRight of
597-
{ atom, _, _ } -> true;
598-
_ -> false
599-
end
600-
end,
601-
602-
case Optimize of
603-
true ->
604-
%% Register the remote
605-
case TLeft of
606-
{ atom, _, Receiver } ->
607-
Tuple = { element(3, TRight), length(Args) },
608-
elixir_lexical:record_remote(Receiver, S#elixir_scope.lexical_tracker),
609-
elixir_tracker:record_remote(Tuple, Receiver, S#elixir_scope.module, S#elixir_scope.function);
610-
_ ->
611-
ok
612-
end,
613-
614-
{ TArgs, SA } = translate_args(Args, umergec(S, SR)),
615-
FS = umergev(SL, umergev(SR,SA)),
616-
{ { call, Line, { remote, Line, TLeft, TRight }, TArgs }, FS };
617-
false ->
618-
{ TArgs, SA } = translate_each(Args, umergec(S, SR)),
619-
FS = umergev(SL, umergev(SR,SA)),
620-
{ ?wrap_call(Line, erlang, apply, [TLeft, TRight, TArgs]), FS }
621-
end.
622-
623599
%% __op__ helpers
624600

625601
assert_no_match_scope_for_bin_op(Meta, Op, S)

0 commit comments

Comments
 (0)