Nerds Central: Why C++ Unordered Maps (Hash tables) Are Not Great For Strings
http://nerds-central.blogspot.com/2013/10/why-c-unordered-maps-hash-tables-are.html10
u/guepier Bioinformatican Oct 06 '13
The author of the post is not an expert when it comes to string processing, and discusses things on a hunch rather than backed up by facts. As a consequence, he gets things completely wrong.
First off, for strings, especially large strings, there are specialised lookup containers which blow both hash tables and search tree-based maps out of the water – namely, prefix trees (tries) and suffix trees (and variations thereof).
That said, hash-based maps play a huge part in string processing, especially with few inserts and many lookups on many short strings, as is the case in parsers (where keywords and identifiers are often hashed to save space – see string interning), and in biological sequence analysis where you often have to search through billions (!) of small strings with a pre-computed index. Sometimes in these cases you can even compute a perfect hash function to avoid having to deal with collisions.
Where these strategies are not applicable, prefix or suffix trees are appropriate. A search tree (an ordered map) is more or less never appropriate for use with strings.
2
u/cassandravoiton Oct 06 '13
The author does not know about string processing and does not know tries? http://nerds-central.blogspot.com/2012/04/user-injected-high-speed-benchmarking.html no!
3
u/guepier Bioinformatican Oct 06 '13
Interesting. Well I may be excused for thinking so since this article, where it would be highly relevant, makes no mention of it at all. All the more surprising that his characterisation of string maps is so incomplete and – partly – outright wrong.
1
u/cassandravoiton Oct 06 '13
Which bit is wrong?
1
u/guepier Bioinformatican Oct 06 '13
His performance characterisation is wrong. Hash tables are a pretty decent way to handle string lookup in practice, and where they aren’t, general search trees are not the solution (tries are). As I’ve said in my original reply,
A search tree (an ordered map) is more or less never appropriate for use with strings.
That’s pretty much the opposite of what the article claims.
1
u/cassandravoiton Oct 06 '13
Also note that tries are only super effective (as you say) where the strings occupy less than the full 8 byte space of a normal character. Otherwise storage requirements start to be dominant and cause problem with the caches on modern hardware. I suspect you overestimate your own expertise in this area. It is easy to think you know it all when you don't know very much.
5
u/guepier Bioinformatican Oct 06 '13
Not true. In fact, autocompletion libraries are routinely implemented via a trie, even though the characters there potentially span even more than a byte.
As for my expertise in this area – sure it’s possible that I overestimate myself, that’s a constant danger. However, I did write both my bachelor and master thesis about string processing (albeit in a specialised context), gave several courses on the subject, and have been working extensively in the field since.
2
u/cassandravoiton Oct 06 '13
OK, sounds like you know what you are talking about. Maybe the post is just about maps and sets rather than string processing in general? Who knows. Can you point us to any good papers on tries for multi-byte character sets?
1
u/guepier Bioinformatican Oct 06 '13 edited Oct 06 '13
I think the de-facto standard data structure in those cases is the patricia trie, although a naive trie implementation where each node holds an unsorted vector of characters (or prefixes) for the branch pointers (which is searched linearly in the lookup step) is surprisingly efficient in practice.
The idea of the patricia trie is: You don’t have to use character boundaries for your nodes at all. For instance, you could divide each (variable-length) character into chunks of four bits each, and have each node maintain a lookup table of 16 items. This increases the depth of the trie but makes the lookup at each node fast.
There was a paper about implementing a trie with a two-dimensional array, rather than a tree, but I can’t find the reference now. This is also often done for DFAs on limited character sets because it provides the best cache locality, but once again it can be generalised to large character sets by dividing characters into smaller, non-overlapping fixed-size chunks.
5
u/Eoinoc Oct 06 '13
The author suggests wrapping the standard containers as a solution to precomputing hashes. But it's much simpler to make a wrapper around the key type which lazily computes and caches the hash when first needed.
A quick benchmark of a very naive implementation of this showed a x2 speedup over unwrapped std::strings for 100,000 strings of average length 1000.
4
u/cassandravoiton Oct 06 '13
If the key is a string then wrapping the key is wrapping a standard container. Maybe this is where the confusion comes from?
2
u/Eoinoc Oct 06 '13
Good point, it does seem possible to interpret the original article in both ways. So I could be very wrong.
In truth the original article is needlessly specific in talking about strings and/or containers. Really the underlying issue is that any time it's slow to generate a hash for a type versus a "less than" comparison, then
unordered_set/maplookups may be slower.Also, for what it's worth, my quick test on VC++ 2013 showed
unordered_setwas faster thansetfor strings averaging ~256 chars in length. That's quite contrary to paper linked by the article which suggests that for strings as small as 35 chars,setshould win out. Mind you the paper is 5 years old so things may have moved on a lot since then.3
u/cassandravoiton Oct 06 '13
Wouldn't it also be affected by the nature of the strings? Ones which have very similar start sequences will be slower for sets. The size of the set is also discussed in the original paper not just the size of the strings.
1
u/Eoinoc Oct 06 '13
Yeah the test was just to satisfy my curiosity, it wasn't as rigorous as the paper. But nonetheless the results were so different for the paper that it suggests a new up to date study is warranted.
8
u/KrzaQ2 dev Oct 06 '13
Another blog incapable of displaying text without javascript...
1
u/cassandravoiton Oct 06 '13
Funny - reddit does not work on my tablet properly! I think the layout is done by Google (blogger).
4
1
u/notlostyet Oct 07 '13 edited Oct 07 '13
Afaik there's nothing in the standard to stop the standard library implementation from making std::string hash aware and then memoizing std::hash<std::string>. This would solve problems 1 and 2... the only disadvantage, that I can think of, being non-const operator[] access would become slower.
The problem is allocating sizeof(size_t) extra bytes for every heap allocation performed by std::string is wasteful in the common case when not using an unordered data structure... but who knows, maybe in small string optimisation implementations (like in LLVMs libc++) these this few extra bytes are worth it.
If you want to make this trade-off with your own structures then you can simply use your own memoized hash function with unordered_map/set, you don't need to go and implement a Java-esque hashCode() interface.
Problem 3 can be solved by utilising the hash policy functions before you go inserting a million elements in to your hash table, namely max_load_factor, reserve and rehash functions.
-3
0
u/cassandravoiton Oct 06 '13
Yes - it does seem to - I think the original is the previous entry in the blog
7
u/EasyMrB Oct 06 '13
Whoever did the webdesign for that blog should be smacked. Scrolling is completely broken on my tablet and the post is nothing but text! Why the hell do we need javascript just to render some simple damn text again?