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

科技回声

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

GitHubTwitter

首页

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

资源链接

HackerNews API原版 HackerNewsNext.js

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

Show HN: Revring, a circular buffer with zero memory waste

12 点作者 codehero将近 10 年前

5 条评论

jhallenworld将近 10 年前
A conceptually simpler way to do this is to assign one (or more) extra bits in the head an tail pointers. For example, for a 512 entry ring, use 16-bit indices. Whenever you index the ring, and the index with 511 before performing the index.<p><pre><code> Write to ring: ring[511&amp;(head++)] = data Read from ring: data = ring[511&amp;(tail++)] Ring is empty: head == tail Ring is full: tail + 512 == head</code></pre>
dpc_pw将近 10 年前
This post is literally about saving one byte, at the cost of being slower and not producer-consumer friendly. Not very interesting.<p>The flag could have been hidden in any other field as a bit or something. Then it could be at least masked with simple AND operation which is usually faster than branching, especially on pipelined CPUs.<p>Update: Quick implementation: <a href="https:&#x2F;&#x2F;gist.github.com&#x2F;dpc&#x2F;a194b7784adfa150a450" rel="nofollow">https:&#x2F;&#x2F;gist.github.com&#x2F;dpc&#x2F;a194b7784adfa150a450</a><p>This fix for concurrency issue is an ugly hack. I&#x27;m not sure if it&#x27;s even correct in this particular scenario, and definitely not proper for anything that would aspire to be good reusable code. I&#x27;d advise this code to push atomicity requirement onto caller. Irqs should have been disabled by calling code.<p>&quot;register&quot; keyword is obsolete. There&#x27;s no point in using it.
kstenerud将近 10 年前
I did something similar back in the DOS era for a serial port library: <a href="https:&#x2F;&#x2F;github.com&#x2F;kstenerud&#x2F;DOS-Serial-Library&#x2F;blob&#x2F;master&#x2F;serial.c" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;kstenerud&#x2F;DOS-Serial-Library&#x2F;blob&#x2F;master&#x2F;...</a>
rhaps0dy将近 10 年前
You still have a buffer or size X that may store X-Y items. I fail to see the &quot;zero memory waste&quot; (not that it&#x27;s a good tradeoff).
评论 #9741547 未加载
评论 #9741571 未加载
cpr将近 10 年前
Gosh, all that to save one byte? ;-)
评论 #9742640 未加载
评论 #9741641 未加载