JuliaLang/julia — comment on commit 3f753435f137cd39afb0ffbdf4fc777efb03c6dd
File:  line 0
By StefanKarpinski:

> Does "%d" ever give different output than int2str? I don't mean general %d, but the exact format string "%d".

I suspect the output is the same. But there is no reason at all that the code emitted for `%d` can't be exactly the code that implements `int2str`. Currently it's not for only two reasons:

- I wrongly assumed that the overhead from calling write lots of times would be less than the overhead from writing to an intermediate buffer and then calling write once at the end. Easy enough to change.
- It handles non-integer values also. People *are* going to pass values of the wrong type to printf expressions because this is a dynamically typed language they don't want to have to think about writing `printf(f"%f", float(1))` instead of `printf(f"%f", 1)`. We can certainly add the cast for them, and that would be easy, but why not just print all values correctly? One call to `isfinite` versus one type conversion. I doubt there's much difference.

Moreover, if the generated printf code gets inlined into a context where the values passed are *known* by inference to be of integers, then the `isfinite(x)` call is always false and the check can be completely eliminated. I'm not sure whether our inlining is good enough for that at the moment, but it's certainly not an insane idea.