Public, allocation-free way to write a `Float64` as `print` does?

I’m writing large XML files (XLSX.jl’s streaming writer) and need to write millions of Float64s into an IO or a byte buffer. The text has to be what print gives: the shortest decimal that reads back as the same value, e.g. 0.1 rather than 0.10000000000000001.

print(io, x) produces the right text, but it allocates on every call:

julia> io = IOBuffer(); f(io, xs) = foreach(x -> print(io, x), xs);

julia> xs = rand(10^5); f(io, xs); take!(io);

julia> @allocated(f(io, xs)) / length(xs)   # bytes per float
454.1

That’s about 450 bytes per float, and in our benchmark it was a large share of the total allocation.

So far, I’ve looked at:

  • Base.Ryu.writeshortest(buf, pos, x) - This does exactly what I want, writing into a caller’s buffer without allocating. But it’s internal: Base.ispublic(Base.Ryu, :writeshortest) == false. Also, I’m not yet fully convinced it covers the necessary range of floats Excel uses.
  • Printf.format(io, Printf.Format("%.17g"), x) - This is public, but it allocates even more (about 500 B) and doesn’t give the shortest round-trip form.
  • Parsers.jl - Perhaps unsurprisingly, these only parse!

For now, I have a small workaround of our own for floats with up to 15 significant digits. It checks that m / 10^d == x using exact float division and falls back to print otherwise. It works (allocations down to ~30 bytes per float), but it’s a partial reimplementation of something Base already does.

So:

  1. Is there a public API (in Base, a stdlib, or a package) that writes a float’s shortest representation into an IO or buffer without allocating?
  2. If not, is Base.Ryu.writeshortest stable and wide enough in practice to rely on, or would it be reasonable to propose making it public?

Thanks!

It’s not Base’s public, but just in case, have you checked BufferIO.jl?

using BufferIO
io = BufWriter(IOBuffer())
write(io, 5.0)

See this post from the package author using integers.

Thanks! I tried it with BufferIO.jl 0.3.4 on Julia 1.13.3. write(io, 5.0) throws a MethodError (0.3.0 removed writes of primitive types other than UInt8), and so does write(io, 5). print(io, x) works, but it forwards to Base’s print via IOWriter, so it allocates the same:

BufWriter print: 495.6 B/float
IOBuffer print:  493.9 B/float

For now, maybe I’ll have to stick to my bespoke approach - but I think question 2 is still open: is Base.Ryu.writeshortest safe to rely on, or worth proposing as public?

You can use BufferIO to get the buffer, then use pointer ops to store/load from the buffer. I’m in the process of adding non-byte IO to BufferIO

XRef: Commit to a stable set of methods for writing floats (shortest, fixed, exp) · Issue #59763 · JuliaLang/julia · GitHub

I think there needs to be a better way to use non public functions in packages. Currently there are public functions that are stable, and internal functions that might change in any way at anytime. A nice middle ground could be experimental functions that will not change behavior in a breaking way, but could be completely removed at anytime. Then if you protect your use of these experimental methods in a if isdefined(Base, ... block you can maybe work around breaking changes more easily.