TE
科技回声
首页24小时热榜最新最佳问答展示工作
GitHubTwitter
首页

科技回声

基于 Next.js 构建的科技新闻平台,提供全球科技新闻和讨论内容。

GitHubTwitter

首页

首页最新最佳问答展示工作

资源链接

HackerNews API原版 HackerNewsNext.js

© 2025 科技回声. 版权所有。

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

140 点作者 grep_it超过 1 年前

7 条评论

agumonkey超过 1 年前
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.
fovc超过 1 年前
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_wot超过 1 年前
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>
hnrj95超过 1 年前
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 未加载
vitiral超过 1 年前
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>
ithkuil超过 1 年前
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>
eterm超过 1 年前
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.