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
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions base/ryu/Ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,15 @@ neededdigits(::Type{Float64}) = 309 + 17
neededdigits(::Type{Float32}) = 39 + 9 + 2
neededdigits(::Type{Float16}) = 9 + 5 + 9

"""
Ryu.neededfloatdigits(T)

Number of digits necessary to represent type `T` in shortest precision.
"""
neededfloatdigits(::Type{Float64}) = 24
neededfloatdigits(::Type{Float32}) = 15
neededfloatdigits(::Type{Float16}) = 20

"""
Ryu.writeshortest(x, plus=false, space=false, hash=true, precision=-1, expchar=UInt8('e'), padexp=false, decchar=UInt8('.'), typed=false, compact=false)
Ryu.writeshortest(buf::AbstractVector{UInt8}, pos::Int, x, args...)
Expand Down Expand Up @@ -111,16 +120,16 @@ end

function Base.show(io::IO, x::T, forceuntyped::Bool=false, fromprint::Bool=false) where {T <: Base.IEEEFloat}
compact = get(io, :compact, false)::Bool
buf = Base.StringVector(neededdigits(T))
buf = Memory{UInt8}(undef, neededfloatdigits(T))
typed = !forceuntyped && !compact && Base.nonnothing_nonmissing_typeinfo(io) !== typeof(x)
pos = writeshortest(buf, 1, x, false, false, true, -1,
(x isa Float32 && !fromprint) ? UInt8('f') : UInt8('e'), false, UInt8('.'), typed, compact)
write(io, resize!(buf, pos - 1))
write(io, view(buf, 1:pos - 1))
return
end

function Base.string(x::T) where {T <: Base.IEEEFloat}
buf = Base.StringVector(neededdigits(T))
buf = Base.StringVector(neededfloatdigits(T))
pos = writeshortest(buf, 1, x, false, false, true, -1,
UInt8('e'), false, UInt8('.'), false, false)
return String(resize!(buf, pos - 1))
Expand Down
7 changes: 7 additions & 0 deletions test/ryu.jl
Original file line number Diff line number Diff line change
Expand Up @@ -122,18 +122,21 @@ end
# 64-bit opt-size=0: 52 <= dist <= 52
# 64-bit opt-size=1: 52 <= dist <= 59
@test "2.900835519859558e-216" == Ryu.writeshortest(todouble(false, 307, 0))
@test "-2.900835519859558e-216" == Ryu.writeshortest(todouble(true, 307, 0))
# 32-bit opt-size=0: 51 <= dist <= 51
# 32-bit opt-size=1: 51 <= dist <= 59
# 64-bit opt-size=0: 52 <= dist <= 52
# 64-bit opt-size=1: 52 <= dist <= 59
@test "5.801671039719115e-216" == Ryu.writeshortest(todouble(false, 306, maxMantissa))
@test "-5.801671039719115e-216" == Ryu.writeshortest(todouble(true, 306, maxMantissa))

# https:#github.com/ulfjack/ryu/commit/19e44d16d80236f5de25800f56d82606d1be00b9#commitcomment-30146483
# 32-bit opt-size=0: 49 <= dist <= 49
# 32-bit opt-size=1: 44 <= dist <= 49
# 64-bit opt-size=0: 50 <= dist <= 50
# 64-bit opt-size=1: 44 <= dist <= 50
@test "3.196104012172126e-27" == Ryu.writeshortest(todouble(false, 934, 0x000FA7161A4D6E0C))
@test "-3.196104012172126e-27" == Ryu.writeshortest(todouble(true, 934, 0x000FA7161A4D6E0C))
end

@testset "SmallIntegers" begin
Expand Down Expand Up @@ -304,6 +307,7 @@ end
@test "1.00014165e-36" == Ryu.writeshortest(1.00014165f-36)
@test "200.0" == Ryu.writeshortest(200f0)
@test "3.3554432e7" == Ryu.writeshortest(3.3554432f7)
@test "-1.00000075e-36" == Ryu.writeshortest(-1.00000075f-36) #longest Float32
end

@testset "LooksLikePow5" begin
Expand Down Expand Up @@ -368,6 +372,9 @@ end
# return fails / (fails + success)
# end

@testset "Regression" begin
@test "-0.00010014" == Ryu.writeshortest(Float16(-0.00010014)) #longest Float16
end
end # Float16

@testset "writeshortest(::AbstractVector, pos, ...)" begin
Expand Down