I looked into and it calls sprintf so I thought it causing it, and seems confirmed but even stranger (how are 5+ allocations swallowed if that’s done implicitly?):
but I’m guessing in some cases the compiler is clever and can optimize it out?
and then for me:
julia> @time Base.show_unquoted(s, x[512])
0.000021 seconds (3 allocations: 104 bytes)
note, if repeated, not consistent, but seemed to be in your case:
julia> @time Base.show_unquoted(s, x[512])
0.000017 seconds (4 allocations: 168 bytes)
julia> @time Base.show_unquoted(s, x[512])
0.000016 seconds (3 allocations: 104 bytes)
In full:
function sprint(f::Function, args...; context=nothing, sizehint::Integer=0)
s = IOBuffer(sizehint=sizehint)
if context isa Tuple
f(IOContext(s, context...), args...)
elseif context !== nothing
f(IOContext(s, context), args...)
else
f(s, args...)
end
String(_unsafe_take!(s))
end
According to @macroexpand the printing happens after performance stats are taken, so we should not see any print related allocs? Here is a benchmark where it is more clear, that printing should not affect the benchmark: