macintosh.world | Log In | Register

Today | News | Books | Recipes
Notes | QuickTake | Wiki | Browse
Maps | Reference | Reddit | YouTube
Chat | Games | About

Back to HN

Fast-Anything: Don't rewrite it in Rust. Rewrite the 5% that's slow

by BaselAshraf81 | 63 points | 40 comments | 2026-11-21 22:10:09 Central

Open Source Link | Read Source Here

Open on Hacker News

Comments

ThouYS
neat, but in my experience bottlenecks that you could
solve this way are low hanging fruits and optimized away
quickly.the true performance hog is like sand, spread
across the system, a little bit in every place.

  > sham1
A lot of lost performance also comes from things like
the architectural decisions and technical debt that
would pile up over time. These considerations are also
like sand in that they're coarse, rough, irritating,
and they get everywhere in the codebase.Oh, and they
can be annoying to refactor later on.

hendler
It's also OK to rewrite in Rust because it's not just
about performance. The fast part in other Python, Ruby,
etc may be written in C/C++, or call an external library,
and be the vulnerable part.

tomp
Or just rewrite in Numba.Most often, Python is enough.
diegocg
Better yet, stop writing large codebases in lenguajes that
will have performance troubles.

  > tripledry
Genuine question, is this a common problem people see
out in the wild? In my dayjob(s) I've basically never
seen major slowness that comes from using something
like nodejs / python, the bigger culprits have been
network / db / bad architecture or code.

    > > afavour
In my experience memory issues are more common.
The way JS stores objects in memory is
fundamentally inefficient (though it's deliberate,
it's why you can do object.propertyIJustMadeUp =
true on anything) and if you're moving a ton of
data it can cause issues eventually.The only
JS-native answer is to use typed arrays, which is
using a sledgehammer to crack a nut.

    > > austin-cheney
Maybe. When I stress test my JavaScript
application I can push up to 6,000,000 WebSocket
messages per second in Node (C++) but do
experience some amount of memory instability. When
I run the same application on Bun (Rust) it's not
as fast but it's extremely stable.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.

    > > andyferris
Yeah, Python is one I where I find moderate
amounts data can be a pain - I end up tempted to
move CPU work to the database or precompute more
things, and it's hard to get the advantages of
single binary programs (for deployment, startup
time, etc).(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).

    > > fiedzia
Mostly because anything that needed to be fast
already was written in a language that allows that
and most of web/app development focuses on
shuffling small amounts of data around. Also pure
Python is worse than node in this aspect. Still,
there many non-trivial cases. There is a reason
most of infrastructure moves to Rust. I've seen
poetry spending 30min (!) on dependency resolution
once. Test runners and some background/nightly
data processing if often first place where you
realize you may want to rewrite.

    > > chrismorgan
Performance is broader than just slowness; it's
also CPU and memory usage, things which will force
you to scale horizontally (multiple machines) much
sooner.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.

    > > FartyMcFarter
Yes. Python is usually 10-100x slower than
languages like C, C++ or Rust. If your program is
CPU bound, this shows up very obviously. If it
isn't, you may never notice at all.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.

      > > > rhdunn
Libraries generally write hot code in
languages like C, C++, or Rust and bind that
to Python (PyTorch, etc.) or provide
bindings/wrappers around libraries in those
languages (SQLite, PostgreSQL, etc.).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.

      > > > cadamsdotcom
What you're saying is valid too; but, the post
you're replying to implied most slowness is
network (i/o bound) or bad architecture
(accidental cpu bound, fixable by tech debt
cleanup).

        > > > > FartyMcFarter
That post was also asking if other people
have seen language mattering. In the
places where I've worked programs are
often CPU bound. Places like these are why
compiled languages are still alive and
kicking.I'm not denying that domains exist
where being I/O bound is a thing. For
those cases Python can be a fine choice.

          > > > > > cadamsdotcom
We all have different lenses on the
makeup of the entire set of software
out there, it's likely you're coming
from that experience and the other
post was coming from experience mostly
with i/o bound software.For cpu bound
stuff the language makes potentially
an enormous difference; but then you
get to trade off: eg. if you're not
gonna need a fast language you are
free to choose based on other
considerations.Might still both pick
the same language though ;)

            > > > > > > pessimizer
This conversation is pointless.
The answer to whether some people
see actual problems in the real
world© with using very slow
interpreted languages is "yes."I'd
make the case that everything on a
computer is CPU bound depending on
scale, even your crud app. Half
the time when somebody is saying
that X is I/O bound, they actually
mean that it's bound by the
performance of their database -
which wisely, in the real world,
is not written in python.edit:
and, of course, things in python
that would run terribly slow if
written in python become
primitives, which are written in
C. Python is a C program.

            > > > > > > pdhborges
The easiest way to have an I/O
bound workload is to use fast
languages.

tialaramex
Note that this strategy only works if 5% of your software
is the problem - because of Amdahl's law.

perdy
We took this route in a Python web framework I maintain:
route matching, JSON encoding, request body parsing,
compression and some other specific parts moved to Rust
with PyO3, everything else stayed in Python.

  > krmmalik
How do you feel about your decision now that this has
been done? Was it worth it or not?

afdbcreid
Funny that this codebase is 62% Rust, 9% Python. And I'm a
huge Rust proponent.

Bolwin
The results section feels a bit like a joke. They rewrote
C and C++ to Rust for performance?And something tells me
C# and Java could have gotten 90% of the improvement
without any rewrite. I'll have to try later today.

anttiharju
Hah, pretty neat!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.

  > combobyte
Doesn't that kind of defeat the purpose? Compiling to
WASM is going to erase any speed advantage you'd get
from native Rust.

    > > himata4113
wasm compiled to native is only ~62% slower (LLVM)
and only 3% slower for non memory bound
operations.

searealist
This cope was common 20 years ago with Python. It rarely
worked out. It's rare that 5% of your program is taking up
that large a percentage of your execution. It's usually
much more spread out than that.Also, we have LLMs now.
Rust should be the default for many things.

  > benjiro29
"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.

egorfine
What does %LANGUAGE% have anything to do with the goal?
Totally same principles apply for any other lower level
language: Zig, C.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.

  > eterm
> the only apparent reason to chose Rust over C is
virtue signalingIf 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.

    > > tialaramex
In this particular topic (performance) Rust
matters because it will deliver excellent
performance. You could write a container -> map ->
map -> container flow in fifty memory safe
languages, but probably it would be a bit slower
than the nasty hack you'd write in C to convert
the data in place. Rust isn't, if you're intrigued
you will discover that Rust 1) figured out that
the two containers must be the same size in memory
so it can re-use the original container and 2) did
the in-place conversion even though that wasn't
what you wrote, because it's faster.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.

    > > curt15
In the age of machine-generated code, why not ask
LLMs to write "safe C"?

      > > > eterm
You've received downvotes, but I'll do you the
kindness to actually answer you.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.

  > QuantumNomad_
> a lot of times the only apparent reason to chose
Rust over C is virtue signaling.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.

  > virtualritz
That's a pile of bollocks.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.

  > jph
Today I added Rust checked arithmetic which detects
and protects math bugs such as integer addition
overflow and division by zero.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.

    > > Conscat
This is in the C23 standard library as well, just
without nice sum types sadly.

  > booi
That's actually why we chose Rust over C at my new
company, Virtual Signaling as a service™. We rewrite
anything in Rust for companies looking to virtue
signal.

  > jstanley
Rust's incredibly strict and detailed static analysis,
while a huge headache for human developers, is really
useful with LLMs.

  > IceDane
> While Rust is awesome, a lot of times the only
apparent reason to chose Rust over C is virtue
signaling.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.