\chapter{Hashing structure writ large}
\hypertarget{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large}{}\label{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large}\index{Hashing structure writ large@{Hashing structure writ large}}
\label{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md51}%
\Hypertarget{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md51}%
 In Lisp, there\textquotesingle{}s an expectation that any object may act as a key in a hash table. What that means, in practice, is that if a list


\begin{DoxyCode}{0}
\DoxyCodeLine{'(foo\ bar\ ban)}

\end{DoxyCode}


is a key, and we pass a list


\begin{DoxyCode}{0}
\DoxyCodeLine{'(foo\ bar\ ban)}

\end{DoxyCode}


as a query, we ought to get the value to which the first instance of `'(foo bar ban)\textquotesingle{} was bound, even if the two instances are not the same instance. Which means we have to compute the hash value by exploring the whole structure, no matter how deep and convoluted it may be.\hypertarget{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md52}{}\doxysection{\texorpdfstring{The cost of this}{The cost of this}}\label{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md52}
The cost of this, in the \href{https://git.journeyman.cc/simon/post-scarcity}{\texttt{ post scarcity software environment}} is potentially enormous\+: is potentially a blocker which could bring the whole project down. The post-\/scarcity architecture as currently conceived allows for 2\textsuperscript{64} cons cells. Take the most obvious example of a wholly linear, non-\/branching structure, a string\+: it would be perverse, but possible to have a single string which occupied the entire address space.

But to be less perverse, the text of English Wictionary is 3.\+9 billion words, so it\textquotesingle{}s reasonable to assume that the text of the Encyclopaedia Brittanica is of the same order of magnitude. There are, on average, 4.\+7 characters in an English word, plus slightly more than one for punctuation, so we can round that up to six. So the string required to store the text of the Encyclopedia Brittanica would be approximately 24 billion characters long; and storing that in a string would not, in the context of the post-\/scarcity software environment, be utterly perverse.

But the cost of hashing it would be enormous\+: would be even greater in the hypercube architecture of the proposed \href{Post-scarcity-hardware.html}{\texttt{ post scarcity hardware}} than on one with a \href{https://en.wikipedia.org/wiki/Von_Neumann_architecture}{\texttt{ von Neumann architecture}}, since we cannot even split the string into chunks to take advantage of parallelism (since the string will almost certainly be non-\/contiguous in memory), so the hash has to be computed in a single thread, and also since all the cells of the string -- inevitably the majority -- not native to the memory map of the processor node calculating the hash have to be fetched hop-\/de-\/hop across the lattice of the hypercube, with each hop costing a minimum of six clock ticks (about 260 clock ticks over a serial link).

A fully populated post scarcity hardware implementation -- i.\+e. one large enough to contain such perverse strings -- would be a hypercube of side 1625, which means the longest path between any pair of nodes is 812 hops, which means the average path is 406 hops. But, one in every four hops, if the machine is built as I currently conceive it, is a serial link. So the average cost of fetching a datum from an arbitrary node is (6 x 3 x (406 / 4)) + (260 x (406 / 4)), which is to say, roughly, 28,700 clock ticks.

So to fetch the whole string takes about 30,000 x 24,000,000,000, or 720,000,000,000,000 clock ticks, which, assuming a 3GHz clock, is about quarter of a million seconds, or three days.

To make matters worse, suppose we now stored the hash value in a hash table as the value of that string so as to avoid having to compute it again, we could then not ever garbage collect the string, since that hash table would contain a pointer to it.

So clearly, hashing structures when required in the post scarcity software environment just will not work.\hypertarget{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md53}{}\doxysection{\texorpdfstring{Finding a solution}{Finding a solution}}\label{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md53}
Necessarily, most data structures in the post scarcity software environment must be immutable, because most of the time we will be operating on copies of them in compute nodes remote from the node to which they are native. Thus, we can compute a hash value when the structure is first created, and cache it on the structure itself.

This option first occurred to me in the special case of string-\/like-\/things (strings, atoms, keywords). Because a wide (32 bit UTF) character sits in 32 bits of memory, and a string cell has to sit in the memory footprint of a cons cell, my string payload had 32 unused bits\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{/**}}
\DoxyCodeLine{\textcolor{comment}{\ *\ payload\ of\ a\ string\ cell.\ At\ least\ at\ first,\ only\ one\ UTF\ character\ will}}
\DoxyCodeLine{\textcolor{comment}{\ *\ be\ stored\ in\ each\ cell.\ The\ doctrine\ that\ 'a\ symbol\ is\ just\ a\ string'}}
\DoxyCodeLine{\textcolor{comment}{\ *\ didn't\ work;\ however,\ the\ payload\ of\ a\ symbol\ cell\ is\ identical\ to\ the}}
\DoxyCodeLine{\textcolor{comment}{\ *\ payload\ of\ a\ string\ cell.}}
\DoxyCodeLine{\textcolor{comment}{\ */}}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\mbox{\hyperlink{consspaceobject_8h_structstring__payload}{string\_payload}}\ \{}
\DoxyCodeLine{\ \ \ \ wint\_t\ \mbox{\hyperlink{consspaceobject_8h_ad559bf66587451a4d12f9e974579204e}{character}};\ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ the\ actual\ character\ stored\ in\ this\ cell\ */}}
\DoxyCodeLine{\ \ \ \ uint32\_t\ padding;\ \ \ \ \ \ \ \ \ \ \ \textcolor{comment}{/*\ unused\ padding\ to\ word-\/align\ the\ cdr\ */}}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{struct\ }\mbox{\hyperlink{consspaceobject_8h_structcons__pointer}{cons\_pointer}}\ \mbox{\hyperlink{consspaceobject_8h_a4f7e23046a1d60b3497678af0f94a90c}{cdr}};}
\DoxyCodeLine{\};}

\end{DoxyCode}


So it was straightforward to reassign that unused {\ttfamily padding} as a cache for the hash value\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{/**}}
\DoxyCodeLine{\textcolor{comment}{\ *\ payload\ of\ a\ string\ cell.\ At\ least\ at\ first,\ only\ one\ UTF\ character\ will}}
\DoxyCodeLine{\textcolor{comment}{\ *\ be\ stored\ in\ each\ cell.\ The\ doctrine\ that\ 'a\ symbol\ is\ just\ a\ string'}}
\DoxyCodeLine{\textcolor{comment}{\ *\ didn't\ work;\ however,\ the\ payload\ of\ a\ symbol\ or\ keyword\ cell\ is\ identical}}
\DoxyCodeLine{\textcolor{comment}{\ *\ to\ the\ payload\ of\ a\ string\ cell,\ except\ that\ a\ keyword\ may\ store\ a\ hash}}
\DoxyCodeLine{\textcolor{comment}{\ *\ of\ its\ own\ value\ in\ the\ padding.}}
\DoxyCodeLine{\textcolor{comment}{\ */}}
\DoxyCodeLine{\textcolor{keyword}{struct\ }\mbox{\hyperlink{consspaceobject_8h_structstring__payload}{string\_payload}}\ \{\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ /**\ the\ actual\ character\ stored\ in\ this\ cell\ */}}
\DoxyCodeLine{\ \ \ \ wint\_t\ \mbox{\hyperlink{consspaceobject_8h_ad559bf66587451a4d12f9e974579204e}{character}};\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ /**\ a\ hash\ of\ the\ string\ value,\ computed\ at\ store\ time.\ */}}
\DoxyCodeLine{\ \ \ \ uint32\_t\ \mbox{\hyperlink{consspaceobject_8h_a11ecb029164e055f28f4123ce3748862}{hash}};\textcolor{comment}{}}
\DoxyCodeLine{\textcolor{comment}{\ \ \ \ /**\ the\ remainder\ of\ the\ string\ following\ this\ character.\ */}}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{struct\ }\mbox{\hyperlink{consspaceobject_8h_structcons__pointer}{cons\_pointer}}\ \mbox{\hyperlink{consspaceobject_8h_a4f7e23046a1d60b3497678af0f94a90c}{cdr}};}
\DoxyCodeLine{\};}

\end{DoxyCode}


But new items can be consed onto the front of lists, and that means, in practice, new characters can be consed onto the front of strings, too. What this means is that


\begin{DoxyCode}{0}
\DoxyCodeLine{(hash\ "{}the\ quick\ brown\ fox\ jumped\ over\ the\ lazy\ dog"{})}

\end{DoxyCode}


and


\begin{DoxyCode}{0}
\DoxyCodeLine{(hash\ (append\ "{}the"{}\ "{}quick\ brown\ fox\ jumped\ over\ the\ lazy\ dog"{}))}

\end{DoxyCode}


must return the same value. But, obviously, we don\textquotesingle{}t want to have to walk the whole structure to compute the hash, because we cannot know in principle, when passed a string, whether or not it is extremely long.

The answer that occurred to me, for strings, is as follows\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{\textcolor{comment}{/**}}
\DoxyCodeLine{\textcolor{comment}{\ *\ Return\ a\ hash\ value\ for\ this\ string\ like\ thing.}}
\DoxyCodeLine{\textcolor{comment}{\ *\ }}
\DoxyCodeLine{\textcolor{comment}{\ *\ What's\ important\ here\ is\ that\ two\ strings\ with\ the\ same\ characters\ in\ the}}
\DoxyCodeLine{\textcolor{comment}{\ *\ same\ order\ should\ have\ the\ same\ hash\ value,\ even\ if\ one\ was\ created\ using}}
\DoxyCodeLine{\textcolor{comment}{\ *\ \`{}"{}foobar"{}`\ and\ the\ other\ by\ \`{}(append\ "{}foo"{}\ "{}bar"{})`.\ I\ *think*\ this\ function\ }}
\DoxyCodeLine{\textcolor{comment}{\ *\ has\ that\ property.\ I\ doubt\ that\ it's\ the\ most\ efficient\ hash\ function\ to\ }}
\DoxyCodeLine{\textcolor{comment}{\ *\ have\ that\ property.}}
\DoxyCodeLine{\textcolor{comment}{\ *\ }}
\DoxyCodeLine{\textcolor{comment}{\ *\ returns\ 0\ for\ things\ which\ are\ not\ string\ like.}}
\DoxyCodeLine{\textcolor{comment}{\ */}}
\DoxyCodeLine{uint32\_t\ \mbox{\hyperlink{consspaceobject_8c_a10cecbd3a3077f601359388153677000}{calculate\_hash}}(wint\_t\ c,\ \textcolor{keyword}{struct}\ \mbox{\hyperlink{consspaceobject_8h_structcons__pointer}{cons\_pointer}}\ ptr)}
\DoxyCodeLine{\{}
\DoxyCodeLine{\ \ \ \ \textcolor{keyword}{struct\ }\mbox{\hyperlink{consspaceobject_8h_structcons__space__object}{cons\_space\_object}}\ *cell\ =\ \&\mbox{\hyperlink{consspaceobject_8h_af31e1da9438d7ae4410864ecc901b131}{pointer2cell}}(ptr);}
\DoxyCodeLine{\ \ \ \ uint32\_t\ result\ =\ 0;}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{switch}\ (cell-\/>\mbox{\hyperlink{consspaceobject_8h_a21cb645ce4ae20308447dcd59534bf38}{tag}}.value)}
\DoxyCodeLine{\ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{case}\ \mbox{\hyperlink{consspaceobject_8h_a027cdb41b87676d9d2fb55dd058c0078}{KEYTV}}:}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{case}\ \mbox{\hyperlink{consspaceobject_8h_a3a4310cf1909a3b9c167a945ef71d3e1}{STRINGTV}}:}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{case}\ \mbox{\hyperlink{consspaceobject_8h_a1cef10aef0e023839dbcfe6d5c305393}{SYMBOLTV}}:}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{if}\ (\mbox{\hyperlink{consspaceobject_8h_a758a42bfac22caaf66e29c666c91702d}{nilp}}(ptr))}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ result\ =\ (uint32\_t)c;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \textcolor{keywordflow}{else}}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \{}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ result\ =\ ((uint32\_t)c\ *}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ cell-\/>\mbox{\hyperlink{consspaceobject_8h_a259b3afe36776f8ba8498fdf95904f25}{payload}}.string.hash)\ \&}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ 0xffffffff;}
\DoxyCodeLine{\ \ \ \ \ \ \ \ \}}
\DoxyCodeLine{\ \ \ \ \}}
\DoxyCodeLine{}
\DoxyCodeLine{\ \ \ \ \textcolor{keywordflow}{return}\ result;}
\DoxyCodeLine{\}}

\end{DoxyCode}


That is to say, the hash value is the least significant 32 bits of the product of multiplying the hash of the tail of the string by the character code of the character added. This means we have a very small fixed cost -- one 32 bit integer multiplication -- every time a character is added to a string, rather than an enormous cost every time the hash value of a string is required. I {\itshape believe} this is a good trade-\/off, just as I believe using reference counting rather than mark-\/and-\/sweep for garbage collection is a good trade-\/off\+: it\textquotesingle{}s better, in my opinion, to have steady is slightly slower than optimal performance from the machine than for it to intermittently lock up for no apparent reason.

In any case, what this means is that getting the hash value -- by the standard hashing function -- of a string cell is absurdly cheap\+: it\textquotesingle{}s just a bit mask on the cell.

Is this a good hash function? Probably not. A hash function should ideally distribute arbitrary values pretty evenly between hash buckets, and this one is probably biased. Perhaps at some stage someone will propose a better one. But in practice, I believe that this will do for now. It is, after all, extremely cheap.\hypertarget{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md54}{}\doxysection{\texorpdfstring{To generalise, or not to generalise?}{To generalise, or not to generalise?}}\label{md_workspace_2post-scarcity_2docs_2_hashing-structure-writ-large_autotoc_md54}
Certainly in Clojure practice, keys in hash maps are almost always \textquotesingle{}keywords\textquotesingle{}, a particular variety of string-\/like-\/thing. Nevertheless, Clojure, like Common Lisp (and many, perhaps all, other lisps) allows, in principal, that any value can be used as a key in a hash map.

The hack outlined above of using 32 bits of previously unused space in the string payload works because there were 32 unused bits in the string payload. While a hash function with similar properties can be imagined for cons cells, there are not currently any unused bits in a cons payload. To add a hash value would require adding more bits to every cons space object.

Our current cons space object is 256 bits\+:


\begin{DoxyCode}{0}
\DoxyCodeLine{+-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+}
\DoxyCodeLine{|\ header\ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \ \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\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/-\/+}

\end{DoxyCode}


This means that cells are word aligned in a 64 bit address space, and a perfect fit for a reasonably predictable future general purpose processor architecture with a 256 bit address bus. If we\textquotesingle{}re now going to add more bits for hash cache, it would be perverse to add less than 64 bits because we\textquotesingle{}d lose word alignment; and it\textquotesingle{}s not particularly likely, I think, that we\textquotesingle{}ll see future processors with a 320 bit address bus.

It would be possible to steal some bits out of either the tag, the count, or both -- 32 bits for the tag, in particular, is absurdly generous, but it does help greatly in debugging.

For now, I\textquotesingle{}m of a mind to cache hash values only for string-\/like-\/things, and, if users want to use other types of values as keys in hash maps, use access-\/time hashing functions. After all, this works well enough in Common Lisp. 