Skip to content

Commit

Permalink
workaround slow dynamic dispatch when creating objects
Browse files Browse the repository at this point in the history
  • Loading branch information
KristofferC committed Nov 2, 2018
1 parent 0ea0945 commit 7ff5666
Showing 1 changed file with 12 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/Parser.jl
Original file line number Diff line number Diff line change
Expand Up @@ -389,10 +389,20 @@ function unparameterize_type(T::Type)
candidate <: Union{} ? T : candidate
end

# Workaround for slow dynamic dispatch for creating objects
const DEFAULT_PARSERCONTEXT = ParserContext{Dict{String, Any}, Int64}()
function _get_parsercontext(dicttype, inttype)
if dicttype == Dict{String, Any} && inttype == Int64
DEFAULT_PARSERCONTEXT
else
ParserContext{unparameterize_type(dicttype), inttype}.instance
end
end

function parse(str::AbstractString;
dicttype=Dict{String,Any},
inttype::Type{<:Real}=Int64)
pc = ParserContext{unparameterize_type(dicttype), inttype}()
pc = _get_parsercontext(dicttype, inttype)
ps = MemoryParserState(str, 1)
v = parse_value(pc, ps)
chomp_space!(ps)
Expand All @@ -405,7 +415,7 @@ end
function parse(io::IO;
dicttype=Dict{String,Any},
inttype::Type{<:Real}=Int64)
pc = ParserContext{unparameterize_type(dicttype), inttype}()
pc = _get_parsercontext(dicttype, inttype)
ps = StreamingParserState(io)
parse_value(pc, ps)
end
Expand Down

0 comments on commit 7ff5666

Please sign in to comment.