https://www.youtube.com/watch?v=UBhT7nbWpMg


So if you're writing Python, you do need to understand what will become a dictionary, and roughly how duck-typing works.
If you're writing a JITted language, you should roughly understand how the JITter works.
If you're writing C# or other managed language, be aware of how the GC works, and you should be able to write the equivalent C++ code without too much trouble.
Writing C++ code? You should be able to mentally translate this into C. Where are the creator/destructors happening, and what do they turn into? Is that function call virtual? How does that work in practice, and what are the perf implications?
If you're writing C, you really should be able to read assembly, and know what instructions are available, how flow control turns into branches, what a cache line is, what happens when you run out of registers.
And if you're writing assembly, you should absolutely know the principles of branch prediction, micro-ops, how atomic instructions work, and M(O)ESI(F).
But there's no need for a Python coder to know the whole stack. Even if they do (like I happen to) - if they try to think about them all at once, their head will explode!
@Doomed_Daniel It's a rule of thumb. In general, I find that most C++ perf problems are because you're doing too many alloc/frees, and too many virtual calls. Those are solved by thinking about C.
Yes, if you then discover you're thrashing caches or causing too many branch mispredictions, then that's actually a good problem to have - it means your code is faster than almost all other C++ code. Well done.
@TomF re translating C++ to C: this would have been pretty much trivial to do back in the days of Cfront.
I think it may still even be possible with compilers that use the EDG front end...? (Or at least, if you get the front end directly from EDG, it has a C-generating back-end just like Cfront did.)
But if you're using, say, Clang, you probably want to think in terms of LLVM IR, or possibly the new ClangIR (and its translation to LLVM IR).
@Doomed_Daniel It's a rule of thumb. In general, I find that most C++ perf problems are because you're doing too many alloc/frees, and too many virtual calls. Those are solved by thinking about C. Yes, if you then discover you're thrashing caches or causing too many branch mispredictions, then that's actually a good problem to have - it means your code is faster than almost all other C++ code. Well done.
@JamesWidman @TomF
other bad news: other compilers are still relevant :-p
MSVC still is dominant on Windows and so is GCC on Linux
@TomF I think that was generally true 25 years ago and I think itâs still true in some cases now, but for most programmers working on most software it just isnât necessary anymore, and often isnât even possible to do well.
Cheap computers and reasonable abstractions have made it largely unnecessary while complex architectures, sophisticated compilers and large libraries/frameworks have made it largely infeasible for most.
@TomF I read the rest of the thread before I replied. My reference to framework complexity was meant to address that my comment wasnât just about assembly.
Modern systems, at every level from âwhat the compiler doesâ to âwhat the hardware doesâ are often too complex to justify the cost of understanding the next level down more than superficially, and sufficiently robust to not require that for most uses.
@MartyFouts To clarify - I don't mean you should *actually* know what the compiler is going to do. That is hard, and most compilers don't actually go through those intermediate stages.
I mean that when you're writing a piece of code in a language, you should be able to *in theory* write the same code using the next simpler language. Now, you're choosing not to do that for efficiency - but you COULD. And knowing what that version would look like will inform how you write the real code.
@TomF Yes, I got that. I donât agree. At least not for most programmers, most of the time.
understanding the semantics of the language at the level of abstraction of the language and its model has been enough for most programmers and problems for at least 50 years.
There are exceptions, of course. But what used to be a requirement for everyone when I started 50 years ago is now the exception rather than the rule.
@TomF Ironically, some of the slowest ugliest code I have had to debug is in compilers. đ
But as Knuth pointed out long ago, the fastest way to slow code is premature optimization (but thatâs far from the point of this discussion.)
@MartyFouts Well that's a whole other discussion, on which I and many others are perfectly comfortable with Knuth being wrong.
It is the act of thinking one level down, that ensures that people don't end up with a mad panic scramble to optimise inherently slow code at the end of the project.
@TomF Knuth wrong about the well documented perils of premature optimization? Un huh.
I think, perhaps, from your last sentence you donât understand the nuances of Knuthâs point, which was mostly an expansion on Tony Hoare, anyway.
Knuth never argued to wait until the end to optimize, by the way.
And long experience tells me that concentrating one level down rarely works as well as concentrating on good abstraction before writing code.
@TomF Your first few replies to me in the thread were assertions that I didnât understand you.
You said something that suggests you donât understand Knuthâs argument about optimization when you talked above panic optimization. How is pointing out that thatâs not something Knuth argued âshittyâ but claiming I didnât understand you not?
I donât question your depth of experience, only argue that it no longer applies as broadly as it did when you started.