6 ms·
> What if d is a constant, but not known to the compiler? Then you can use a library like libdivide. Or you can use a JIT compiler ...
by amelius 5y ago
> What if d is a constant, but not known to the compiler? Then you can use a library like libdivide.
Or you can use a JIT compiler ...
- eloff 5y agoThere are lots of JIT compilers out there, I don't know if any that would detect the divisor is a constant at runtime and specialize code for that. I think it's almost never done. In real life JIT compilation can't take long so the JIT compiler leaves a lot of possible optimizations on the table, just covering the low hanging fruit.
- 10000truths 5y agoAllocating executable pages and running a compiler is orders of magnitude slower than just precomputing the multiplication factor.
- anfilt 5y agoNot the op your responding too, but If this bit of of code is gonna run many billions of time it could very well be worth that cost.
- foota 5y agoIn a lot of cases you're working with code where you're storing divisors as a part of a data structure, so a JIT isn't really going to help with that.
- olliej 5y agoThere are plenty of times when you’re processing data where you load what is a runtime constant value but not a compile time constant. I mean the extreme case is obviously a scripting language , where lowering to multiplication may well be possible. That said you are correct in a JIT being vastly more heavyweight than most cases would need - but mostly due to the cost of making an indirect call rather than the cost of the jit itself
- jheriko 5y agohow does it know the value remains constant? this is annoyingly non-trivial, and exactly the sort of thing that gets culled for JIT performance. the problem isn't generating code properly targeted at the hardware, or unrolling late-binding cycle wasting.