Skip to content

Commit 3ac7593

Browse files
michalmuskalaericmj
authored andcommitted
Inline String.duplicate to :binary.copy BIF (#6238)
Furthermore, also inline duplicate/2 inside the String module, since it's used there in couple places.
1 parent 71c47db commit 3ac7593

File tree

3 files changed

+7
-2
lines changed

3 files changed

+7
-2
lines changed

lib/elixir/lib/string.ex

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1177,9 +1177,13 @@ defmodule String do
11771177

11781178
defp do_reverse(nil, acc), do: IO.iodata_to_binary(acc)
11791179

1180+
@compile {:inline, duplicate: 2}
1181+
11801182
@doc """
11811183
Returns a string `subject` duplicated `n` times.
11821184
1185+
Inlined by the compiler.
1186+
11831187
## Examples
11841188
11851189
iex> String.duplicate("abc", 0)
@@ -1193,7 +1197,7 @@ defmodule String do
11931197
11941198
"""
11951199
@spec duplicate(t, non_neg_integer) :: t
1196-
def duplicate(subject, n) when is_integer(n) and n >= 0 do
1200+
def duplicate(subject, n) do
11971201
:binary.copy(subject, n)
11981202
end
11991203

lib/elixir/src/elixir_rewrite.erl

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,6 +151,7 @@ inline(?port, list, 0) -> {erlang, ports};
151151
inline(?string, to_float, 1) -> {erlang, binary_to_float};
152152
inline(?string, to_integer, 1) -> {erlang, binary_to_integer};
153153
inline(?string, to_integer, 2) -> {erlang, binary_to_integer};
154+
inline(?string, duplicate, 2) -> {binary, copy};
154155

155156
inline(?system, stacktrace, 0) -> {erlang, get_stacktrace};
156157
inline(?system, monotonic_time, 0) -> {erlang, monotonic_time};

lib/elixir/test/elixir/string_test.exs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -372,7 +372,7 @@ defmodule StringTest do
372372
assert String.duplicate("abc", 1) == "abc"
373373
assert String.duplicate("abc", 2) == "abcabc"
374374
assert String.duplicate("&ã$", 2) == "&ã$&ã$"
375-
assert_raise FunctionClauseError, fn ->
375+
assert_raise ArgumentError, fn ->
376376
String.duplicate("abc", -1)
377377
end
378378
end

0 commit comments

Comments
 (0)