To Trie or not to Trie – a comparison of efficient data structures
bhavin.directi.com
bhavin.directi.com
One thing I'd add is that if you aren't exploiting the ordered nature of the keys within a trie - i.e. if your keys have no underlying substructure or you aren't doing linear scans through the key space - then you don't want a trie at all and you'd be better off with a hash table.
A hash table will be faster and use less memory if your keys are unstructured (imagine for a minute choosing random integers for your keys - then mod N is a good hash function for those keys, so it is obvious a hash table will work well for them, whereas a trie will waste space trying to find common prefixes between random keys.)
If you are accessing completely randomly, then the cache utilisation will be similar between a good trie and a hash table. If you are accessing some things more than others, then you should use a move-to-front hash table to be competitive.
It is obvious, but it is wrong. I observed Judy beating out many hash tables in specifically this situation, and comparing favorably to google's sparsehash. Given its much smaller code size and fewer dependencies this has demonstrated itself a huge win at my organization.
Link to my benchmark: http://reddit.com/r/programming/comments/bhxwh/hash_table_be...
I'd be interested to see 10 million 64-bit integers in Judy and DenseHash though. Since ten million is 0.2% of 2^32, in some sense lots of the potential key space is actually in use, so I'd expect a lot of sharing to be possible and a sparse trie to perform well.
Is there a 64-bit integer key Judy array you could test on?
Simply using the 64-bit integer as a 8-byte key is what I use.
Treating the 32-bit integer as 4-byte keys performs similarly to using the integer-API directly.
> I'd be interested to see 10 million 64-bit integers in Judy and DenseHash though
It's easy enough to check :)
Further, the article is only an announcement that some benchmarking would be happening later...
But (linked directly from the wikipedia article), this article gives benchmarking and a good argument that a "Judy array" is a rather over-engineered solution: http://www.nothings.org/computer/judy/ (and that article is linked to the wikipedia article).
- Bhavin
This is why Judy arrays seemed a bit odd to me. I feel that if I put as much effort into making a super-fast hash table as the author did into making a super-fast trie, I feel like it would perform as well or better. That's just a gut-instinct though and I don't really have any data to back it up.
I suppose an exception could when the median key length is very short, tries tend to be better since a secure hash function could take longer than a very small number of memory lookups(especially since some of them will be cached).
It seems like it could be more efficient as you could send to the browser the node ID of the current buffer and then when the next key was pressed, use the current ID to efficiently look forward just one level.
Im working on a project to do this right now in fact, since it seeems all the open-source autocomplete implementations I can find just come down to doing a "SELECT bla where bla like bla", which is pretty slow. The one major issue is that tries are prefix-based, so it has to match starting from the beginning of the query. My sort of solution around that is to break up the terms your matching against into groups of phrases, and allow prefix matching on any of those phrases.
As a question to HN-I was planning on using Radix Trees, but now Ive read this article it seems like there might be some other good options. Are any of those structures noticeable better for storing lots of strings?
The worst cases are editing midstring, and worst, pasting to the start of the query string. Still, those are O(q) rather than O(N) where N are the total number of strings in the database. In my case, N will have millions of records where q will rarely exceed 250 characters.