From 699b588210e92ccd9b526df689698a5d4b24d87f Mon Sep 17 00:00:00 2001 From: Kristoffer Carlsson Date: Wed, 22 Aug 2018 12:10:22 +0200 Subject: [PATCH] improve performance of parse (#28787) (cherry picked from commit 81850b6eaa7cc079005db752d1e381f7fb654196) --- base/parse.jl | 24 ++++++++++++++++-------- 1 file changed, 16 insertions(+), 8 deletions(-) diff --git a/base/parse.jl b/base/parse.jl index d0037ca6a6574e..50b76e0577eb10 100644 --- a/base/parse.jl +++ b/base/parse.jl @@ -106,14 +106,21 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos:: return nothing end - base = convert(T,base) - m::T = div(typemax(T)-base+1,base) + base = convert(T, base) + m::T = div(typemax(T) - base + 1, base) n::T = 0 a::Int = base <= 36 ? 10 : 36 + _0 = UInt32('0') + _9 = UInt32('9') + _A = UInt32('A') + _a = UInt32('a') + _Z = UInt32('Z') + _z = UInt32('z') while n <= m - d::T = '0' <= c <= '9' ? c-'0' : - 'A' <= c <= 'Z' ? c-'A'+10 : - 'a' <= c <= 'z' ? c-'a'+a : base + _c = UInt32(c) + d::T = _0 <= _c <= _9 ? _c-_0 : + _A <= _c <= _Z ? _c-_A+ UInt32(10) : + _a <= _c <= _z ? _c-_a+a : base if d >= base raise && throw(ArgumentError("invalid base $base digit $(repr(c)) in $(repr(SubString(s,startpos,endpos)))")) return nothing @@ -129,9 +136,10 @@ function tryparse_internal(::Type{T}, s::AbstractString, startpos::Int, endpos:: end (T <: Signed) && (n *= sgn) while !isspace(c) - d::T = '0' <= c <= '9' ? c-'0' : - 'A' <= c <= 'Z' ? c-'A'+10 : - 'a' <= c <= 'z' ? c-'a'+a : base + _c = UInt32(c) + d::T = _0 <= _c <= _9 ? _c-_0 : + _A <= _c <= _Z ? _c-_A+ UInt32(10) : + _a <= _c <= _z ? _c-_a+a : base if d >= base raise && throw(ArgumentError("invalid base $base digit $(repr(c)) in $(repr(SubString(s,startpos,endpos)))")) return nothing