Skip to content

Commit eb7c541

Browse files
author
José Valim
committed
Inline functions in the Tuple module, closes #2261
1 parent 80b3183 commit eb7c541

File tree

3 files changed

+28
-2
lines changed

3 files changed

+28
-2
lines changed

lib/elixir/lib/tuple.ex

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ defmodule Tuple do
99
Creates a tuple of size `size` containing the
1010
given `data` at every position.
1111
12+
Inlined by the compiler.
13+
1214
## Examples
1315
1416
iex> Tuple.duplicate(:hello, 3)
@@ -27,6 +29,8 @@ defmodule Tuple do
2729
Raises an `ArgumentError` if `index` is greater than the
2830
length of `tuple`.
2931
32+
Inlined by the compiler.
33+
3034
## Examples
3135
3236
iex> tuple = {:bar, :baz}
@@ -46,6 +50,8 @@ defmodule Tuple do
4650
Raises an `ArgumentError` if `index` is greater than
4751
or equal to the length of `tuple`.
4852
53+
Inlined by the compiler.
54+
4955
## Examples
5056
5157
iex> tuple = {:foo, :bar, :baz}

lib/elixir/src/elixir_dispatch.erl

Lines changed: 11 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,12 @@
1010
-include("elixir.hrl").
1111
-import(ordsets, [is_element/2]).
1212

13-
%% Module macros
1413
-define(kernel, 'Elixir.Kernel').
14+
-define(map, 'Elixir.Map').
1515
-define(node, 'Elixir.Node').
1616
-define(process, 'Elixir.Process').
1717
-define(system, 'Elixir.System').
18-
-define(map, 'Elixir.Map').
18+
-define(tuple, 'Elixir.Tuple').
1919

2020
default_functions() ->
2121
[ {?kernel, elixir_imported_functions()} ].
@@ -341,8 +341,17 @@ rewrite(?kernel, elem, [Tuple, Index], _) ->
341341
{ok, erlang, element, [increment(Index), Tuple]};
342342
rewrite(?kernel, set_elem, [Tuple, Index, Value], _) ->
343343
{ok, erlang, setelement, [increment(Index), Tuple, Value]};
344+
344345
rewrite(?process, monitor, [Arg], _) ->
345346
{ok, erlang, monitor, [process, Arg]};
347+
348+
rewrite(?tuple, insert_at, [Tuple, Index, Term], _) ->
349+
{ok, erlang, insert_element, [increment(Index), Tuple, Term]};
350+
rewrite(?tuple, delete_at, [Tuple, Index], _) ->
351+
{ok, erlang, delete_element, [increment(Index), Tuple]};
352+
rewrite(?tuple, duplicate, [Data, Size], _) ->
353+
{ok, erlang, make_tuple, [Size, Data]};
354+
346355
rewrite(?map, 'has_key?', [Map, Key], _) ->
347356
{ok, maps, is_key, [Key, Map]};
348357
rewrite(?map, fetch, [Map, Key], _) ->

lib/elixir/test/elixir/tuple_test.exs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,17 +28,28 @@ defmodule TupleTest do
2828
end
2929

3030
# Tuple module
31+
# We check two variants due to inlining.
3132

3233
test :duplicate do
3334
assert Tuple.duplicate(:foo, 0) == {}
3435
assert Tuple.duplicate(:foo, 3) == {:foo, :foo, :foo}
36+
37+
mod = Tuple
38+
assert mod.duplicate(:foo, 0) == {}
39+
assert mod.duplicate(:foo, 3) == {:foo, :foo, :foo}
3540
end
3641

3742
test :insert_at do
3843
assert Tuple.insert_at({:bar, :baz}, 0, :foo) == {:foo, :bar, :baz}
44+
45+
mod = Tuple
46+
assert mod.insert_at({:bar, :baz}, 0, :foo) == {:foo, :bar, :baz}
3947
end
4048

4149
test :delete_at do
4250
assert Tuple.delete_at({:foo, :bar, :baz}, 0) == {:bar, :baz}
51+
52+
mod = Tuple
53+
assert mod.delete_at({:foo, :bar, :baz}, 0) == {:bar, :baz}
4354
end
4455
end

0 commit comments

Comments
 (0)