Skip to content

Commit

Permalink
cache the utf8 array instead of recreating it for every number
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Nov 2, 2018
1 parent 7ff5666 commit 9fb529c
Showing 1 changed file with 8 additions and 4 deletions.
12 changes: 8 additions & 4 deletions src/Parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ abstract type ParserState end
mutable struct MemoryParserState <: ParserState
utf8::String
s::Int
utf8array::Vector{UInt8}
end

# it is convenient to access MemoryParserState like a Vector{UInt8} to avoid copies
Expand All @@ -31,8 +32,9 @@ mutable struct StreamingParserState{T <: IO} <: ParserState
io::T
cur::UInt8
used::Bool
utf8array::Vector{UInt8}
end
StreamingParserState(io::IO) = StreamingParserState(io, 0x00, true)
StreamingParserState(io::IO) = StreamingParserState(io, 0x00, true, UInt8[])

struct ParserContext{DictType, IntType} end

Expand Down Expand Up @@ -362,7 +364,7 @@ end
function parse_number(pc::ParserContext, ps::ParserState)
# Determine the end of the floating point by skipping past ASCII values
# 0-9, +, -, e, E, and .
number = UInt8[]
number = ps.utf8array
isint = true

@inbounds while hasmore(ps)
Expand All @@ -380,7 +382,9 @@ function parse_number(pc::ParserContext, ps::ParserState)
incr!(ps)
end

number_from_bytes(pc, ps, isint, number, 1, length(number))
v = number_from_bytes(pc, ps, isint, number, 1, length(number))
resize!(number, 0)
return v
end

unparameterize_type(x) = x # Fallback for nontypes -- functions etc
Expand All @@ -403,7 +407,7 @@ function parse(str::AbstractString;
dicttype=Dict{String,Any},
inttype::Type{<:Real}=Int64)
pc = _get_parsercontext(dicttype, inttype)
ps = MemoryParserState(str, 1)
ps = MemoryParserState(str, 1, UInt8[])
v = parse_value(pc, ps)
chomp_space!(ps)
if hasmore(ps)
Expand Down

0 comments on commit 9fb529c

Please sign in to comment.