-
Notifications
You must be signed in to change notification settings - Fork 102
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Improve number parsing in streams: use a PushVector to work around sl…
…ow push! in Base (#264) * restore special case parsing of numbers for in memory JSON * use a PushVector to work around slow push! in Base
- Loading branch information
1 parent
8d4346e
commit 741cbd5
Showing
2 changed files
with
37 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
# This is a vector wrapper that we use as a workaround for `push!` | ||
# being slow (it always calls into the runtime even if the underlying buffer, | ||
# has enough space). Here we keep track of the length using an extra field | ||
mutable struct PushVector{T, A<:AbstractVector{T}} <: AbstractVector{T} | ||
v::A | ||
l::Int | ||
end | ||
|
||
# Default length of 20 should be enough to never need to grow in most cases | ||
PushVector{T}() where {T} = PushVector(Vector{T}(undef, 20), 0) | ||
|
||
Base.unsafe_convert(::Type{Ptr{UInt8}}, v::PushVector) = pointer(v.v) | ||
Base.length(v::PushVector) = v.l | ||
Base.size(v::PushVector) = (v.l,) | ||
@inline function Base.getindex(v::PushVector, i) | ||
@boundscheck checkbounds(v, i) | ||
@inbounds v.v[i] | ||
end | ||
|
||
function Base.push!(v::PushVector, i) | ||
v.l += 1 | ||
if v.l > length(v.v) | ||
resize!(v.v, v.l * 2) | ||
end | ||
v.v[v.l] = i | ||
return v | ||
end | ||
|
||
function Base.resize!(v::PushVector, l::Integer) | ||
# Only support shrinking for now, since that is all we need | ||
@assert l <= v.l | ||
v.l = l | ||
end |