I certainly basically mentally execute code to some extent as I write it. Is there a way to program that doesn't do something like that?
I certainly basically mentally execute code to some extent as I write it. Is there a way to program that doesn't do something like that?
The only thing you can do is run your code and get an empirical answer to the question 'Is it fast enough'.
Most common instructions in modern CPU cores are decoded directly into the corresponding micro-operation(s), without involving the microcode ROM. Only the most complex instructions, involving many micro-operations, will use the microcode decoder. Also, there are often several copies of the simpler instruction decoder, so the CPU can decode several instructions in a single cycle, while there's normally a single copy of the complex (microcode-using) instruction decoder.
A good resource if you want to read more is part 3 of https://www.agner.org/optimize/ which describes the decoder (and other parts) of several families of x86 processors.
(I too execute the code in my head, but these days usually a couple layers above x86 ASM, in my own mental "bytecode" that tracks how expensive are some of programming language's operations and stdlib functions.)
An example - some code I was working with gave subtly different results on two processors. It turned out this was because one of them implemented FMA - Fused Multiply Add.
https://en.wikipedia.org/wiki/Multiply%E2%80%93accumulate_op...
It wasn't particularly painful to find this, but if you were attempting to debug this kind of problem without considering lower level issues, you'd spend a whole lot of time banging your head against a wall.
I think if you're talking micro-code fusing then Intel does guarantee it has exactly the same semantics as a separate multiply and add.
Interesting fact - I believe modern Intel architectures actually only have fused multiply add. If you do just a multiply it'll do a fused multiply and add zero.
Logic and relational programming (Prolog, SQL) comes pretty close. "Here's some rules about the data. Execute."
I remember university classes in Prolog. Solving logical puzzles tended to be declarative, but trying to write any actual program involved shifting to the mindset that you're dealing with a regular programming language with a built-in DFS engine underneath (the same way programming in JS involves learning you have a hidden event loop running in the background). For practical use of SQL, you need to be able to choose between equivalent queries based on how the database engine actually turns them into lookups.
Maybe 30 years ago. It's really not so important nowadays. Computers are fast.
> For practical use of SQL, you need to be able to choose between equivalent queries based on how the database engine actually turns them into lookups.
Maybe there are some cases where you need to, but there are also million-dollar businesses that have succeeded while only ever writing their SQL queries as naively as possible. The notion that all non-toy practical uses need to understand such low-level details is just utterly false.
Citation needed. Given engineering staff sizes and the amount of performance problems RDBMSes have, that seems highly doubtful.
But in regards to "Is there a way to program that doesn't do something like that?", I would say relational is that way.
You must be psychic if you can figure out what the DB engine is going to do without running it.
Bitmap scan, parallel index scan, merge join, nested loop join, etc. I always have to try and see.
You don't necessarily have to know whether an index scan is going to be parallelized or whether the CBO is going to switch to a loop because its heuristics think your result set is likely tiny to think about things like "is this data being accessed via the right indexes? Is the data likely in memory or not? Will my joins, given my consistency level, impose locks or concurrency considerations on the tables I expect?"
- Guess what I think is going to happen
- EXPLAIN ANALYZE
- Ohhh, it did [thing]. Lemme tweak if I can convince it to do [other thing] that should work better
- <Repeat>
I'd argue testing against the DB is a core part of the process for writing a query.