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.

Speed of Rust vs. C

619 pointsby siviziusabout 4 years ago

37 comments

Animatsabout 4 years ago
<i>&quot;But the biggest potential is in ability to fearlessly parallelize majority of Rust code, even when the equivalent C code would be too risky to parallelize. In this aspect Rust is a much more mature language than C.&quot;</i><p>Yes. Today, I integrated two parts of a 3D graphics program. One refreshes the screen and lets you move the viewpoint around. The other loads new objects into the scene. Until today, all the objects were loaded, then the graphics window went live. Today, I made those operations run in parallel, so the window comes up with just the sky and ground, and over the next few seconds, the scene loads, visibly, without reducing the frame rate.<p>This took about 10 lines of code changes in Rust. It worked the first time it compiled.
评论 #26446109 未加载
评论 #26447632 未加载
评论 #26445172 未加载
评论 #26446495 未加载
评论 #26445790 未加载
评论 #26445167 未加载
评论 #26445969 未加载
moonchildabout 4 years ago
&gt; C libraries typically return opaque pointers to their data structures, to hide implementation details and ensure there&#x27;s only one copy of each instance of the struct. This costs heap allocations and pointer indirections. Rust&#x27;s built-in privacy, unique ownership rules, and coding conventions let libraries expose their objects by value<p>The primary reason c libraries do this is not for safety, but to maintain ABI compatibility. Rust eschews dynamic linking, which is why it doesn&#x27;t bother. Common lisp, for instance, does the same thing as c, for similar reasons: the layout of structures may change, and existing code in the image has to be able to deal with it.<p>&gt; Rust by default can inline functions from the standard library, dependencies, and other compilation units. In C I&#x27;m sometimes reluctant to split files or use libraries, because it affects inlining<p>This is again because c is conventionally dynamically linked, and rust statically linked. If you use LTO, cross-module inlining will happen.
评论 #26444180 未加载
评论 #26446384 未加载
评论 #26445148 未加载
评论 #26447560 未加载
评论 #26446590 未加载
vlmutoloabout 4 years ago
&gt; &quot;Clever&quot; memory use is frowned upon in Rust. In C, anything goes. For example, in C I&#x27;d be tempted to reuse a buffer allocated for one purpose for another purpose later (a technique known as HEARTBLEED).<p>This made me laugh
评论 #26444051 未加载
评论 #26444865 未加载
评论 #26445297 未加载
评论 #26446343 未加载
AndyKelleyabout 4 years ago
&gt; computed goto<p>I did a deep dive into this topic lately when exploring whether to add a language feature to zig for this purpose. I found that, although finnicky, LLVM is able to generate the desired machine code if you give it a simple enough while loop continue expression[1]. So I think it&#x27;s reasonable to not have a computed goto language feature.<p>More details here, with lots of fun godbolt links: <a href="https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;8220" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ziglang&#x2F;zig&#x2F;issues&#x2F;8220</a><p>[1]: <a href="https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;T3v881" rel="nofollow">https:&#x2F;&#x2F;godbolt.org&#x2F;z&#x2F;T3v881</a>
评论 #26446430 未加载
评论 #26445358 未加载
评论 #26445806 未加载
评论 #26446400 未加载
jandrewrogersabout 4 years ago
As an observation, performance optimized code is almost always effectively single-threaded these days, even when using all the cores on a CPU to very efficiently process workloads. Given this, it is not clear to me that Rust actually buys much when it comes to parallel programming for the purposes of performance. Is there another reason to focus on parallelism aside from performance?<p>This reminds me of when I use to write supercomputing codes. Lots of programming language nerds would wonder why we didn’t use functional models to simplify concurrency and parallelism. Our code was typically old school C++ (FORTRAN was already falling out of use). The truth was that 1) the software architecture was explicitly single-threaded — some of the first modern thread-per-core designs — to maximize performance, obviating any concerns about mutability and concurrency and 2) the primary performance bottlenecks tended to be memory bandwidth, of which functional programming paradigms tend to be relatively wasteful compared to something like C++. Consequently, C++ was actually simpler and higher performance for massively parallel computation, counterintuitively.
评论 #26444312 未加载
评论 #26444078 未加载
评论 #26445832 未加载
评论 #26444070 未加载
评论 #26444160 未加载
评论 #26445024 未加载
评论 #26447076 未加载
ackxolotlabout 4 years ago
We&#x27;ve implemented network drivers in C and Rust and did a performance comparison. Interestingly, the C-to-Rust-transpiled code ended up being faster than the original C implementation: <a href="https:&#x2F;&#x2F;github.com&#x2F;ixy-languages&#x2F;ixy-languages&#x2F;blob&#x2F;master&#x2F;Rust-vs-C-performance.md" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;ixy-languages&#x2F;ixy-languages&#x2F;blob&#x2F;master&#x2F;R...</a>
评论 #26446440 未加载
simiasabout 4 years ago
I completely agree with the points made here, it matches my experience as a C coder who went all-in on Rust.<p>&gt;&quot;Clever&quot; memory use is frowned upon in Rust. In C, anything goes. For example, in C I&#x27;d be tempted to reuse a buffer allocated for one purpose for another purpose later (a technique known as HEARTBLEED).<p>Ha!<p>&gt;It&#x27;s convenient to have fixed-size buffers for variable-size data (e.g. PATH_MAX) to avoid (re)allocation of growing buffers. Idiomatic Rust still gives a lot control over memory allocation, and can do basics like memory pools, combining multiple allocations into one, preallocating space, etc., but in general it steers users towards &quot;boring&quot; use or memory.<p>Since I write a lot of memory-constrained embedded code this actually annoyed me a bit with Rust, but then I discovered the smallvec crate: <a href="https:&#x2F;&#x2F;docs.rs&#x2F;smallvec&#x2F;1.5.0&#x2F;smallvec&#x2F;" rel="nofollow">https:&#x2F;&#x2F;docs.rs&#x2F;smallvec&#x2F;1.5.0&#x2F;smallvec&#x2F;</a><p>Basically with it you can give your vectors a static (not on the heap) size, and it will automatically reallocate on the heap if it grows beyond that bound. It&#x27;s the best of both world in my opinion: it lets you remove a whole lot of small useless allocs but you still have all the convenience and API of a normal Vec. It might also help slightly with performance by removing useless indirections.<p>Unfortunately this doesn&#x27;t help with Strings since they&#x27;re a distinct type. There is a smallstring crate which uses the same optimization technique but it hasn&#x27;t been updated in 4 years so I haven&#x27;t dared use it.
评论 #26446869 未加载
评论 #26446890 未加载
jblowabout 4 years ago
This entire article is nonsense. To a first approximation, the speed of your program in 2021 is determined by locality of memory access and overhead with regard to allocation and deallocation. C allows you to do bulk memory operations, Rust does not (unless you turn off the things about Rust that everyone says are good). Thus C is tremendously faster.<p>There is this habit in both academia and industry where people say &quot;as fast as C&quot; and justify this by comparing to a tremendously slow C program, but don&#x27;t even know they are doing it. It&#x27;s the blind leading the blind.<p>The question you should be asking yourself is, &quot;If all these claims I keep seeing about X being as fast as Y are true, then why does software keep getting slower over time?&quot;<p>(If you don&#x27;t get what I am saying here, it might help to know that performance programmers consider malloc to be tremendously slow and don&#x27;t use it except at startup or in cases when it is amortized by a factor of 1000 or more).
评论 #26448369 未加载
评论 #26448394 未加载
评论 #26448310 未加载
benreesmanabout 4 years ago
A comparison between Rust and modern C++ would be more interesting in my opinion. It seems that those languages are closer in the design goal space than either is to C.
评论 #26446945 未加载
not_knuthabout 4 years ago
What a well-written and interesting piece that gets to the point!<p>Compared to all the religious texts I&#x27;ve read about Rust, this is a huge breath of fresh air.<p>Thanks for sharing! Bookmarking this.
Aissenabout 4 years ago
&gt; Rust can&#x27;t count on OSes having Rust&#x27;s standard library built-in, so Rust executables bundle bits of the Rust&#x27;s standard library (300KB or more). Fortunately, it&#x27;s a one-time overhead.<p>No, it&#x27;s not, especially if you have multiple binaries. There are hacks, like using a multi-call single binary, (forget about file-based privilege separation), or using an unmaintained fork of cargo to build a rust toolchain capable of dynamic linking libstd. See: <a href="https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;link-the-rust-standard-library-dynamically&#x2F;29175&#x2F;4" rel="nofollow">https:&#x2F;&#x2F;users.rust-lang.org&#x2F;t&#x2F;link-the-rust-standard-library...</a> and <a href="https:&#x2F;&#x2F;github.com&#x2F;johnthagen&#x2F;min-sized-rust" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;johnthagen&#x2F;min-sized-rust</a><p>I&#x27;d be interested in any up-to-date trick to do better than this.
pabs3about 4 years ago
FTR, there are some efforts to integrate GCC &amp; Rust:<p><a href="https:&#x2F;&#x2F;github.com&#x2F;antoyo&#x2F;rustc_codegen_gcc" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;antoyo&#x2F;rustc_codegen_gcc</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;Rust-GCC&#x2F;gccrs" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;Rust-GCC&#x2F;gccrs</a> <a href="https:&#x2F;&#x2F;github.com&#x2F;sapir&#x2F;gcc-rust&#x2F;" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;sapir&#x2F;gcc-rust&#x2F;</a>
jancsikaabout 4 years ago
&gt; alloca and C99 variable-length arrays<p>I remember making an argument on a mailing list against using alloca on the grounds that there&#x27;s usually a stack-blowing bug hiding behind it. As I revisited the few examples I remembered of it being used correctly, I strengthened my argument by finding more stack-blowing bugs hiding behind uses of alloca.
评论 #26444355 未加载
评论 #26444048 未加载
skohanabout 4 years ago
&gt; Both are &quot;portable assemblers&quot;<p>I don&#x27;t tend to think of Rust as &quot;portable assembly&quot;, and this is indeed one of the points where I think it differs the most from C. I think of &quot;portable assembly&quot; as being applicable to C, because it is some version of a &quot;minimal&quot; level of abstraction for a high-level language. Rust is very much a tool for abstraction, and one of the USPs of rust is that the compiler abstracts away the low-level details of memory management in a way which is not as costly as other automatic memory management strategies.<p>Maybe it&#x27;s due to lack of experience, but with C code it&#x27;s fairly easy to look at a block of code and imagine approximately which assembly would be generated. With highly abstract Rust code, like with template-heavy C++ code, I don&#x27;t feel like that at all.
评论 #26447511 未加载
zestererabout 4 years ago
Code &#x27;bloat&#x27; is a bizarre metric to use for anything unless you&#x27;re on a platform with incredibly constrained executable memory like an embedded device.<p>The fact that Rust specialises its generic code according to the type it&#x27;s used with it not some inherent disadvantage of generics. That&#x27;s what they&#x27;re <i>supposed</i> to do. By choosing to not specialise, you&#x27;re actively making the decision to make your code <i>slower</i>. Rust has mechanisms for avoiding generic specialisation. They&#x27;re called trait objects and they work brilliantly.<p>When you use void* in your data structures in C, you&#x27;re not winning anything when compared to Rust. You&#x27;re just producing slower code that mimics the behaviour of Rust&#x27;s trait objects, but more dangerously.<p>Code &#x27;bloat&#x27; (otherwise known as &#x27;specialising your code correctly to make it run faster&#x27;) is not a reason to not use Rust in 2021, so please stop pretending that it is.
评论 #26446131 未加载
dig1about 4 years ago
&gt; For example, in C I&#x27;d be tempted to reuse a buffer allocated for one purpose for another purpose later (a technique known as HEARTBLEED).<p>You can do that in Java (with byte arrays) or in Common Lisp, so what is the point here? It is not practice in Java, Lisp nor in C and C++.<p>&gt; It&#x27;s convenient to have fixed-size buffers for variable-size data (e.g. PATH_MAX) to avoid (re)allocation of growing buffers<p>This is because OS&#x2F;Kernel&#x2F;filesystem guarantee path max size.<p>&gt; Idiomatic Rust still gives a lot control over memory allocation, and can do basics like memory pools, ... but in general it steers users towards &quot;boring&quot; use or memory.<p>The same is done by sane C libraries (e.g. glib).<p>&gt; Every operating system ships some built-in standard C library that is ~30MB of code that C executables get for &quot;free&quot;, e.g. a &quot;Hello World&quot; C executable can&#x27;t actually print anything, it only calls the printf shipped with the OS.<p>printf is not shipped with the OS, but with libc runtime. It doesn&#x27;t have to be runtime (author needs to learn why this libc runtime is shared library and not the usually statically linked library) and you can use minimal implementations (musl) if you want static binaries with minimal size.<p>So you are saying Rust doesn&#x27;t call (g)libc at all and directly invoke kernel interrupts? Sure, you can avoid this print &quot;overhead&quot; in C with 3-4 lines of inline assembly, but, why?<p>&gt; Rust by default can inline functions from the standard library, dependencies, and other compilation units.<p>So do C compiler.<p>&gt; In C I&#x27;m sometimes reluctant to split files or use libraries, because it affects inlining and requires micromanagement of headers and symbol visibility.<p>Functions doesn&#x27;t have to be in headers to be inlined.<p>&gt; C libraries typically return opaque pointers to their data structures, to hide implementation details and ensure there&#x27;s only one copy of each instance of the struct. This costs heap allocations and pointer indirections. Rust&#x27;s built-in privacy, unique ownership rules, and coding conventions let libraries expose their objects by value, so that library users decide whether to put them on the heap or on the stack. Objects on the stack can can be optimized very aggressively, and even optimized out entirely.<p>WTF? Stopped reading after this.<p>I find this post a random nonsense and I&#x27;d urge author to read some serious C book.
评论 #26450783 未加载
评论 #26446178 未加载
cjohanssonabout 4 years ago
Human-friendlyness and bug-prevention is very important, of course everthing in Rust can be created in C or Assembler och in machine-code but the question is how feasible is it that a typical human can do it? Rust has a lot of potential I think
评论 #26445514 未加载
gattrabout 4 years ago
To practise Rust, I rewrote my small C99 library in it [1]. Performance is more or less the same, I only had to use unchecked array access in one small hot loop (details in README.md). I haven&#x27;t ported multithreading yet, but I expect Rust&#x27;s Rayon parallel iterators will likewise be comparable to OpenMP.<p>[1] <a href="https:&#x2F;&#x2F;github.com&#x2F;GreatAttractor&#x2F;libskry_r" rel="nofollow">https:&#x2F;&#x2F;github.com&#x2F;GreatAttractor&#x2F;libskry_r</a>
评论 #26449765 未加载
mratsimabout 4 years ago
&gt; There are other kinds of concurrency bugs, such as poor use of locking primitives causing higher-level logical race conditions or deadlocks, and Rust can&#x27;t eliminate them, but they&#x27;re usually easier to diagnose and fix.<p>Which is why so many people are creating formal verification languages and spending years in research to fix those ... That just isn&#x27;t true. It&#x27;s a very complex problem that is an issue in both hardware (cache-coherency protocols) to OS (atomics locks) to higher level construct (commit-rollback in databases).<p>Consequently<p>&gt; But the biggest potential is in ability to fearlessly parallelize majority of Rust code, even when the equivalent C code would be too risky to parallelize. In this aspect Rust is a much more mature language than C.<p>This couldn&#x27;t be more wrong either. Rust doesn&#x27;t help you write synchronization primitives safely because it doesn&#x27;t handle synchronization like locks, condition variables or atomics. You need formal verification to be fearless.
评论 #26446171 未加载
评论 #26446130 未加载
nyc_pizzadevabout 4 years ago
Shouldn’t this be Rust vs C++? C++ has a lot more parallels to Rust. Both are big, complex, and safe languages that can tuned for high performance. Infact, I would like to see more comparisons of Rust and C++ in the future.
评论 #26449334 未加载
评论 #26446876 未加载
评论 #26459865 未加载
ReactiveJellyabout 4 years ago
I&#x27;m a Rust evangelist, but the article is titled &quot;Speed of Rust vs. C&quot; and doesn&#x27;t seem to contain even one benchmark.<p>For fuck&#x27;s sake.
评论 #26446855 未加载
评论 #26447704 未加载
评论 #26446864 未加载
评论 #26452079 未加载
brundolfabout 4 years ago
&gt; For example, in C I&#x27;d be tempted to reuse a buffer allocated for one purpose for another purpose later (a technique known as HEARTBLEED)<p>Pahaha
12thwonderabout 4 years ago
I prefer to have great ideas in rust ported over to C instead of rewriting everything with Rust. this approach will benefit all the existing softwares written in C which I think is much larger than Rust in terms of both impact and code size.<p>am I a minority having this opinion?
评论 #26446205 未加载
评论 #26447896 未加载
评论 #26446143 未加载
评论 #26446503 未加载
评论 #26446063 未加载
planetisabout 4 years ago
Its just amusing, in this thread everyone with critical thinking and skeptical is down voted, even if one expresses himself moderately. It shows how much of zealots, Rust fanboys have become.
评论 #26447315 未加载
评论 #26448243 未加载
评论 #26446460 未加载
评论 #26447171 未加载
评论 #26449781 未加载
评论 #26446760 未加载
评论 #26447967 未加载
hsaliakabout 4 years ago
For parallelism, Modern tooling like TSAN can close the gap somewhat. If you are planning to introduce threads, not testing it with TSAN is silly at best.
评论 #26448262 未加载
antiquarkabout 4 years ago
Yeah but, C is essentially 32 years old by now.<p>A more useful comparison would be to modern C++.
评论 #26444917 未加载
评论 #26445303 未加载
eqvinoxabout 4 years ago
Is it possible to do RCU in Rust? Without unsafe blocks?
评论 #26449132 未加载
评论 #26449584 未加载
docmarsabout 4 years ago
Awesome, now do charts! ;)
Shadonototroabout 4 years ago
Very biased comparison without actual source or numbers to back things<p>Even more surprising it got to front page<p>Do people really have low standard of quality on hacker news too?
评论 #26446879 未加载
knownabout 4 years ago
<a href="https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;fastest&#x2F;rust.html" rel="nofollow">https:&#x2F;&#x2F;benchmarksgame-team.pages.debian.net&#x2F;benchmarksgame&#x2F;...</a> shows C is generally better
评论 #26445452 未加载
0xdeadfeedabout 4 years ago
&gt; While C is good for writing minimal code on byte-by-byte pointer-by-pointer level,<p>Billions of cars with multi-billion ECUs, practically every device running an OS, and several NASA rovers disagree.
up2isomorphismabout 4 years ago
The article talks way too high level and is written like a marketing people even the title sounds technical, for example:<p>&quot;Rust enforces thread-safety of all code and data, even in 3rd party libraries, even if authors of that code didn&#x27;t pay attention to thread safety. Everything either upholds specific thread-safety guarantees, or won&#x27;t be allowed to be used across threads.&quot;
评论 #26447978 未加载
评论 #26447903 未加载
up2isomorphismabout 4 years ago
My experience is that languages survives not because of a particular feature, but because they are USEFUL in practice to produce a software.<p>The fact that C is used in so many places speaks for itself about it usefulness. And this is done by writing software by majority of C programmers instead of jumping on every forum to attack other languages, writing extended blog posts just to convince people that they &quot;should&quot; switch to the language they like.<p>Also if you believe bounds check is the most difficult thing in software development, it just mean that you haven&#x27;t dealt with a sufficient system yet or you just pretends to be.<p>The similar thing also applied to that if you think naively putting pthread_mutex_lock and unlock around the data structure is hard, it just means you haven&#x27;t touched the scenarios that C programmers resorts to non-trivial locking mechanisms for.
评论 #26452709 未加载
评论 #26452080 未加载
_a1_about 4 years ago
I appreciate the article, but it would be really nice if the author could add a timestamp to his blog posts. Without timestamps, it&#x27;s impossible to know if any issue described in the article body still exists.<p>I didn&#x27;t read it, because it might present outdated knowledge.
评论 #26444904 未加载
评论 #26447627 未加载
brwellabout 4 years ago
&gt; &quot;Clever&quot; memory use is frowned upon in Rust. In C, anything goes.<p>No, it does not. If Rust programmers don&#x27;t have discipline in C, other people have.<p>And don&#x27;t drag out some random CVE numbers again. These are about a <i>fraction</i> of existing C projects, many of them were started 1980-2000.<p>It is an entirely different story if a project is started with sanitizers, Valgrind and best practices.<p>I&#x27;m not against Rust, except that they managed to take OCaml syntax and make it significantly worse. It&#x27;s just ugly and looks like design by committee.<p>But the evangelism is exhausting. I also wonder why corporations are pushing Rust. Is it another method to take over C projects that they haven&#x27;t assimilated yet?
评论 #26446682 未加载
评论 #26446141 未加载
评论 #26445841 未加载
评论 #26445334 未加载
评论 #26446216 未加载
评论 #26450977 未加载
评论 #26445220 未加载
评论 #26445751 未加载
discardable_danabout 4 years ago
A graph would be good. Any graph. Preferably multiple. Otherwise, this is all empirical data. Show me why Rust wins, and how. Telling me &quot;doubly-linked lists are slow&quot; is not useful, as a developer considering one of these two languages.
评论 #26444779 未加载
评论 #26444225 未加载
评论 #26450985 未加载
0xdeadfeedabout 4 years ago
Show me some numbers please, or I’ll just take it as another list of wishes that Rust fans think&#x2F;want to be true.