Skip to content

Commit baf7b7a

Browse files
michalmuskalaJosé Valim
authored andcommitted
Optimise binary pattern matching in String.Break (#6340)
In downcase and upcase pattern match tail as ::bits instead of ::binary. This saves us a check on each iteration if the tail is a proper binary. We only do this check in the very beginning. In trim_leading additionally make sure every head of the recursive function pattern matches on a binary in first argument so the single binary match context optimisation can be applied.
1 parent aee5dc2 commit baf7b7a

File tree

1 file changed

+12
-9
lines changed

1 file changed

+12
-9
lines changed

lib/elixir/unicode/properties.ex

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -70,31 +70,31 @@ defmodule String.Casing do
7070

7171
# Downcase
7272

73-
def downcase(string), do: downcase(string, "")
73+
def downcase(string) when is_binary(string), do: downcase(string, "")
7474

7575
for {codepoint, _upper, lower, _title} <- codes, lower && lower != codepoint do
76-
defp downcase(unquote(codepoint) <> rest, acc) do
76+
defp downcase(<<unquote(codepoint), rest::bits>>, acc) do
7777
downcase(rest, acc <> unquote(lower))
7878
end
7979
end
8080

81-
defp downcase(<<char, rest::binary>>, acc) do
81+
defp downcase(<<char, rest::bits>>, acc) do
8282
downcase(rest, <<acc::binary, char>>)
8383
end
8484

8585
defp downcase("", acc), do: acc
8686

8787
# Upcase
8888

89-
def upcase(string), do: upcase(string, "")
89+
def upcase(string) when is_binary(string), do: upcase(string, "")
9090

9191
for {codepoint, upper, _lower, _title} <- codes, upper && upper != codepoint do
92-
defp upcase(unquote(codepoint) <> rest, acc) do
92+
defp upcase(<<unquote(codepoint), rest::bits>>, acc) do
9393
upcase(rest, acc <> unquote(upper))
9494
end
9595
end
9696

97-
defp upcase(<<char, rest::binary>>, acc) do
97+
defp upcase(<<char, rest::bits>>, acc) do
9898
upcase(rest, <<acc::binary, char>>)
9999
end
100100

@@ -140,11 +140,14 @@ defmodule String.Break do
140140

141141
# trim_leading
142142

143+
def trim_leading(string) when is_binary(string) do
144+
do_trim_leading(string)
145+
end
146+
143147
for codepoint <- whitespace do
144-
def trim_leading(unquote(codepoint) <> rest), do: trim_leading(rest)
148+
def do_trim_leading(<<unquote(codepoint), rest::bits>>), do: do_trim_leading(rest)
145149
end
146-
def trim_leading(""), do: ""
147-
def trim_leading(string) when is_binary(string), do: string
150+
def do_trim_leading(<<rest::bits>>), do: rest
148151

149152
# trim_trailing
150153

0 commit comments

Comments
 (0)