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 :-)
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.
For further actions, you may consider blocking this person and/or reporting abuse
We're a place where coders share, stay up-to-date and grow their careers.
Really nice post, as usual!
Some small things to further improve the performance:
str.each_char_with_indexinstead ofstr.chars.each_with_index(the former doesn't allocate memory while the latter does)active.compact_map { |t| t[char] }instead ofactive.map { |t| t[char] }.compact(one less intermediate array)active.clear; active.push @trie; active.concat next_triesinstead of creating a new array for each charThat 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 :-)
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.