215 points · by BaselAshraf81 · 6d ago · 175 comments
Taking python as an example, one could use benefit of hindsight and make better decisions from the start ( offloading slow parts to C binaries, better DS decisions, eliminating bloat etc.. ) if they rewrite the same app in Python. This is kind of what happens when a program gets rewritten to another language ( Rust ) - better decisions are made from the start and less bloat is copied over.
Yes, obviously there are exceptions to this. But most of the time it is the choices we make that make the program slow and not the language itself.
It used to be that rewriting programs were almost always a dumb idea due to how much effort went into it - see: https://www.joelonsoftware.com/2000/04/06/things-you-should-... . But AI has made this argument less compelling imo. I now regularly rewrite big chunks of programs with various end goals given to the agent.
One of the problems, for C, is the use of . to access a field via a variable, and -> to access a field via a pointer. To switch between a pointer and a variable in a struct means you've got to re-edit large quantities of code everywhere. The result is just too tedious.
This was the motivation in D for using . for both. It turns out, it is completely unnecessary for having both . and -> as the compiler semantic pass can trivially figure it out. Eliminating this issue makes for much easier redesign of a data structure.
My mind equates -> to "this could be NULL" without ever looking at the function signature.
It is way more of a pain in the ass for refactors than = vs := though.
-> just… isn’t. Acknowledging it’s a pointer with a different accessor is just not really that important. I mean, in some specific cases it might be, but in general it’s not.
Point in case: go?
struct S {
int a;
int f(); //getter
void f(int i); // setter
}
void test() {
S s;
int i = s.a;
int j = s.f;
s.f = i;
S* ps;
int k = ps.a;
int l = ps.f;
ps.f = k;
}
i.e. the field can be replaced by a function and the usage of it would not change.For me that's definitely a bad thing. It's the reason I don't like Rust syntax, they make you write everything out even if there is no possible way to misinterpret the code. And they never do things like Zig enum variant shorthand because syntax sugar is evil. Maybe they're just diabetic.
The port was pretty direct, function for function. It was even line for line where language and library differences didn't offer an easier way. I was just trying to see if Python was so slow that we couldn't consider using it in production, and this was the most speed critical batch we had.
I assumed the speed difference was a bug that was skipping work, but that seemed unlikely because the output was correct. It turned out to be a bug in the C++ code that was needlessly reproducing work (an issue with cache keys). The bug was extremely subtle. When I fixed the C++, there was very little difference in speed as the batch was I/O bound when the cache was working.
So, adding to your point, sometimes you can greatly improve speed by rewriting in a slower language, even without performance in mind.
But also technically I think your point still stands. Because rewriting it helped you find a defect in the original. Which did improve performance of the original.
It’s quite interesting the two implementations performance was so close in the end. Do you consider one implementation “better” for non-performance reasons? Maybe one type of dev is easier to find. Or one language is easier to create and maintain correctly behaving programs in your environment?
Hiring devs who already knew Python was not really an option in 1997. But we quickly found that there was not much difference in time to ramp up a C++ developer on our C++ codebase vs. ramping up a developer on Python plus our Python codebase. It was just a matter of finding developers who were happy to learn a language they may or may not have heard of, but had likely never looked into.
We converted a Python pipeline to Rust and it's 50x+ faster depending on the customer.
This was mostly important for tasks like backfills where you're processing hundreds of times more data than usual.
the true performance hog is like sand, spread across the system, a little bit in every place.
The number of use cases where you actually can move just a small part of the program to C++/Rust and that solves performance is actually really small. Basically highly numerical things like AI or SMT solving. Even supposedly IO bound things like Django tend to be quite slow in Python.
Somewhere in your code is a little whammy where someone invalidated a bunch of tree nodes which took about 8 nanoseconds and caused the layout engine to now do tons of computation that it didn't have to do before. No profiler in the world is going to save you from this problem, it's going to tell you that the problem is in the layout code. Well duh, thanks for that, of course the compute is in the layout code, it's always in the layout code, all we do is layout and draw. So I have to find the 3 nanoseconds where somebody invalidated something that they have no business invalidating because it's wrong. And the way you fix that is [to have a sense of how to check that you're invalidating the right thing, regression testing, visualizations, logging, planning] and it could be literally 4 or 5 instructions dirtying the cache and that's not going to show up in a profile.
[paraphrasing; Casey: if you've developed the whole browser without caring about this, at the end you're going to find it very slow and not be able to squeeze any more performance out of the layout engine? Rico: Right, this is not something you can fix at the finish line, it needs a plan]"""
- Rico Mariani, interview: https://youtu.be/48Rig6v-xYU?t=3615
Oh, and they can be annoying to refactor later on.
It had some warts, but IMO was far more easy to solve than doing something like the swift project. In hindsight apple should've invested into Rust and continued to improve Objective-C as an applications programming language.
https://simonshine.dk/articles/nix-appimage-when-musl-is-not...
I was thinking of maybe turning it into a minimal UI framework. I noticed that Quickshell apps in Hyprland are comparatively very large so something lighter weight might be useful.
In mid-2013, NewsBlur described in some detail their deployment and statistics. Python/Django, 48 servers. I confidently calculated that, with implementation efficient, one would be sufficient, though with little headroom, and further vertical scaling might have started running into I/O limitations. (I doubt a single server would be possible any more.)
It’s popular to scale horizontally far earlier than you actually need to. And if you can commit to sticking with vertical scaling (single big machine), you can simplify (and further speed up) things enormously.
I don’t know about now, but five years ago HN ran on a single server <https://news.ycombinator.com/item?id=28479595>, though with a standby machine also present since 2017ish.
(For big data you can use Python to drive some native code frameworks - it is good at that, for tiny data it’s irrelevant because there’s not much to do, but the moderate case gets awkward).
And python being interpreted and dogslow means any heavy computation you write in pure python needs to be offloaded too instead of just putting it on a threadpool, again making your architecture more complicated.
Add in serverless functions, which need startup, and now something that should have taken microseconds will take seconds.
Node is JITed at least, but still no shared-memory multithreading, so it still encourages all the architectural bloat.
Depends what you are doing. Not a problem for something like a CRUD web app. Not a problem for most desktop apps.
> heavy computation you write in pure python
Why would you write heavy computation in pure Python?
Imagine being able to use only 1% of the machines you used to require, by migrating away from Python. This is very realistic for companies doing heavy computational work.
As with anything performance related you need to profile the code with realistic data/workflows and identify the parts that are slow.
Typically, doing things like changing algorithms, data structures, careful use of caching, reusing calculations, etc. are more important to performance than the choice of language.
Thing is, the TS app weighted around 4GB overall and took 2s to do its thing. [A backend process, an http api and a static website generator].
I needed to decrease the size and increase performance dramatically,to be able to put it in a 2GB 2vcpu instance, which costs $12 monthly. Otherwise it would eat the profit.
Claude ported it to Rust, in around 2 days total. And it went down to 1.5Gb max, and 1/10 of the CPU usage. Now it sits in the aws europe zone making money.
The only JS-native answer is to use typed arrays, which is using a sledgehammer to crack a nut.
In all fairness 6,000,000 messages per second across a single socket is likely an unrealistic scenario but application stability over multiple months pushing out message traffic across many sockets from many servers under a single application process is a realistic concern as is number of simultaneous open sockets.
100x performance gain on on-prem hosting means everything to hardware costs.
The biggest differentiator is proper thread support. You can handle things concurrently within a single process and not start one or more processes per concurrent user. Go can do this, too.
The secondary is just having a chance to rethink the architecture. Do something smart.
The third is language and ecosystem features that encourage more correct software. Rust beats Go here. Too many specifics, nothing new. Haskell can do this, too.
I've seen this at almost every company I've worked at.
In the app you could refactor the slow code, or optimise the run the time without introducing Rust.
You could run an optimised micro service.
Finally it is most likely to be an issue with database transactions which can be solved with better queries, indexes, separate read/write, caching, batching, and so many other techniques to avoid non critical blocking.
Do we actually see this? I mostly write in a dynamic language (Elixir) and it generally one-shots features with no errors. I also have an extended project in zig and it rarely if ever writes spatiotemporal memory errors and doesn't segfault unless I'm miscompiling targets or doing something truly cursed (pointers crossing ABIs in a dllloaded shared library)
Rust here was mostly because they wanted to link to it. It has some benefits by itself, but the real killer feature was the e2e tests that they had for the effort.
TCC even has a —script mode for C.
Since then writing full 100% C or C++ applications, has been more the exception than the rule.
Even at CERN, Python | Java and C++ combo was already slowly taking off in 2003.
Usually it has been the 5% that is slow in Java, .NET, Node.js, <pick your lang>, than full rewrites, hence I am note big fan of "Rewrite X in Y", which more often than not is all about pimping up CVs.
Thanks to the runtime improvements (JIT and AOT), additional language features for low level coding, this need to rewrites has been decreasing, with exception to bindings to existing libraries.
I noticed something interesting about LLMs: if you ask them to create a Rust inference tool, it may use Rust for the glue code, but it'll stick to Python for everything else. LLMs are just like us: they don't like wasting time rewriting everything in Rust, especially when they know that the most important libs, say PyTorch, aren't written in Python.
Py2many is an implementation of these concepts.
I recently discovered that one can apparently distribute Rust projects on npm by compiling to wasm. action-validator used to do this. Had been thinking about something like this.
Rust itself had an understandability issue until LLMs came along and started speedrunning parts of the Rust development process, so this argument is language AND time immaterial.
And something tells me C# and Java could have gotten 90% of the improvement without any rewrite. I'll have to try later today.
Then absolutely rewrite it so that it uses Tauri instead. Which takes your executable from 300MB to about 5MB for something that actually does something meaningful.
It's certainly still "replace the slow (but really, inefficient, not just slow) parts", but instead of 5% it's 98%.
Doesn't seem a recipe for success
"When in Rome, do as the Romans do"
Most often, Python is enough.
But if you can optimize C, most often you can also optimize Python.
I have a SLAM implementation in pure Python+Numba where all points/keyframes are kept in a single flat table, no allocation.
Also, we have LLMs now. Rust should be the default for many things.
Or C Or Go Or ... Its like people can not look past one language, to understand that with LLMs, a ton of languages gotten way more easy.
Personal experience that running a few subagents parallel with Rust, just slows everything down to a crawl, even wit ccache and a ton of other tweaks. As long as your codebase is a few 100k lines, Rust is ok. But start going in the 500k, 1 million... starting to get C++ flashbacks where your spending more time waiting on the compile jobs, that fight each other for resources then actual testing.
Until Rust can fix their slow and resource munching compiler, its been off the menu for me. Ended up having everything in C and the difference was very noticeable.
While Rust is awesome, a lot of times the only apparent reason to chose Rust over C is virtue signaling.
Update: like I said, Rust is awesome and has so many advantages over C it's not worth a discussion really. But again I see that many projects are choosing Rust not because of merits but simply because it's a fashion.
If you'd said another memory safe language I might have been on board, but C? We've seen a complete collapse in the ability of software to go un-hacked and un-exploited. Not all of that is down to poor memory safety of course, but it's a category of vulnerability that is massively increased in both surface and risk by choosing to write C.
Choosing Rust alone might be virtue signalling, but choosing a memory safe language is just pragmatic.
So magically the Rust was just as fast, but much more readable and when a junior (or an LLM) modifies it slightly so that the output container is bigger it won't suddenly segfault due to that scary in-place conversion loop now being a bounds miss, the Rust won't attempt the in-place conversion now that it can't work.
Firstly the obvious, LLMs aren't perfect yet, they still make mistakes.
But even if they could achieve safe C by asking them to "Write Safe C, Make no mistakes", there would still be a significant benefit to achieving safety by using Rust or a memory safe language.
LLMs work best when they have a combination of clear objective functions and guardrails. In this case, the compiler acts as a guardrail to keep it safe while trying to solve the problem that needs solving.
Having a compiler step prevents the LLM taking a "shortcut". Despite models improving, they will still take shortcuts, sometimes absurd ones, to meet some definition of a "job done".
The other massive benefit is cost and speed. Having an LLM analyze and reason about the safety is expensive and wasteful. Having this done at compile time is cheaper and quicker. The earlier in the feedback loop, the cheaper it will be.
Save your tokens for "real" work, not stuff you can check at compile time.
Rust protects you from a whole class of bugs (borrow checker) and lets you enforce almost as many invariants as you chose, through the type system.
LLM are just as bad as humans at enforcing/tracking ownership and invariants.
We maintain two code bases; one is TS and the other Rust.
Both are by now written foremost by agents (since August 2025, I'd say).
We had numerous non-(business)-logic bugs in the TS code base over this period.
Zero such bugs in Rust. Because the compiler catches them for us. All bugs we had in that code where actual logic bugs. I.e. the spec already had the bug or contained an oversight.
Zero.
We have since started rewriting the TS code base in Rust and will likely switch over in the next three months.
Quite the opposite. Outside of a couple of very specific kind of cases (for example working with microcontrollers not supported by Rust toolchain), there is no good reason to choose C when you could choose Rust.
Choosing C over Rust means throwing away the memory safety features that Rust gives you for free. And for what? There is just no upside to choosing C in the vast, vast majority of cases.
Rust checked arithmetic is built into Rust's standard library (std) via explicit methods on primitive integer types (such as checked_add, checked_sub, checked_mul, and checked_div), which return Option<T> and evaluate to None on overflow. There's a known tradeoff of some speed, which is fine for my purposes because I need reliability more than raw speed.
I'm longtime C coder so I know how to do this in C. Rust strong types and static analysis make it so much easier to do correctly.
Seems like just virtue to me. The guy who volunteers every week to pick up garbage from the beach isn't virtue signalling, he's just virtuous. The guy who came once, with a film crew and left once they'd got the shots they wanted - that is virtue signalling. They're both shown in the film, but only one of them actually does the signalled virtuous activity when the cameras aren't rolling.
You could argue that RIIR was conducted as virtue signalling if what we saw was one of those translations smeared in unexplained unsafe blocks, with rats nest logic that no human is going to untangle. But that doesn't seem to be the case here.
That seems like an extremely dubious claim.
Rust is just a lot more pleasant to work with in every possible way. C has the abstractive capabilities of a deaf-blind toddler.
Even very basic abstractions possible in basically every language, like say a type that holds JSON text, is not possible in C.
For example, you cannot even write type-safe generic data structures in C. You just have to muck around with void pointers. You might reach for macros to make it "nicer" to use, or write "type-safe" functions that cast for you, but that's mostly a veneer.
Developers rewrite code in rust for memory safety, not speed.
* Memory safety
* Performance
* Logical correctness
People definitely rewrite in Rust for speed. To be honest I would say memory safety has been overegged as an advantage - yes obviously you want memory safety, but I think some people think it's the only advantage Rust has, when logical correctness (due to the stronger type system) is definitely in the same league.
But in many cases a rust version already exists (like for tokenization etc.). I wouldn't advise to write it yourself if a supported repo is already doing it.