Skip to content

Commit 20f8987

Browse files
authored
Optimize Kernel.binary_slice/2 for stepped ranges (#15989)
1 parent e8adeee commit 20f8987

1 file changed

Lines changed: 12 additions & 6 deletions

File tree

‎lib/elixir/lib/kernel.ex‎

Lines changed: 12 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5045,13 +5045,19 @@ defmodule Kernel do
50455045
amount = last - first + 1
50465046

50475047
if first < total and amount > 0 do
5048-
part = binary_part(binary, first, min(amount, total - first))
5048+
length = min(amount, total - first)
50495049

5050-
if step == 1 do
5051-
part
5052-
else
5053-
<<first_byte, rest::binary>> = part
5054-
for <<_::size(^step - 1)-bytes, byte <- rest>>, into: <<first_byte>>, do: <<byte>>
5050+
cond do
5051+
step == 1 ->
5052+
binary_part(binary, first, length)
5053+
5054+
length <= step ->
5055+
binary_part(binary, first, 1)
5056+
5057+
true ->
5058+
<<first_byte, rest::binary>> = binary_part(binary, first, length)
5059+
skip = step - 1
5060+
for <<_::size(^skip)-bytes, byte <- rest>>, into: <<first_byte>>, do: <<byte>>
50555061
end
50565062
else
50575063
""

0 commit comments

Comments
 (0)