by zodwick · 1w ago · 0 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.- It does not run code behind my back when I access a field.
- I see when a pointer is used, so the syntactic distinction between -> and . is important.
Being able to change this without changing these little things is far less important to me than being able to understand important properties of the code just by looking at it.
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.
#[derive(Debug)]
enum E { A, B, C }
pub fn main() {
use E::*;
let x = B;
println!("{:?}", x);
}Not really Go in general, though. It’s mostly disappeared from my personal projects, in part because of their matching allergy to syntax sugar. I am incredibly tired of writing “if err != nil” blocks, and I don’t understand why there isn’t some kind of “if this function returns a non-nil error, return zero values and the error”. If only I could get something better to make a static binary. By which I mean “I like Clojure and refuse to learn SBCL because… reasons”
I'm not necessarily a "statically link all the things" zealot, but I think that pattern definitely has a place where problems like yours often occur.
Rust's async situation is still a pain in many ways.
But throwing Rayon at something often results in very reasonable parallel performance gains without cluttering things much. It usually won't get you to an optimal solution, but it's good for 80% with 10% of the pain.
The performance oriented (data plane) work happens on dedicated, cpu-pinned threads and sockets outside the tokio runtime. The control plane work happens in tokio async. Interaction between the two is tricky. But decreased latency and increased throughput is valuable. And it's a complex server, so we can't really try too many approaches.
It certainly doesn't help that the data structures are, by necessity, a cephalopod that have to be used across threads.
Everything wants to push you down the path of using `ARC<Thing>` even when `Thing` is immutable or the lifetime can be mostly guaranteed.
Here's an example of that problem. Let's say I want to do 2 things concurrently against the same data set. I might want to write code that looks like this:
let v = vec![1, 2, 3];
let taskA = tokio::spawn(async {
println!("Here's a vec: {:?}", v);
});
let taskB = tokio::spawn(async {
println!("Here's a vec: {:?}", v);
});
tokio::join!(taskA, taskB);
But that immediately gets you in hot water because "async block may outlive the current function, but
it borrows `v`, which is owned by the current function"Now, you and I can clearly see that the async block does not, in fact, outlive the current function. However, AFAIK, there's no real way to convince rust that that is the case.
So now even though `v` is immutable, making this a thread safe operation, we need to introduce unnecessary concurrency constructs or break out unsafe in order to force the language to recognize that "Yes, you can trust me".
For rust to actually be fast with concurrent operations, it needs to be smarter about lifetimes in terms of async.
I thought we as an industry had hopefully moved past "async == magic code-go-fast sauce", but apparently we have not.
As much as Go annoys me, you're right there as well. I have to admit that Go did "solve" the make-concurrency-and-parallelism-accessible (and relatively safe) space in a way that very, very few other programming languages did (and none that people widely use).
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.