Prefix Trees in Action
medium.com
medium.com
> Making an operation that accounts for 1% of the overall runtime 100% faster is far less effective than making something that accounts for 50% of the runtime10% faster.
Currently dealing with a slow DB and a team of "engineers" who have just NOT internalized the above. It's been very demoralizing.
https://www.youtube.com/watch?v=r-TLSBdHe1A
And who knows, maybe they'd even be interested in using coz afterwards:
I've never actually worked with a prefix tree before, so this was quite helpful for me.
This may be of interest as well - a related paper, Pigasus, does this with specialized hardware but it's a similar problem of applying 10s of thousands of rules as quickly as possible. I found it while reading through acolyer's morning paper. https://blog.acolyer.org/2020/11/16/pigasus/
Again, slightly different task - they wouldn't be going for the longest match exclusively, but all matches. Also, the rules they use are fundamentally regular expressions, so the filtering pass is to take advantage of the domain specific knowledge that rule-matches are very rare.
I believe Go's regex engine is built on RE2 though, so idk.
But yes, tries are very cool.
Worse, they are stupid common in interviews.
- Barring very degenerate inputs, prefix tries are usually faster than `std::set<std::string>`. And for longer strings, prefix tries are definitely faster than `std::unordered_set<std::string>`.
- The same ideas that apply to strings apply to bitvectors too, so we can handle arbitrary binary data the same way. (This gives us things like Patricia trees.)
- They're great for implementing immutable sets. In fact most functional languages use some variant of prefix tries for their set/map data structures.
And some real-world use cases:
- Symbol lookup in mach-O (macOS) binaries use prefix tries
- One of the fastest string sorting algorithms (burstsort) uses prefix tries
- Program analysis / abstract interpretation really benefits from immutable sets of bitvectors with fast union operations
- Pretty sure every typeahead / autocomplete implementation uses some kind of trie
- And for something a bit more esoteric: scrabble/boggle AIs use them too
I suspect you are right on most uses, though for many first iterations a simple ordered set works wonderfully well, and is much easier to think of.
I want to point out that I wasn't forced though. Rather, they asked around if people have some interesting things to share and this particular PR seemed a good fit for an article. I don't really write about Go stuff on my own blog so I figured why not contribute to our company blog.