DEV Community

Discussion on: 100 Languages Speedrun: Episode 65: Randomized Finite Automaton for Fast Thue Interpreter in Crystal

Collapse
 
asterite profile image
Ary Borenszweig

Really nice post, as usual!

Some small things to further improve the performance:

  • Use str.each_char_with_index instead of str.chars.each_with_index (the former doesn't allocate memory while the latter does)
  • Use active.compact_map { |t| t[char] } instead of active.map { |t| t[char] }.compact (one less intermediate array)
  • Do something like active.clear; active.push @trie; active.concat next_tries instead of creating a new array for each char

That said, I totally understand that the goal is to improve performance compared to before while also keeping the code as readable as possible. I'm just suggesting these because you said "There's probably a lot small optimizations" and because I like optimizing things :-)

Collapse
 
taw profile image
Tomasz Wegrzanowski

Oh for sure. I think the biggest performance savings would come from switching from processing characters to processing bytes, as in UTF-8 this works perfectly well without any changes, and then we could just use 256 entry arrays instead of hashes for the trie.

Not like this really makes much difference for programs with so few rules.