jayjun / slugify

An Elixir library to convert strings in any language into slugs
MIT License
124 stars 10 forks source link

Use pattern matching lookup to speed things up #1

Closed Kabie closed 5 years ago

Kabie commented 7 years ago

I believe this is a perfect usecase for pattern matching lookup:

https://github.com/devonestes/fast-elixir#map-lookup-vs-pattern-matching-lookup-code

jayjun commented 7 years ago

Thanks for the link. My first go used pattern matching but as it turns out, compiling 39,000+ functions takes a really long time (3-4 minutes).

for {codepoint, replacement} <- @replacements do
  defp transliterate([unquote(codepoint) | rest], acc, ignored_codepoints) do
    if unquote(codepoint) in ignored_codepoints do
      transliterate(rest, [<<unquote(codepoint)::utf8>> | acc], ignored_codepoints)
    else
      transliterate(rest, [unquote(replacement) | acc], ignored_codepoints)
    end
  end
end

I went with a touch longer execution time (less than 1 µs more, apparently).

jayjun commented 5 years ago

Five Elixir releases later and compilation still takes a minute and a half on my machine.

Closing because the slightly faster runtime performance will probably never outweigh much slower builds.