Skip to content

reduce allocations for string(::IEEEFloat) #57977

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from

Conversation

oscardssmith
Copy link
Member

@oscardssmith oscardssmith commented Apr 1, 2025

julia> @time string(1.0)
  0.000004 seconds (3 allocations: 432 bytes) # before
  0.000007 seconds (3 allocations: 112 bytes) # after
julia> io = IOBuffer();
julia> @time show(io, 1.0)
  0.000014 seconds (3 allocations: 432 bytes) # before
  0.000012 seconds (1 allocation: 48 bytes) # after

mitigates #57976

@JeffBezanson JeffBezanson added the performance Must go faster label Apr 1, 2025
@bvdmitri
Copy link
Contributor

bvdmitri commented Apr 2, 2025

Can this PR also fix Printf.format in a similar manner? Currently it does allocate an intermediate array too and works almost in exactly same way as the show method. With the Printf.format though by using some internal API it is possible to do non-allocating printing of a number

julia> buf = Vector{UInt8}(undef, 10)
10-element Vector{UInt8}:
 0x69
 0x09
 0x00
 0x00
 0x00
 0x00
 0x00
 0x00
 0x6a
 0x09

julia> format = Printf.format"%.4f"
Printf.Format{Base.CodeUnits{UInt8, String}, Tuple{Printf.Spec{Val{'f'}}}}(UInt8[0x25, 0x2e, 0x34, 0x66], UnitRange{Int64}[1:0, 5:4], (%.4f,), 1)

julia> @time Printf.format(buf, 1, format, 1.0)
  0.000016 seconds
7

However, the signature of format is constrained to Vector and cannot use the Memory

@oscardssmith
Copy link
Member Author

I think printf should be left to a separate PR. The last thing to do here, is I would appreciate someone double checking that the numbers I've put down for neededfloatdigits are correct (and ideally find the formula that generates them). I believe these are the right numbers, but my search for them wasn't exactly principled)

@KristofferC
Copy link
Member

KristofferC commented Apr 6, 2025

Would be good with tests for the cases where the constants were wrong since that seems like good edge cases to test.

@oscardssmith
Copy link
Member Author

tests added.

@oscardssmith oscardssmith force-pushed the os/reduce-float-to-string-allocs branch from 5b631b7 to 6176380 Compare April 22, 2025 21:24
@oscardssmith
Copy link
Member Author

good to merge?

@KristofferC
Copy link
Member

Test Failed at /Users/julia/.julia/scratchspaces/a66863c6-20e8-4ff4-8a62-49f30b1f605e/agent-cache/default-macmini-x64-5.0/build/default-macmini-x64-5-0/julialang/julia-master/julia-a03f845f70/share/julia/test/ryu.jl:310
  | Expression: "-1.00000075f-36" == Ryu.writeshortest(-1.00000075f-36)
  | Evaluated: "-1.00000075f-36" == "-1.00000075e-36"
  |  

So, I guess, no?

@oscardssmith
Copy link
Member Author

oops. Tests now look a lot happier.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
performance Must go faster
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants