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.

Hard Mode Rust (2022)

141 pointsby rrampage4 months ago

18 comments

tomsmeding4 months ago
I can&#x27;t help but feel this is reintroducing some of the problems it claims to solve in working around the difficulties.<p>* The fact that there is an `Oom` error that can be thrown means that there is no increase in reliability: you still don&#x27;t know how much memory you&#x27;re going to need, but now you have the added problem of asking the user for how much memory you&#x27;re going to be using — which they are going to be guessing blindly on!<p>* This is because the memory usage is not much more predictable than it would be in easy mode Rust. (Also that &quot;mem.lem()&#x2F;2&quot; scratch space is kind of crappy; if you&#x27;re going to do this, do it well. Perhaps in allocating the <i>correct</i> amount of scratch space, you end up with a dynamic allocator at the end of your memory. Does that sound like stack space at the start of memory and heap space at the end of memory? Yes, your programming language does that for you already, but built-in instead of bolted-on.)<p>* Furthermore, the &quot;easy mode&quot; code uses lots of Box, but if you want you can get the benefits of RAII without all the boxes by allocating owned vectors scrupulously. Then you get the benefit of an ownership tracking system in the language&#x27;s typesystem without having to `unsafe` your way to a half reimplementation of the same. You can get your performance without most of the mess.<p>* Spaghetti can be avoided (if you so desire) in the same way as the previous point.<p>What you do achieve is that at least you can <i>test</i> that Oom condition. Perhaps what you actually want is an allocator that allows for simulating a particular max heap size.
评论 #42875774 未加载
throwaway20374 months ago
The article starts with:<p><pre><code> &gt; This criticism is aimed at RAII — the language-defining feature of C++, which was wholesale imported to Rust as well. &gt; because allocating resources becomes easy, RAII encourages a sloppy attitude to resources </code></pre> Then lists 4x bullet points about why it is bad.<p>I never once heard this criticism for RAII in C++. Am I missing something? Coming from C to C++, RAII was a godsend for me regarding resource clean-up.
评论 #42875786 未加载
评论 #42875603 未加载
评论 #42877021 未加载
评论 #42875597 未加载
评论 #42876238 未加载
kelnos4 months ago
The criticism of RAII is a little odd to me. The author list four bullet points as to why RAII is bad, but I don&#x27;t think I&#x27;ve ever found them to be an issue in practice. When I&#x27;m writing C (which of course does not have RAII), I rarely think all that deeply about how much I am allocating. But I also don&#x27;t write C for embedded&#x2F;constrained devices anymore.
评论 #42875912 未加载
评论 #42875868 未加载
feverzsj4 months ago
So, it&#x27;s just arena allocator, which is still RAII.
siev4 months ago
Moving all allocations outside the library was also explored in [Minimalist C Libraries](<a href="https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2018&#x2F;06&#x2F;10&#x2F;" rel="nofollow">https:&#x2F;&#x2F;nullprogram.com&#x2F;blog&#x2F;2018&#x2F;06&#x2F;10&#x2F;</a>)
forrestthewoods4 months ago
Interesting conclusion. I recently tried something similar and gave up. The extra lifetimes are awful. Including in author’s post, imho.<p>Rust sucks at arenas. I love Rust. But you have to embrace RAII spaghetti or you’re gonna have an awful, miserable time.
评论 #42878634 未加载
witx4 months ago
These are some really weird arguments against RAII. First and foremost it is not only used for memory allocation. Second the fact that we have RAII doesn&#x27;t mean it is used like &quot;std::lock_guard&quot; to acquire and free the resource in the same &quot;lifetime&quot;, always. Actually in 10+ years of c++ that&#x27;s like 1% of what I use RAII for<p>The only point I agree with is the deallocation in batches being more efficient.<p>&gt; Lack of predictability. It usually is impossible to predict up-front how much resources will the program consume. Instead, resource-consumption is observed empirically.<p>I really don´t understand this point.
WhereIsTheTruth4 months ago
C++ problem isn&#x27;t just RAII, it&#x27;s the language becomes an unreadable mess, just like this Rust code<p>C++ and Rust share the same criticism<p>- poor readability<p>- poor code navigation<p>- poor compilation speed<p>I find it interesting how they call simplicity &quot;hard mode&quot;, quite concerning
kookamamie4 months ago
&gt; Ray tracing is an embarrassingly parallel task<p>Yet, most implementations do not consider SIMD parallelism, or they do it in a non-robust fashion, trusting the compiler to auto-vectorize.
akshayshah4 months ago
It’s interesting that the author now works on TigerBeetle (written in Zig). As I understand it, TigerBeetle’s style guide leans heavily on this style of resource management.
评论 #42876981 未加载
评论 #42875611 未加载
nayuki4 months ago
&gt; Hard Mode means that you split your program into std binary and #![no_std] no-alloc library. Only the small binary is allowed to directly ask OS for resources. For the library, all resources must be injected. In particular, to do memory allocation, the library receives a slice of bytes of a fixed size, and should use that for all storage.<p>I did exactly this in my QR Code generator library, C port and second Rust port: <a href="https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;QR-Code-generator&#x2F;blob&#x2F;master&#x2F;c&#x2F;qrcodegen.h">https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;QR-Code-generator&#x2F;blob&#x2F;master&#x2F;c&#x2F;qr...</a> , <a href="https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;QR-Code-generator&#x2F;blob&#x2F;master&#x2F;rust-no-heap&#x2F;src&#x2F;lib.rs">https:&#x2F;&#x2F;github.com&#x2F;nayuki&#x2F;QR-Code-generator&#x2F;blob&#x2F;master&#x2F;rust...</a><p>Writing the Rust code felt way safer because the language took care of enforcing rules about borrowing parts of the buffer.
tialaramex4 months ago
Probably wants a (2022) acknowledgement that this is a blog post from 2022.
agentultra4 months ago
I think Haskell also pushes you in this direction. It gets super annoying to annotate every function with ‘IO’ and really difficult to test.<p>Sure, you still have garbage collection but it’s generally for intermediate, short lived values if you follow this pattern of allocating resources in main and divvying them out to your pure code as needed.<p>You can end up with some patterns that seem weird from an imperative point of view in order to keep ‘IO’ scoped to main, but it’s worth it in my experience.<p><i>Update</i>: missing word
jpc04 months ago
&gt; It’s on the caller to explicitly implement a concurrent queue to distributed specific work items<p>The threadpool is usually the easy part of concurrent algorithm&#x27;s, with the concurrent queue being one of the hardest to implement in a performant manner. The is a &quot;draw the rest of the owl&quot; moment...
jokoon4 months ago
I know that the C++ syntax can be messy, but to me the rust syntax can reach above levels of complex and difficult to read.<p>Maybe I should practice it a bit more.
purplesyringa4 months ago
&gt; This… probably is the most sketchy part of the whole endeavor. It is `unsafe`, requires lifetimes casing, and I actually can’t get it past miri. But it should be fine, right?<p>I just can&#x27;t. If you&#x27;re ignoring the UB checker, what are you even doing in Rust? I understand that &quot;But it should be fine, right?&quot; is sarcastic, but I don&#x27;t understand why anyone would deliberately ignore core Rust features (applies both to RAII and opsem).
duped4 months ago
I usually like matklad&#x27;s writings but I have to be overly dismissive for a change.<p>This is a skill issue.<p>C, C++, Rust, it doesn&#x27;t matter - if you have a systems problem where you are concerned about resource constraints and you need to care about worst-case resource allocation, you <i>must</i> consider the worst case when you are designing the program. RAII does not solve this problem, it never did, and anyone who thinks it does needs to practice programming more.<p>It is not &quot;hard&quot; to solve though. It&#x27;s just another thing your compiler can&#x27;t track and you need to be reasonably intelligent about. There are sections of a program that cannot tolerate resource exhaustion, for which you set some limit, which is then programmable in some way, which then uses some shared state to acquire and release things of which RAII may help. But you still must handle or otherwise report failures.<p>This is a bread and butter architecture concern for systems programming and if you&#x27;re <i>not</i> doing it, get good at it.
评论 #42875845 未加载
评论 #42876019 未加载
mplanchard4 months ago
(2022)