TE
TechEcho
Home24h TopNewestBestAskShowJobs
GitHubTwitter
Home

TechEcho

A tech news platform built with Next.js, providing global tech news and discussions.

GitHubTwitter

Home

HomeNewestBestAskShowJobs

Resources

HackerNews APIOriginal HackerNewsNext.js

© 2025 TechEcho. All rights reserved.

An easy-to-implement, arena-friendly hash map

140 pointsby grep_itover 1 year ago

7 comments

agumonkeyover 1 year ago
The article <a href="https:&#x2F;&#x2F;www.rfleury.com&#x2F;p&#x2F;untangling-lifetimes-the-arena-allocator" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.rfleury.com&#x2F;p&#x2F;untangling-lifetimes-the-arena-all...</a>, linked 2 level deep to this article, is beyond fascinating.<p>discussed here :<p><a href="https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33379079">https:&#x2F;&#x2F;news.ycombinator.com&#x2F;item?id=33379079</a><p><a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;programming&#x2F;comments&#x2F;xmnfo8&#x2F;untangling_lifetimes_the_arena_allocator&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;programming&#x2F;comments&#x2F;xmnfo8&#x2F;untangl...</a><p><a href="https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;C_Programming&#x2F;comments&#x2F;xmxmex&#x2F;untangling_lifetimes_the_arena_allocator&#x2F;" rel="nofollow noreferrer">https:&#x2F;&#x2F;old.reddit.com&#x2F;r&#x2F;C_Programming&#x2F;comments&#x2F;xmxmex&#x2F;untan...</a><p>PS:it also ties with forth different stacks. Interesting design space.
fovcover 1 year ago
IIRC, NRK started down this path because normal hash tables without free leak memory on every resize.<p>The original series of articles had me thinking about how to implement the normal hash table without leaking. I think it’s possible:<p>First consider a hash table using linear probing with a dedicated arena (restriction will be lifted later). That means memory can grow for free. However, resizing is still tricky because we need to rehash in place while ensuring we don’t “strand” any entries. This can be accomplished by iterating through the hash table entries in order, but starting at an empty entry and looping around. (Proof left as an exercise for the reader)<p>So now we have a hash table that can use realloc to grow. How to generalize for arenas? The key is that the HT will maintain a page map of sorts. The first page is of size M, the second is of size M, and thereafter they’ll be of size 2^n * M. Basically on each resize we double the total capacity by adding a new page. Since the pages are of variable width, though, how to map indexes to pages quickly? The key is to use ffs or ctz to get a rounded log2.
评论 #37727022 未加载
chris_wotover 1 year ago
I finally understand arenas by reading <a href="https:&#x2F;&#x2F;www.rfleury.com&#x2F;p&#x2F;untangling-lifetimes-the-arena-allocator" rel="nofollow noreferrer">https:&#x2F;&#x2F;www.rfleury.com&#x2F;p&#x2F;untangling-lifetimes-the-arena-all...</a>
hnrj95over 1 year ago
Great article, but slight issue. The iteration expression shifts left by 2, effectively limiting the height of the tree to 32. You probably want a rotation instead; otherwise you’ll be locked into child 0 from depth 32 down. I suppose with a good hash the manifestation would be rare.
评论 #37724118 未加载
vitiralover 1 year ago
I had a similar idea and called them &quot;Shifted Search Trees&quot;. I can see how they are also a kind of Trie<p>I&#x27;ve written about how they can not only be used for hashes but also for storing sparse indexes. I&#x27;m hoping to write an extremely tiny Lua implementation which uses only slab allocation (even better than arena IMO! Though I do love arena allocators)<p><a href="https:&#x2F;&#x2F;github.com&#x2F;civboot&#x2F;civboot&#x2F;blob&#x2F;main&#x2F;blog&#x2F;0013-civboot-languages.md#shifted-index-tree-sit">https:&#x2F;&#x2F;github.com&#x2F;civboot&#x2F;civboot&#x2F;blob&#x2F;main&#x2F;blog&#x2F;0013-civbo...</a>
ithkuilover 1 year ago
Related data structure: <a href="https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hash_array_mapped_trie" rel="nofollow noreferrer">https:&#x2F;&#x2F;en.m.wikipedia.org&#x2F;wiki&#x2F;Hash_array_mapped_trie</a>
etermover 1 year ago
Fantastic! I&#x27;ve been working on a project to benchmark different data structures such as this, I&#x27;ll have to have a go at implementing this to see how it fares.