Remix.run Logo
jdw64 5 days ago

Knuth's warning about premature optimization is really about not increasing complexity based on guesswork without profiling the actual bottleneck. That's about overall architecture design. What's happening here (in this blog post) is locally for the sake of learning, and further, for intellectual fun - I think it's completely justified. Usually, the 'don't do premature optimization' quote gets misused as an excuse to avoid careful design, but setting that aside, learning within these kinds of constraints and eventually producing something. that's not premature optimization in my view.

Looking at this code, they saved one AND instruction and reduced a pipeline stall, but it seems like it would be harder for a future maintainer to understand, because not_received feels a bit less readable. I always think code that's easy for the computer to read and code that's easy for humans to understand are different things.

After writing my comment, I realized it came across as overly critical. But actually, I think this work is completely justified and beautiful. Honestly, it's at a level I couldn't achieve myself. I respect it.

srean 5 days ago | parent | next [-]

Premature optimization is that occasional dessert we serve ourselves because it tastes so good.

That said, I agree with you that the maxim is often used to justify poor planning or absence of planning. Premature pessimization is bad too.

We don't have the bandwidth to test every micro-decision. That's what the learning of domain specific principles are for. Then some choices you reason through, some you test and when confronted with a perf problem, test those reasoning based choices that benchmarks point fingers at.

jdw64 5 days ago | parent [-]

On top of that, I use it as a way to express jealousy when I see impressive optimization work. "Don't do premature optimization like this (I can't do it myself)."

benj111 5 days ago | parent | prev | next [-]

Is this about premature optimisation or just good architecture?

The number of data formats I see that miss obvious things like alignment etc. it isn't something you can easily change later if it becomes widely used.

To me this post represents the minimum you should be thinking about when designing any kind of data structure/format.

The only time where I would say it strays into premature is inverting the received flag, but only because you're optimising it for a particular processor and in a way that isn't particularly obvious. The inversion can easily be hidden behind a function call.

jdw64 5 days ago | parent [-]

>The number of data formats I see that miss obvious things like alignment etc.

After reading that sentence, I felt a little guilty. It's actually a basic principle of design, but in practice, I just don't pay much attention to it and only write code for readability

benj111 5 days ago | parent [-]

It doesn't even have to impact readability.

See for example

https://github.com/MayaPosch/NymphRPC/blob/master/doc/nymphr...

There's an int8 in the middle of int32s knocking everything out of alignment. And it doesn't need to be because flags is int32 and only uses 4 bits.

Changing that doesn't have to impact readability. Makes it easier to process, makes a struct representation more compact.

jdw64 5 days ago | parent [-]

Cool

TacticalCoder 5 days ago | parent | prev [-]

> Usually, the 'don't do premature optimization' quote gets misused as an excuse to avoid careful design ...

The "don't do premature optimization" mindset is the reason why we've got monstrous Electron apps doing jack shit.

I hate that quote, I hate that mindset. It's the reason everything is bloated and sucks.

sgarland 5 days ago | parent [-]

Everything would still be bloated, it’s just that the people doing the bloating point to that quote as though it’s defending their position.

What kills me is that a lot of the stuff just isn’t that hard. You need a data structure that you’ll later check membership of? Use a set. Might a list / array be more front-of-mind? Yes. But if you don’t need it, why? Is it noticeably slower? Not really. Is it objectively the correct answer? No, and it costs you essentially nothing, so just use the correct one.