{"id":9085,"date":"2026-08-21T09:32:40","date_gmt":"2026-08-21T09:32:40","guid":{"rendered":"https:\/\/www.dyalog.com\/blog\/?p=9085"},"modified":"2026-08-21T09:45:37","modified_gmt":"2026-08-21T09:45:37","slug":"hashing-it-out-lookup-performance-in-dyalog-apl","status":"publish","type":"post","link":"https:\/\/www.dyalog.com\/blog\/2026\/08\/hashing-it-out-lookup-performance-in-dyalog-apl\/","title":{"rendered":"Hashing It Out: Lookup Performance in Dyalog APL"},"content":{"rendered":"<p class=\"code-line\" dir=\"auto\">Sometimes it seems that I have the attention span of a cocker spaniel puppy. I start to look at one thing, and then something pops up to distract me and I follow that until another distraction or tangent comes along and then&#8230;. well, you get the point. What started out as one blog post has morphed a few times into what you&#8217;re about to read!<\/p>\n<h2 id=\"background-and-original-goal\" class=\"code-line\">Background and Original Goal<\/h2>\n<p class=\"code-line\" dir=\"auto\">In a former life, I wrote an APL-based full-text search engine for searching legislative and regulatory texts. At the time, its speed rivalled any of the online search engines. In that governmental context, words like &#8220;the&#8221; and &#8220;of&#8221; have significance, for example, the &#8220;the&#8221; in &#8220;the white house&#8221; is significant, as are the &#8220;of&#8221; and &#8220;the&#8221; in &#8220;speaker of the house&#8221;. Most search engines would ignore those high-frequency &#8220;noise&#8221; words \u2013 mine couldn&#8217;t. My goal was to be able to search for such phrases without having to examine the data for the high-frequency words. I found this to be an interesting problem at the time (in the 1990s) and subsequently included it 25 years later as the\u00a0<a href=\"https:\/\/www.dyalog.com\/uploads\/files\/student_competition\/2015_problems_phase2.pdf\" target=\"_blank\" >2015 APL Problem Solving Competition Phase 2 Applications Problem 2<\/a>. My original idea for this blog post was to examine approaches to that problem.<\/p>\n<p class=\"code-line\" dir=\"auto\">The core of the problem was straightforward \u2013 you have a list of words (vector of character vectors) and a corresponding vector of frequencies for each word. We&#8217;ll call these two vectors\u00a0<code>Words<\/code>\u00a0and\u00a0<code>Freqs<\/code>. For those interested, the source data I used for my recent effort came from a repository that tracks\u00a0<a href=\"https:\/\/github.com\/IlyaSemenov\/wikipedia-word-frequency\/blob\/master\/results\/enwiki-2023-04-13.txt\" target=\"_blank\" rel=\"noopener\">Wikipedia word frequencies<\/a>. The task is to, for a given phrase, break the phrase up into individual words (we&#8217;ll call this\u00a0<code>words<\/code>), look up\u00a0<code>words<\/code>\u00a0in\u00a0<code>Words<\/code>, and use the indices to extract their frequencies from\u00a0<code>Freqs<\/code>. This is basic APL, the sort of thing we do all the time:\u00a0<code>Freqs[Words \u2373 words]<\/code>. If we want to consider the case where a word in\u00a0<code>words<\/code>\u00a0isn&#8217;t found in\u00a0<code>Words<\/code>, we can append\u00a0<code>0<\/code>\u00a0to\u00a0<code>Freqs<\/code>.<\/p>\n<p class=\"code-line\" dir=\"auto\">The source for\u00a0<code>Words<\/code>\u00a0and\u00a0<code>Freqs<\/code>\u00a0has 2,765,377 words. To emulate my original work done in the 1990s when we were using only ASCII characters, I decided to remove words containing non-ASCII characters. As a result, we&#8217;re left with 1,905,526 words.<\/p>\n<h2 id=\"distraction-1\" class=\"code-line\">Tangent #1 \u2013 \u2395CSV<\/h2>\n<p class=\"code-line\" dir=\"auto\"><span style=\"font-weight: 300;\">If you&#8217;re interested in obtaining <code>Words<\/code> and <code>Freqs<\/code> yourself, you can download the <\/span><a style=\"font-weight: 300;\" href=\"https:\/\/github.com\/IlyaSemenov\/wikipedia-word-frequency\/raw\/refs\/heads\/master\/results\/enwiki-2023-04-13.txt\">raw data file<\/a><span style=\"font-weight: 300;\"> and use <\/span><code style=\"font-style: inherit;\">\u2395CSV<\/code><span style=\"font-weight: 300;\"> and a bit of code:<\/span><\/p>\n<pre><code class=\"code-line\">      (Words Freqs)\u2190\u2395CSV\u2360('Separator' ' ')('Invert' 2)\u22a2'{your-path-here}\/enwiki-2023-04-13.txt' ''(1 2)\r\n      (Words Freqs)\/\u2368\u2190\u2282Words(\u2227\/\u220a)\u00a8\u2282\u2395C\u2395A \u235d limit words to ASCII alphabetics<\/code><\/pre>\n<p><code>\u2395CSV<\/code> can do some amazing things.\u00a0Ad\u00e1m Brudzewsky made a very interesting video on <a href=\"https:\/\/www.youtube.com\/watch?v=AHoiROI15BA\" target=\"_blank\" rel=\"noopener\">Parsing content from text files using <code>\u2395CSV<\/code><\/a>.<\/p>\n<p>From that list, the five most and least frequent words are:<\/p>\n<pre><code class=\"code-line\">      \u2349\u21915(\u2191,-\u235b\u2191)\u00a8Words Freqs \u235d look mom! I used \u235b (behind)!\r\n the          186631452\r\n of            88349543\r\n in            76718795\r\n and           76039670\r\n a             54631147\r\n byodoinji            3\r\n houryuuji            3\r\n groendorpen          3\r\n witotoanas           3\r\n kimely               3\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Interestingly, it seems a word must occur at least 3 times in Wikipedia to make it into this list. Okay, enough background information&#8230;<\/p>\n<p id=\"distraction-1\" class=\"code-line\"><strong>Distraction #1<\/strong><\/p>\n<p class=\"code-line\" dir=\"auto\">In my testing, I found that, when\u00a0<code>words<\/code>\u00a0exceeded 3 elements, things slowed down a lot. For example:<\/p>\n<pre><code class=\"code-line\">      ]RunTime -c \"Words\u2373'the' 'big' 'dog'\" \"Words\u2373'the' 'big' 'fuzzy' 'dog'\"\r\n  Words\u2373'the' 'big' 'dog'         \u2192 1.3E\u00af4 |       0%                                          \r\n* Words\u2373'the' 'big' 'fuzzy' 'dog' \u2192 2.5E\u00af1 | +202400% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">One would expect the performance to degrade linearly with the number of words. This appears to be true until you reach 4 elements in the right argument. Let&#8217;s create some test cases using lists of 1 to 6 words; since &#8216;the&#8217; is the first word in the list, these searches should be as fast as possible.<\/p>\n<pre><code class=\"code-line\">      (p1 p2 p3 p4 p5 p6)\u21901 2 3 4 5 6\u2374\u00a8\u2282'the'\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Now let&#8217;s see what happens when we search for lists of 1 to 4 words:<\/p>\n<pre><code class=\"code-line\">      ]RunTime -c Words\u2373p1 Words\u2373p2 Words\u2373p3 Words\u2373p4\r\n  Words\u2373p1 \u2192 2.1E\u00af2 |     0% \u2395\u2395\u2395                                      \r\n* Words\u2373p2 \u2192 4.2E\u00af2 |  +102% \u2395\u2395\u2395\u2395\u2395\u2395                                   \r\n* Words\u2373p3 \u2192 6.2E\u00af2 |  +204% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395                               \r\n* Words\u2373p4 \u2192 2.6E\u00af1 | +1147% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">It looks linear for lengths 1 to 3, but jumps at length 4. If we look at lengths 4 to 6, we see a linear pattern, albeit considerably slower.<\/p>\n<pre><code class=\"code-line\">      ]RunTime -c Words\u2373p4 Words\u2373p5 Words\u2373p6\r\n  Words\u2373p4 \u2192 2.5E\u00af1 |  0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395   \r\n* Words\u2373p5 \u2192 2.5E\u00af1 | +1% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395  \r\n* Words\u2373p6 \u2192 2.6E\u00af1 | +4% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">What&#8217;s going on?!\u00a0After a bit of research (I asked Morten!), I found that Dyalog is hashing <code>Words<\/code> extemporaneously when <code>words<\/code>\u00a0exceeds 3 elements. Because the hash table is not retained,\u00a0<code>Words<\/code>\u00a0is hashed for each iteration in\u00a0<code>]RunTime<\/code>, causing the slowdown.<\/p>\n<p class=\"code-line\" dir=\"auto\">Since creating the hash table is a relatively expensive operation, it only makes sense to do if there is a mechanism to retain the table from which subsequent searches can benefit or the right argument is long enough to justify the upfront cost.<\/p>\n<h2 id=\"tangent-1---a-brief-history-of-hashing-in-dyalog-apl\" class=\"code-line\">Tangent #2 \u2013 A Brief History of Hashing in Dyalog APL<\/h2>\n<p class=\"code-line\" dir=\"auto\">First, a bit of terminology \u2013 search functions have a &#8220;principal&#8221; argument (the array being searched) and a &#8220;subject&#8221; argument (the items being searched for). In the example above,\u00a0<code>Words<\/code>\u00a0is the principal argument and\u00a0<code>words<\/code>\u00a0is the subject argument.<\/p>\n<p class=\"code-line\" dir=\"auto\">Hashing, in one form or another, has existed in Dyalog APL from its earliest days; it&#8217;s an industry standard technique. The interpreter decides, based on a number of factors, whether to hash an array extemporaneously or not. This hashing is an internal process and beyond the user&#8217;s control.<\/p>\n<p class=\"code-line\" dir=\"auto\">Dyalog v10.0, released in 2003, gave users the ability to bind a search function like\u00a0<code>\u2373<\/code>\u00a0(<em>index of<\/em>),\u00a0<code>\u220a<\/code>\u00a0(<em>membership<\/em>),\u00a0<code>\u2229<\/code>\u00a0(<em>intersection<\/em>),\u00a0<code>\u222a<\/code>\u00a0(<em>union<\/em>), or\u00a0<code>~<\/code>\u00a0(<em>without<\/em>) to its principal argument, resulting in a derived function that will, on its first invocation, create and retain a hash table. The disadvantage of this is that the hash table is used exclusively for the bound search function. If you needed to use more than one search function you would bind them individually and each would have its own hash table.<\/p>\n<p class=\"code-line\" dir=\"auto\">Dyalog v15.0, released in 2016, introduced\u00a0<code>1500\u2336<\/code>\u00a0(hash array). This allows the user to mark an array for hashing and, after the first search operation creates the hash table, the hash table is available for all applicable search functions.\u00a0<code>array\u21901500\u2336array<\/code>\u00a0returns a copy of\u00a0<code>array<\/code>\u00a0that is marked for hashing. The hash is created on the first invocation of a search function on\u00a0<code>array<\/code>.\u00a0<code>1(1500\u2336)array<\/code>\u00a0returns\u00a0<code>0<\/code>\u00a0if\u00a0<code>array<\/code>\u00a0has not been marked for hashing,\u00a0<code>1<\/code>\u00a0if\u00a0<code>array<\/code>\u00a0has been marked for hashing but the hash has not been created yet, and\u00a0<code>2<\/code>\u00a0if\u00a0<code>array<\/code>\u00a0has a hash table. For example:<\/p>\n<pre><code class=\"code-line\">      1 (1500\u2336) Words \u235d not marked yet\r\n0\r\n      Words\u21901500\u2336 Words \u235d mark for hashing\r\n      1 (1500\u2336) Words\r\n1\r\n      Words\u2373\u2282'the' \u235d perform a search (this is a bit slow)\r\n      1 (1500\u2336) Words \u235d Words now has a hash table\r\n2\r\n      Words,\u2190\u2282'thermoflocker' \u235d add a new word to the tail end\r\n      1 (1500\u2336) Words \u235d still hashed\r\n2\r\n      Words\u2190 1 \u2193 Words \u235d drop something from the front end\r\n      1 (1500\u2336) Words \u235d no longer hashed nor marked for hashing\r\n0\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">The size of the hash table can impact performance. In general, larger hash tables result in faster lookups. Dyalog v19.0, released in 2024, introduced\u00a0<code>8468\u2336<\/code> (hash table size), which allowed the user to adjust the size factor of created hash tables. Its purpose was to allow users to evaluate the potential side effects of a proposed larger hash table size.<\/p>\n<p class=\"code-line\" dir=\"auto\">Hash tables in Dyalog APL work best with (mostly) static principal arrays. In general, modifying the array invalidates the hash and the array will need to be rehashed. However, there are three forms of modified assignment that will preserve and efficiently update the hash \u2013 notice that they all involve the tail end of the principal array.<\/p>\n<pre><code class=\"code-line\">      R,\u2190Y    \u235d only for scalar or vector R\r\n      R\u236a\u2190Y\r\n      R\u2193\u2368\u2190Y   \u235d only for negative singleton Y\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Dyalog v20.0, released in 2025, implemented hash tables that are 8 times (<code>2*3<\/code>) larger than previously and also removed\u00a0<code>8468\u2336<\/code>. More on this later&#8230;<\/p>\n<p id=\"why-hash\" class=\"code-line\"><strong>Why Hash?<\/strong><\/p>\n<p class=\"code-line\" dir=\"auto\">The simplistic approach to search functions like\u00a0<code>\u2373<\/code>\u00a0and\u00a0<code>\u220a<\/code>\u00a0is to perform a linear search, starting at the first element of the principal argument and iterating through its elements until you find a match. Obviously, elements that occur near the front will be found faster. In a worst case, searching for an element that&#8217;s not in the principal argument will have to search its entirety. Doing a linear search on the first word in\u00a0<code>Words<\/code>\u00a0and for a word not found in\u00a0<code>Words<\/code>\u00a0demonstrates this:<\/p>\n<pre><code class=\"code-line\">      ]RunTime -c \"Words\u2373\u2282'the'\" \"Words\u2373\u2282'thermoflocker'\"\r\n  Words\u2373\u2282'the'           \u2192 0.0E0  |    -100%                                          \r\n* Words\u2373\u2282'thermoflocker' \u2192 2.0E\u00af2 | +250200% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Hashing smooths out this variability and makes all lookups relatively similar in performance, not to mention considerably faster. Doing the same search on\u00a0<code>aWords<\/code>, a hashed version of\u00a0<code>Words<\/code>, shows this.<\/p>\n<pre><code class=\"code-line\">      ]RunTime -c \"aWords\u2373\u2282'the'\" \"aWords\u2373\u2282'thermoflocker'\"\r\n  aWords\u2373\u2282'the'           \u2192 2.1E\u00af7 |   0% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395     \r\n* aWords\u2373\u2282'thermoflocker' \u2192 2.4E\u00af7 | +11% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p><strong>&#8220;I Feel the Need for Speed&#8221; (Top Gun, 1986)<\/strong><\/p>\n<p class=\"code-line\" dir=\"auto\">Let&#8217;s revisit the issue that led us down the hashing path. We&#8217;ll create a hashed copy of\u00a0<code>Words<\/code>, but leave\u00a0<code>Words<\/code>\u00a0alone so that we can compare performance. We&#8217;ll also create a function bound with\u00a0<code>\u2373<\/code>.<\/p>\n<pre><code class=\"code-line\">      hWords\u21901500\u2336Words \u235d hWords is marked for hashing\r\n      hWords\u2373\u2282'the'      \u235d first search creates the hash table\r\n1\r\n      fWords\u2190Words\u2218\u2373      \u235d also create the derived function  \r\n      fWords \u2282'the'      \u235d first search creates the hash table\r\n1\r\n      p\u2190'the' 'big' 'fuzzy' 'dog'\r\n      ]RunTime -c \"hWords \u2373 p\" \"fWords p\" \"Words \u2373 p\"\r\n                                                                        \r\n  hWords \u2373 p \u2192 0.0E0  |    -100%                                          \r\n  fWords p \u2192 0.0E0    |    -100%                                          \r\n  Words \u2373 p  \u2192 3.5E\u00af1 | +556800% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Once we have a retained hash table, <strong>all<\/strong> lookups, no matter the length of\u00a0<code>words<\/code>, will be faster.<\/p>\n<h2 id=\"tangent-2---when-does-a-principal-argument-get-hashed\" class=\"code-line\">Tangent #3 \u2013 When Does a Principal Argument Get Hashed?<\/h2>\n<p class=\"code-line\" dir=\"auto\">The short answer is: it depends on a number of factors, such as:<\/p>\n<ul class=\"code-line\">\n<li class=\"code-line\">whether a search function like\u00a0<code>\u2373<\/code>\u00a0or\u00a0<code>\u220a<\/code>\u00a0is used<\/li>\n<li class=\"code-line\">the sizes of the principal and subject arrays<\/li>\n<li class=\"code-line\">the datatypes of the arrays \u2013 character, integer, floating point, complex<\/li>\n<li class=\"code-line\">are the arrays simple (depth 0 or 1) or not?<\/li>\n<li class=\"code-line\">the amount of workspace available \u2013 can it fit the hash table?<\/li>\n<\/ul>\n<p class=\"code-line\" dir=\"auto\">In short, Dyalog APL hashes an array when it &#8220;thinks&#8221; doing so will help. However, you know your application and its data better than the interpreter does. In my example above, the interpreter can&#8217;t know if my search is a one-time thing or something that will be executed many times.<\/p>\n<p class=\"code-line\" dir=\"auto\">How can you determine where hashing kicks in? What I did was to start with a single element subject argument and increase its length until I saw a &#8220;bump&#8221; in runtime:<\/p>\n<pre><code class=\"code-line\">      iv\u21901e7?2*24  \u235d build 10-million element integer vector with no duplicates\r\n\r\n      \u235d now time lookups for subject arrays of length 1 to 15\r\n      \u2395SE.UCMD 'runtime -c',\u220a' iv\u2373iv['\u2218,\u00a8(\u2355\u00a8\u237315),\u00a8\u2282'\u23745000000]'\r\n  iv\u2373iv[1\u23745000000]  \u2192 1.0E\u00af3 |     0%                                          \r\n* iv\u2373iv[2\u23745000000]  \u2192 2.1E\u00af3 |  +100% \u2395                                        \r\n* iv\u2373iv[3\u23745000000]  \u2192 3.2E\u00af3 |  +206% \u2395                                        \r\n* iv\u2373iv[4\u23745000000]  \u2192 4.3E\u00af3 |  +315% \u2395\u2395                                       \r\n* iv\u2373iv[5\u23745000000]  \u2192 5.3E\u00af3 |  +412% \u2395\u2395                                       \r\n* iv\u2373iv[6\u23745000000]  \u2192 6.2E\u00af3 |  +503% \u2395\u2395\u2395                                      \r\n* iv\u2373iv[7\u23745000000]  \u2192 7.3E\u00af3 |  +609% \u2395\u2395\u2395                                      \r\n* iv\u2373iv[8\u23745000000]  \u2192 8.4E\u00af3 |  +718% \u2395\u2395\u2395\u2395                                     \r\n* iv\u2373iv[9\u23745000000]  \u2192 9.3E\u00af3 |  +800% \u2395\u2395\u2395\u2395                                     \r\n* iv\u2373iv[10\u23745000000] \u2192 1.0E\u00af2 |  +909% \u2395\u2395\u2395\u2395\u2395                                    \r\n* iv\u2373iv[11\u23745000000] \u2192 9.2E\u00af2 | +8784% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n* iv\u2373iv[12\u23745000000] \u2192 9.1E\u00af2 | +8757% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n* iv\u2373iv[13\u23745000000] \u2192 9.2E\u00af2 | +8818% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n* iv\u2373iv[14\u23745000000] \u2192 9.2E\u00af2 | +8821% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n* iv\u2373iv[15\u23745000000] \u2192 9.2E\u00af2 | +8806% \u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395\u2395 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Can you spot where hashing kicks in? That&#8217;s right \u2013 when the subject array exceeds 10 elements. But what if my subject array is only ever a single element? Then you need to determine whether the one-time cost of building the hash table is less than the accumulated time of subsequent lookups. Once hashed, all lookups are faster.<\/p>\n<p class=\"code-line\" dir=\"auto\">One caveat about using the\u00a0<code>]RunTime<\/code>\u00a0user command \u2013 the cost of creating the hash table on the first lookup of a hashed array is lost because\u00a0<code>]RunTime<\/code>\u00a0executes each expression once before collecting timing information.\u00a0<code>\u2395PROFILE<\/code>\u00a0provides a more accurate picture:<\/p>\n<pre><code class=\"code-line\">     \u2207 r\u2190test_iv;iv;hiv;_;d;l;t;i\r\n[1]    iv\u219010000000?2*24 \u235d 10,000,000 integers in the range 1-2*24\r\n[2]    hiv\u21901500\u2336iv      \u235d hiv is marked for hashing\r\n[3]    i\u21905000000\u2283iv     \u235d search for the middle element\r\n[4]   l1:\u2395PROFILE\u00a8'stop' 'clear' 'start'\r\n[5]    _\u2190iv\u2373i  \u235d unhashed lookup\r\n[6]    _\u2190hiv\u2373i \u235d first hashed lookup (creates hash table)\r\n[7]    _\u2190hiv\u2373i \u235d subsequent hashed lookup     \r\n[8]   l2:\u2395PROFILE'stop'\r\n[9]    d\u2190\u2395PROFILE'data'\r\n[10]   l\u2190l1+\u2373l2-l1+1 \r\n[11]   r\u2190(\u2193'%',\u2368\u2355\u236a\u230a0.5+100\u00d7\u00af1+t\u00f7\u230a\/t),(\u2395NR\u2283\u2395SI)[1+l],\u23683\u2355\u236at\u2190d[;4]\u233f\u2368d[;2]\u220al\r\n[12]   \u2395PROFILE'clear'\r\n     \u2207\r\n\r\n      test_iv\r\n   1044%   1.047  _\u2190iv\u2373i  \u235d unhashed lookup\r\n 236125% 216.147  _\u2190hiv\u2373i \u235d first hashed lookup (creates hash table) \r\n      0%   0.092  _\u2190hiv\u2373i \u235d subsequent hashed lookup\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">In this case, the cost of creating the hash table is about 200\u00d7 the cost of doing a single lookup. Subsequent hashed lookups are about 10\u00d7 faster. It&#8217;s up to you, and your understanding of your application, to determine whether retaining the hash table is worth it.<\/p>\n<p id=\"tanstaafl-there-aint-no-such-thing-as-a-free-lunch\" class=\"code-line\"><strong>TANSTAAFL (There Ain&#8217;t No Such Thing As A Free Lunch)<\/strong><\/p>\n<p class=\"code-line\" dir=\"auto\">The other cost of using hash tables for faster searching is that hash tables take up space. Remember when I mentioned that the default hash table size in Dyalog v20.0 is 8x larger than previously? The working theory is that larger hash tables ought to result in faster searches, but how much faster and at what cost in space? Unfortunately there isn&#8217;t a direct way to measure the hash table size. You have to look at the differential in\u00a0<code>\u2395WA<\/code>\u00a0before and after creating the hash table:<\/p>\n<pre><code class=\"code-line\">      ]Config MAXWS\r\n MAXWS  1GB\r\n       \r\n      cf \u2395SIZE'Words' \u235d cf is a utility to comma-format numbers\r\n97,547,968\r\n\r\n      cf \u2395WA-\u2368(hWords\u2373\u2282'the')\u22a2(hWords\u21901500\u2336Words)\u22a2\u2395WA\r\n268,435,664\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">In my 1GB workspace, the hash table for\u00a0<code>Words<\/code>\u00a0is 268MB and 2.75\u00d7 the size of the\u00a0<code>Words<\/code>\u00a0itself. In Dyalog v20.0, we removed\u00a0<code>8468\u2336<\/code>\u00a0which would allow you to set a scaling factor for the hash table size; there is an experimental I-Beam currently being developed,\u00a0<code>8470\u23361<\/code>\u00a0which does much the same thing.\u00a0<code>8470\u23361 factor<\/code>, where\u00a0<code>factor<\/code>\u00a0is an integer in the range\u00a0<code>\u00af3<\/code>\u00a0to\u00a0<code>3<\/code>, will increase or decrease the hash table size by\u00a0<code>2*factor<\/code>\u00a0times. When Dyalog v20.0 was initially released, the default value for\u00a0<code>factor<\/code>\u00a0was\u00a0<code>0<\/code>\u00a0but based on feedback it&#8217;s now <code>\u00af3<\/code> Let&#8217;s use this to examine its effect on hash table size. I wrote an ugly little operator,\u00a0<code>set<\/code>, to build hash tables with specified scaling factors and look at the\u00a0<code>\u2395WA<\/code>\u00a0differential after each:<\/p>\n<pre><code class=\"code-line\">      ]Config MAXWS\r\n MAXWS  10GB\r\n      'abcdefg' ('Words'set) \u00af4+\u23737\r\n aWords  \u00af3       33,555,104 \r\n bWords  \u00af2       67,109,536 \r\n cWords  \u00af1      134,218,400 \r\n dWords   0      268,436,128 \r\n eWords   1      536,871,584 \r\n fWords   2    1,073,742,496 \r\n gWords   3    2,147,484,320 \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">As expected, each hash table grows by a factor of 2.<\/p>\n<h2 id=\"tangent-3---mapped-files\" class=\"code-line\">Tangent #4 \u2013 Mapped Files<\/h2>\n<p class=\"code-line\" dir=\"auto\">The\u00a0<code>\u2395MAP<\/code>\u00a0system function associates a mapped file with an array in the workspace. One of the client projects I&#8217;ve worked on involves loading CSV data with 47 million records of 85 fields, totalling about 7.5GB. Each field is stored in a separate\u00a0<code>\u2395MAP<\/code>-compatible file. In a 4GB workspace, I&#8217;m able to map all 85 files and have it seemingly consume only a bit over 2MB of workspace:<\/p>\n<pre><code class=\"code-line\">      ]Config MAXWS\r\n MAXWS  4GB \r\n\r\n      \u235d MapFiles maps 85 files into variables in a namespace named Data\r\n      cf wa\u2190\u2395WA \u22c4 MapFiles \u22c4 cf wa-\u2395WA \r\n4,284,076,808\r\n2,009,600\r\n\r\n      cf +\/Data.(\u2395SIZE \u2395NL \u00af2)\r\n7,577,998,192\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Pretty neat huh? This makes 7.5GB of data available in a 4GB workspace!<\/p>\n<p class=\"code-line\" dir=\"auto\">One of the fields,\u00a0<code>Data.Record_ID<\/code>, is a 16-column character matrix and has a size of about 760MB:<\/p>\n<pre><code class=\"code-line\">      \u2374Data.Record_ID\r\n47647772 16\r\n      cf \u2395SIZE 'Data.Record_ID'\r\n762,364,392\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Let&#8217;s bind\u00a0<code>\u2373<\/code>\u00a0with\u00a0<code>Data.Record_ID<\/code>:<\/p>\n<pre><code class=\"code-line\">      lFind\u2190Data.Record_ID\u2218\u2373     \u235d create the bound function\r\n      lFind 1\u2191Data.Record_ID     \u235d create the hash table\r\nWS FULL\r\n      fRec 1\u2191Data.Record_ID\r\n      \u2227\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Uh oh&#8230; It seems we don&#8217;t have enough room in a 4GB workspace for the hash table. Let&#8217;s start a new session with 10GB:<\/p>\n<pre><code class=\"code-line\">      ]Config MAXWS\r\n MAXWS  10GB \r\n      lFind\u2190Data.Record_ID\u2218\u2373\r\n      cf \u2395WA-\u2368(lFind 1\u2191Data.Record_ID)\u22a2\u2395WA\r\n8,589,934,752\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">With the Dyalog v20.0 default scaling factor, the hash table takes 8.5GB of my 10GB workspace. That&#8217;s a hefty chunk. What if we try the smallest scaling factor,\u00a0<code>\u00af3<\/code>?<\/p>\n<pre><code class=\"code-line\">      8470\u23361 \u00af3\r\n0\r\n      sFind\u2190Data.Record_ID\u2218\u2373\r\n      cf \u2395WA-\u2368(sFind 1\u2191Data.Record_ID)\u22a2\u2395WA\r\n 1,073,741,984\r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">That&#8217;s more manageable and would fit in my 4GB workspace. Why did I use a bound function rather than\u00a0<code>1500\u2336<\/code>\u00a0on\u00a0<code>Data.Record_ID<\/code>? Currently,\u00a0<code>1500\u2336<\/code>\u00a0doesn&#8217;t work with mapped files, but this may be taken under consideration in the future.<\/p>\n<pre><code class=\"code-line\">      z\u21901500\u2336Data.Record_ID\r\nDOMAIN ERROR\r\n      z\u21901500\u2336Data.Record_ID\r\n            \u2227\r\n<\/code><\/pre>\n<h2 id=\"tangent-4---what-timer-is-it\" class=\"code-line\">Tangent #5 \u2013 What Timer Is It?<\/h2>\n<p class=\"code-line\" dir=\"auto\">The\u00a0<code class=\"code-line\">]runTime<\/code>\u00a0user command is a wonderfully easy tool to use to get a rough idea of performance and it remains a staple in my toolbox.\u00a0<code>runtime<\/code>\u00a0is based on\u00a0<code>cmpx<\/code>\u00a0which is found in the\u00a0<code>dfns<\/code>\u00a0workspace. I&#8217;ve found that\u00a0<code>runtime<\/code>&#8216;s results can sometimes vary significantly from one invocation to the next. Dyalog APL provides several system functions to help collect performance data:<\/p>\n<ul class=\"code-line\">\n<li class=\"code-line\"><a href=\"https:\/\/docs.dyalog.com\/20.0\/language-reference-guide\/system-functions\/ai\/\"><code>\u2395AI<\/code><\/a>, one of whose elements is cumulative CPU time of your session. You can use the before and after differential of\u00a0<code>\u2395AI<\/code>\u00a0to get an approximate\u00a0idea of the speed of an expression. This is what\u00a0<code>runtime<\/code>\u00a0and\u00a0<code>cmpx<\/code>\u00a0use.<\/li>\n<li class=\"code-line\"><a href=\"https:\/\/docs.dyalog.com\/20.0\/language-reference-guide\/system-functions\/monitor\/\"><code>\u2395MONITOR<\/code><\/a>,\u00a0which can be used to turn monitoring on for specific lines of a function.<\/li>\n<li class=\"code-line\"><a href=\"https:\/\/docs.dyalog.com\/20.0\/language-reference-guide\/system-functions\/profile\/\"><code>\u2395PROFILE<\/code><\/a>, the &#8220;heavy hitter&#8221; of performance measurement. Even though its primary use case is profiling the performance of entire applications, I still find it useful to get impressive-looking, high-precision, performance data.<\/li>\n<\/ul>\n<p class=\"code-line\" dir=\"auto\">I have a template that I use when I want to use\u00a0<code>\u2395PROFILE<\/code>\u00a0to compare performance of expressions:<\/p>\n<pre><code class=\"code-line\">     \u2207 r\u2190timer n;i;d;l;t\r\n[1]   \u235d n is the number of iterations to run\r\n[2]   \u235d r is [;1] the relative percents based on the fastest expression\r\n[3]   \u235d      [;2] the accumulated time in ms for the expression\r\n[4]   \u235d      [;3] the expression\r\n[5]    \u2395PROFILE\u00a8'stop' 'clear' 'start'\r\n[6]   l1: :For i :In \u2373n\r\n[7]   \u235d insert expressions to time between l1 and l2\r\n[8]   l2: :EndFor\r\n[9]    \u2395PROFILE'stop'\r\n[10]   d\u2190\u2395PROFILE'data'\r\n[11]   l\u2190l1+\u2373l2-l1+1\r\n[12]   r\u2190(\u2193'%',\u2368\u2355\u236a\u230a0.5+100\u00d7\u00af1+t\u00f7\u230a\/t),({\u2375\u2193\u2368+\/\u2227\\' '=\u2375}\u00a8(\u2395NR\u2283\u2395SI)[1+l]),[1.1]\u2368\u21933\u2355\u236at\u2190d[;4]\u233f\u2368d[;2]\u220al\r\n[13]   \u2395PROFILE'clear'\r\n     \u2207<\/code><\/pre>\n<p id=\"the-speedspace-tradeoff\" class=\"code-line\"><strong>The Speed\/Space Tradeoff<\/strong><\/p>\n<p class=\"code-line\" dir=\"auto\">We saw earlier that different scaling factors result in different size hash tables, but what&#8217;s the impact on the speed of searching? Using our 7 hashed versions of\u00a0<code>Words<\/code>\u00a0and running 1,000,000 iterations:<\/p>\n<pre><code class=\"code-line\"> 9%   547.303  _\u2190aWords\u2373phrase \r\n 6%   534.984  _\u2190bWords\u2373phrase \r\n 0%   504.180  _\u2190cWords\u2373phrase \r\n 0%   504.822  _\u2190dWords\u2373phrase \r\n 0%   505.082  _\u2190eWords\u2373phrase \r\n 0%   505.404  _\u2190fWords\u2373phrase \r\n 0%   504.561  _\u2190gWords\u2373phrase \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">In this example, the smallest hash table,\u00a0<code>aWords<\/code>\u00a0runs 9% slower than the 8-times-larger default,\u00a0<code>dWords<\/code>. There&#8217;s no significant speedup using a hash table larger than\u00a0<code>cWords<\/code>, at half the default size.<\/p>\n<p class=\"code-line\" dir=\"auto\">Let&#8217;s take a look at my 47-million record table. Since\u00a0<code>1500\u2336<\/code>\u00a0doesn&#8217;t work (yet) with mapped files, we&#8217;ll have to use function binding. First, we&#8217;ll use the default setting of\u00a0<code>8470\u23361 0<\/code>\u00a0to create an 8.5GB hash table and then use\u00a0<code>8470\u23361 \u00af3<\/code>\u00a0to create a hash table 1\/8 (<code>2*\u00af3<\/code>) the default size or about 1GB.<\/p>\n<pre><code class=\"code-line\">      {}8470\u23361 0 \u22c4 b\u2190\u2395WA \u22c4 lfind\u2190Data.Record_ID\u2218\u2373 \u22c4 {}lfind 2\u2191Data.Record_ID \u22c4 cf b-\u2395WA\r\n  8,589,934,808\r\n      {}8470\u23361 \u00af3 \u22c4 b\u2190\u2395WA \u22c4 sfind\u2190Data.Record_ID\u2218\u2373 \u22c4 {}sfind 2\u2191Data.Record_ID \u22c4 cf b-\u2395WA\r\n  1,073,742,040\r\n      recs\u2190Data.Record_ID[10?\u2262Data.Record_ID;]\r\n      timer 1e6\r\n  0%    920.916  _\u2190lfind recs \r\n 11%   1025.790  _\u2190sfind recs \r\n<\/code><\/pre>\n<p class=\"code-line\" dir=\"auto\">Is an 11% speedup worth an extra 7.5GB of workspace? That&#8217;s really up to you and your priorities for your application.<\/p>\n<h2 id=\"the-long-and-winding-blog-with-apologies-to-the-beatles\" class=\"code-line\">The Long and Winding Blog (with apologies to The Beatles)<\/h2>\n<p class=\"code-line\" dir=\"auto\">Let&#8217;s try to summarize&#8230;<\/p>\n<p class=\"code-line\" dir=\"auto\">If your application needs to search data that&#8217;s &#8220;mostly&#8221; static, you can benefit from using retained hash tables. By &#8220;mostly&#8221;, I mean that the only changes to the array are adding or deleting elements from the end. A typical scenario might be, during application initialization, load the data into the workspace, or map a file, and do a search, thereby creating the hash table. Then all subsequent uses of search functions on that array will be considerably faster.<\/p>\n<p class=\"code-line\" dir=\"auto\">I&#8217;ve got some ideas on things that we at Dyalog Ltd might want to consider doing. Most of these involve empowering the user to have more control over how and when hashing is done. For example, we could:<\/p>\n<ul class=\"code-line\">\n<li class=\"code-line\">make\u00a0<code>8470\u23361<\/code>\u00a0ready for release and publish it so that users can understand how to tune hash table size for their best space\/speed tradeoff.<\/li>\n<li class=\"code-line\">make\u00a0<code>1500\u2336<\/code>\u00a0work with mapped files.<\/li>\n<li class=\"code-line\">make it easier to query the size of a hash table rather than relying on\u00a0<code>\u2395WA<\/code>\u00a0differentials.<\/li>\n<li class=\"code-line\">make it possible to mark an array as &#8220;unhashable&#8221;. Consider the scenario where hash-table-triggering searches are done on an array that&#8217;s not static. Whenever the array changes, the hash table is discarded, only for a new hash table to be generated on a subsequent search. In this case, it&#8217;s probably better that the interpreter fall back to a non-hashing approach.<\/li>\n<li class=\"code-line\">Make it possible to monitor where, when, and how often extemporaneous hashing is done.<\/li>\n<\/ul>\n<h2 id=\"disclaimers---ymmv-your-mileage-may-vary\" class=\"code-line\">Disclaimers \u2013 YMMV (Your Mileage May Vary)<\/h2>\n<p class=\"code-line\" dir=\"auto\">The examples shown in this post are a very small sampling of possible hashing scenarios. They were run on my 32GB Lenovo laptop using Windows 11 and Dyalog v20.0. Your environment and hashing opportunities are likely to be different. In writing this post, I hope to have accomplished spurring you on to do your own investigations as there are potentially significant benefits to be explored.<\/p>\n<p class=\"code-line\" dir=\"auto\">I was caught off guard by the apparent slowdown due to the interpreter repeatedly hashing\u00a0<code>Words<\/code>\u00a0extemporaneously. This led me to try to understand how and when hashing is done and what tools are available to the user. I feel I&#8217;ve been marginally successful at this, but it feels like there is still much to learn.<\/p>\n<h1 id=\"addendum\" class=\"code-line\">Addendum<\/h1>\n<p class=\"code-line code-active-line\" dir=\"auto\">I wrote the above in January 2026. Sharing my findings with the Dyalog development team set in motion an initiative to review some aspects of doing lookups. One result of this initiative was to change the default scaling factor for hash table size (<code>8470\u23361 x<\/code>) from\u00a0<code>0<\/code>\u00a0to\u00a0<code>\u00af3<\/code>. This means that, by default, hash tables in the latest releases of Dyalog v20.0 are 1\/8 the size that they were before. Changing to a smaller hash table scaling factor results in slightly slower lookups, but at a significant savings in workspace consumed.<\/p>\n<p class=\"code-line\" dir=\"auto\">Other work looking at the performance of lookup functions is being undertaken.<\/p>\n<h2 id=\"lookups-are-complicated\" class=\"code-line\">Lookups are Complicated<\/h2>\n<p class=\"code-line\" dir=\"auto\">When you use a lookup function like\u00a0<code>\u2373<\/code>\u00a0or\u00a0<code>\u220a<\/code>, the interpreter attempts to pick the best technique, in order from simplest to most complex, between doing a linear search, using a lookup table, or using a hash table. The decision is based on datatype, size, and depth of the arguments, workspace available, and whether the cost of doing the setup of a more complex technique is likely to result in faster overall execution. In general, the interpreter does a good job of deciding. That doesn&#8217;t mean there aren&#8217;t exceptions. Again, this is where you come in \u2013 you know your data and how it&#8217;s used.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Sometimes it seems that I have the attention span of a cocker spaniel puppy. I start to look at one thing, and then something pops up to distract me and I follow that until another distraction or tangent comes along &hellip; <a href=\"https:\/\/www.dyalog.com\/blog\/2026\/08\/hashing-it-out-lookup-performance-in-dyalog-apl\/\">Continue reading <span class=\"meta-nav\">&rarr;<\/span><\/a><\/p>\n","protected":false},"author":10,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[18],"tags":[],"class_list":["post-9085","post","type-post","status-publish","format-standard","hentry","category-tips-tricks"],"_links":{"self":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9085","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/users\/10"}],"replies":[{"embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/comments?post=9085"}],"version-history":[{"count":26,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9085\/revisions"}],"predecessor-version":[{"id":9785,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/posts\/9085\/revisions\/9785"}],"wp:attachment":[{"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/media?parent=9085"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/categories?post=9085"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.dyalog.com\/blog\/wp-json\/wp\/v2\/tags?post=9085"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}