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.
@meltedcheese For example - the STL is bad. I hate it. I always create my own versions that do the good thing (and so do plenty of other people .e.g. EASTL etc).
BUT I have learned the hard way that you need to be reasonably close in syntax and terminology to the STL, otherwise people get quickly lost or tripped up.
@TomF I think there are a few different schools of PL design that sometimes have little to no intersection:
personally, i'm partial to all three (i like rust because it does all of those things! and because i'm tired of writing embedded C) but the third one is my home domain. i want to build towards better ways of thinking, which coincidentally involves a lot of programming language development. but i also do this with libraries, it's not really essential that it's a language
@TomF I don't disagree in principle but you're talking to someone who was hired as a junior webdev for the second real job and immediately went "I'll make a programming language to solve this problem!" and everyone genuinely loved the result (which did solve business objectives)
then I did it two more times or so
@TomF @rygorous metaprogramming: i can only stand it once i have a pipeline that keeps a perfect trace of source locations for each element: where did the original expression come from, and where, if any, is the macro that expanded it? (which has its own trace but you don't need this information in the initial report, just the point of entry)
the problems are at transition points, e.g. when generating C code. i can only specify a # line, so filename + line. no column, let alone a trace.
@TomF @rygorous the other thing is, if there is evaluation then there is intermediate state, and when this evaluation happens at compile time, then some problems need a compile time step debugger (or at the very least compile time printf debugging.)
so one either needs to turn the program into a program that generates a program, so existing debugging facilities can be used, or one avoids compile time expressions completely and lets the existing optimizer infrastructure deal with it.
@lritter @breakin @TomF @rygorous I'm a big fan of code generation in general. It can definitely have its problems, but sometime a DSL or regenerating some tedious spec shit from a schema can save a lot of work and thinking, and still give you a nice clean interface/fast code to work with, if you are disciplined about it.
C and C+ are great targets for that.