Skip to content
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

Fix #5 by using as many BigInt in-place ops as possible to avoid allo… #20

Merged
merged 4 commits into from
Mar 8, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
7 changes: 4 additions & 3 deletions benchmarks/floats.jl
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,11 @@ function bench(n=100_000)
end


function prof(n)
io = IOBuffer("3.2925339999999996e-18")
function prof(str, n)
io = IOBuffer(str)
res = Parsers.Result(Float64)
for i = 1:n
seekstart(io)
Main.Parsers.xparse(io, Float64)
r = Parsers.defaultparser(io, res)
end
end
7 changes: 7 additions & 0 deletions benchmarks/parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,13 @@ run(@benchmarkable Parsers.parse!($l, io, r) setup=(io = IOBuffer("0"); r = Pars
l = Parsers.Delimited(Parsers.Quoted(Parsers.Strip(Parsers.Sentinel(["NA"]))))
run(@benchmarkable Parsers.parse!($l, io, r) setup=(io = IOBuffer("0"); r = Parsers.Result($T)))

run(@benchmarkable Parsers.defaultparser(io, r) setup=(io = IOBuffer("99999999999999974834176"); r = Parsers.Result($T)))
run(@benchmarkable Parsers.defaultparser(io, r) setup=(io = IOBuffer("1.7976931348623157e308"); r = Parsers.Result($T)))

run(@benchmarkable Parsers.defaultparser(io, r) setup=(io = IOBuffer("2.2250738585072011e-308"); r = Parsers.Result($T)))
run(@benchmarkable Parsers.defaultparser(io, r) setup=(io = IOBuffer("0.0017138347201173243"); r = Parsers.Result($T)))


# Tuple{Ptr{UInt8}, Int}
T = Tuple{Ptr{UInt8}, Int}
run(@benchmarkable Parsers.defaultparser(io, r) setup=(io = IOBuffer("0"); r = Parsers.Result($T)))
Expand Down
57 changes: 49 additions & 8 deletions src/Parsers.jl
Original file line number Diff line number Diff line change
Expand Up @@ -9,9 +9,35 @@ function __init__()
for results in RESULTS
Threads.resize_nthreads!(results)
end
Threads.resize_nthreads!(STRINGBUFFERS)
Threads.resize_nthreads!(ONES)
Threads.resize_nthreads!(NUMS)
Threads.resize_nthreads!(QUOS)
Threads.resize_nthreads!(REMS)
Threads.resize_nthreads!(SCLS)
return
end

mutable struct StringBuffer <: IO
data::String
ptr::Int64
size::Int64
StringBuffer() = new("", 1, 0)
end

Base.eof(io::StringBuffer) = (io.ptr - 1) == io.size
Base.position(io::StringBuffer) = io.ptr - 1

const STRINGBUFFERS = [StringBuffer()]

function getio(str)
io = STRINGBUFFERS[Threads.threadid()]
io.data = str
io.ptr = 1
io.size = sizeof(str)
return io
end

"""
Parsers.readbyte(io::IO)::UInt8

Expand All @@ -30,8 +56,9 @@ readbyte(from::IO) = Base.read(from, UInt8)
peekbyte(from::IO) = UInt8(Base.peek(from))

function readbyte(from::IOBuffer)
@inbounds byte = from.data[from.ptr]
from.ptr = from.ptr + 1
i = from.ptr
@inbounds byte = from.data[i]
from.ptr = i + 1
return byte
end

Expand All @@ -40,6 +67,18 @@ function peekbyte(from::IOBuffer)
return byte
end

function readbyte(from::StringBuffer)
i = from.ptr
s = from.data
from.ptr = i + 1
GC.@preserve s unsafe_load(pointer(s, i))
end

function peekbyte(from::StringBuffer)
s = from.data
GC.@preserve s unsafe_load(pointer(s, from.ptr))
end

"""
Parsers.fastseek!(io::IO, n::Integer)

Expand All @@ -48,8 +87,8 @@ end
function fastseek! end

fastseek!(io::IO, n::Integer) = seek(io, n)
function fastseek!(io::IOBuffer, n::Integer)
io.ptr = n+1
function fastseek!(io::Union{IOBuffer, StringBuffer}, n::Integer)
io.ptr = n + 1
return
end

Expand Down Expand Up @@ -312,7 +351,7 @@ function parse end
function tryparse end

function parse(str::AbstractString, ::Type{T}; kwargs...) where {T}
io = IOBuffer(str)
io = getio(str)
res = parse(defaultparser, io, T; kwargs...)
return ok(res.code) ? res.result : throw(Error(io, res))
end
Expand All @@ -321,7 +360,7 @@ function parse(io::IO, ::Type{T}; kwargs...) where {T}
return ok(res.code) ? res.result : throw(Error(io, res))
end
function parse(f::Base.Callable, str::AbstractString, ::Type{T}; kwargs...) where {T}
io = IOBuffer(str)
io = getio(str)
res = parse!(f, io, Result(T); kwargs...)
return ok(res.code) ? res.result : throw(Error(io, res))
end
Expand All @@ -331,15 +370,17 @@ function parse(f::Base.Callable, io::IO, ::Type{T}; kwargs...) where {T}
end

function tryparse(str::AbstractString, ::Type{T}; kwargs...) where {T}
res = parse(defaultparser, IOBuffer(str), T; kwargs...)
io = getio(str)
res = parse(defaultparser, io, T; kwargs...)
return ok(res.code) ? res.result : nothing
end
function tryparse(io::IO, ::Type{T}; kwargs...) where {T}
res = parse(defaultparser, io, T; kwargs...)
return ok(res.code) ? res.result : nothing
end
function tryparse(f::Base.Callable, str::AbstractString, ::Type{T}; kwargs...) where {T}
res = parse!(f, IOBuffer(str), Result(T); kwargs...)
io = getio(str)
res = parse!(f, io, Result(T); kwargs...)
return ok(res.code) ? res.result : nothing
end
function tryparse(f::Base.Callable, io::IO, ::Type{T}; kwargs...) where {T}
Expand Down
65 changes: 47 additions & 18 deletions src/floats.jl
Original file line number Diff line number Diff line change
@@ -1,12 +1,20 @@
using Base.GMP, Base.GMP.MPZ

const ONES = [BigInt(1)]
const NUMS = [BigInt()]
const QUOS = [BigInt()]
const REMS = [BigInt()]
const SCLS = [BigInt()]

const BIG_E = UInt8('E')
const LITTLE_E = UInt8('e')

const bipows5 = [big(5)^x for x = 0:325]

function roundQuotient(num, den)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I assume this is doing something like round(num/den), but round-to-nearest?

quo, rem = divrem(num, den)
@inbounds quo, rem = MPZ.tdiv_qr!(QUOS[Threads.threadid()], REMS[Threads.threadid()], num, den)
q = Int64(quo)
cmpflg = cmp(rem << 1, den)
cmpflg = cmp(MPZ.mul_2exp!(rem, 1), den)
return ((q & 1) == 0 ? 1 == cmpflg : -1 < cmpflg) ? q + 1 : q
end

Expand All @@ -30,9 +38,31 @@ significantbits(::Type{Float16}) = 11
significantbits(::Type{Float32}) = 24
significantbits(::Type{Float64}) = 53

bitlength(this) = Base.GMP.MPZ.sizeinbase(this, 2)
bitlength(this) = GMP.MPZ.sizeinbase(this, 2)
bits(::Type{T}) where {T <: Union{Float16, Float32, Float64}} = 8sizeof(T)

BigInt!(y::BigInt, x::BigInt) = x
BigInt!(y::BigInt, x::Union{Clong,Int32}) = MPZ.set_si!(y, x)
# copied from gmp.jl:285
function BigInt!(y::BigInt, x::Integer)
x == 0 && return y
nd = ndigits(x, base=2)
z = GMP.MPZ.realloc2!(y, nd)
s = sign(x)
s == -1 && (x = -x)
x = unsigned(x)
size = 0
limbnbits = sizeof(GMP.Limb) << 3
while nd > 0
size += 1
unsafe_store!(z.d, x % GMP.Limb, size)
x >>>= limbnbits
nd -= limbnbits
end
z.size = s*size
z
end

@inline function scale(::Type{T}, v, exp) where {T <: Union{Float16, Float32, Float64}}
ms = maxsig(T)
cl = ceillog5(T)
Expand All @@ -45,31 +75,30 @@ bits(::Type{T}) where {T <: Union{Float16, Float32, Float64}} = 8sizeof(T)
end
end
v == 0 && return zero(T)
# if v < 2ms
# if 0 <= exp < 2cl
# return T(Base.twiceprecision(Base.TwicePrecision{T}(v) * pow10(T, exp), significantbits(T)))
# elseif -2cl < exp < 0
# return T(Base.twiceprecision(Base.TwicePrecision{T}(v) / pow10(T, -exp), significantbits(T)))
# end
# end
mant = big(v)
@inbounds mant = BigInt!(NUMS[Threads.threadid()], v)
if 0 <= exp < 327
num = mant * bipows5[exp+1]
num = MPZ.mul!(mant, bipows5[exp+1])
bex = bitlength(num) - significantbits(T)
bex <= 0 && return ldexp(T(num), exp)
quo = roundQuotient(num, big(1) << bex)
@inbounds one = MPZ.mul_2exp!(MPZ.set_si!(ONES[Threads.threadid()], 1), bex)
quo = roundQuotient(num, one)
return ldexp(T(quo), bex + exp)
elseif -327 < exp < 0
maxpow = length(bipows5) - 1
scl = (-exp <= maxpow) ? bipows5[-exp+1] :
bipows5[maxpow+1] * bipows5[-exp-maxpow+1]
@inbounds scl = SCLS[Threads.threadid()]
if -exp <= maxpow
MPZ.set!(scl, bipows5[-exp+1])
else
MPZ.set!(scl, bipows5[maxpow+1])
MPZ.mul!(scl, bipows5[-exp-maxpow+1])
end
bex = bitlength(mant) - bitlength(scl) - significantbits(T)
num = mant << -bex
num = MPZ.mul_2exp!(mant, -bex)
quo = roundQuotient(num, scl)
# @info "debug" mant=mant exp=exp num=num quo=quo lh=(bits(T) - leading_zeros(quo)) rh=significantbits(T) bex=bex
if (bits(T) - leading_zeros(quo) > significantbits(T)) || mant == big(22250738585072011)
if (bits(T) - leading_zeros(quo) > significantbits(T)) || exp == -324
bex += 1
quo = roundQuotient(num, scl << 1)
quo = roundQuotient(num, MPZ.mul_2exp!(scl, 1))
end
if exp <= -324
return T(ldexp(BigFloat(quo), bex + exp))
Expand Down