How the Go runtime implements maps efficiently without generics
The dict&#x2F;hash implementation in K (since 1993 or so ..) is much simpler, cleaner, takes much less memory and often faster - though speed depends on the actual use case more than anything.<p>The idea is that instead of the hash map being a list of &lt;key,value&gt; items indexed by key, there&#x27;s a vector of keys, a vector of values (with matching indices), and a search index (usually a hash, but could be a tree) that maps a key to the index; note that the search index does not need a copy of the key even if it is inexact because one can always refer to the keys vector.<p>The cons:<p>* You can&#x27;t pass a single &lt;key,value&gt; object without building one<p>* You may incur twice or thrice cache loads (one for index, one for key, and if correct, one for value) whereas on a conventional &lt;key, value&gt; list, they are likely to be on the same cache line.<p>* (edit: added based on aidenn0 comment, see discussion below): Removal is generally O(n) unless you are willing to give up one of the pros; but even if you give some up, it&#x27;s still usually more featureful than commonly used implementations.<p>The pros:<p>* Insertion order is preserved - OrderedDict for free!<p>* Getting the list of keys, or the list of values, e.g. python&#x27;s keys() and values() methods, are O(1) and allocation free<p>* Applying a function to each item needs to allocate a new value vector, but can reuse the key vector -- and if the function doesn&#x27;t need the key, you don&#x27;t actually have to fetch it - e.g., to normalize a map so values sum to 1, you can do:<p><pre><code>    whole = sum(mymap.values())
    normalized = apply_values(mymap, lambda x: x&#x2F;whole) 
</code></pre>
And get a normalized map with the same keys and literally the minimal number of allocations and operations possible for a concrete datatype<p>* Specialization is by the key type, regardless of the value type - which means that a few efficient implementations (for ints, longs, floats, strings, symbols) manage to often provide C++ speeds for an interpreted language.<p>* Memory packing is about as tight as it can be. Your key is a double, and your value is one byte? You pay exactly 9 bytes per record + index size (which, e.g. for a 30,000 record map, takes two bytes per hash slot, or ~128KB for a 64K slot table). This is true whether you are running on an 16-bit, 32-bit, 64-bit or 128-bit processor.<p>* It is straightforward and natural to implement with almost no pointers involved, meaning that it is trivial to share memory among processes (at least for read only accesses) and to use with memory-mapped storage -- to the point that you can have an 8GB dictionary which is instantly mmap()&#x2F;MapViewOfFile()d into existence, and is no different in any way than any other one - you pay disk access for the records you use, but enjoy OS cache&#x2F;page management, with cache shared between processes and between subsequent runs -- no need to special case this dictionary just because it&#x27;s big.<p>The Python 3.6 implementation switched from a hash-indexed &lt;hash,key,value&gt; vector to a &lt;hash -&gt; order&gt; and ordered &lt;key,value&gt; vector, and saw speed improvements and memory use reductions. Due to Python&#x27;s legacy and structure, they could not have gone farther on the K way, except for CPU cache effects.<p>Having used APL and K for over 15 years now, I find it weird seeing the same Smalltalk-inspired hash table structure come up in every single new runtime and language implementation.