From 9fb529c4a06f6969a8aef9a11b2d1a6cf00716aa Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Fri, 2 Nov 2018 10:13:35 -0400 Subject: [PATCH] cache the utf8 array instead of recreating it for every number --- src/Parser.jl | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/src/Parser.jl b/src/Parser.jl index d20467d..946db6f 100644 --- a/src/Parser.jl +++ b/src/Parser.jl @@ -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 @@ -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 @@ -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) @@ -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 @@ -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)