Skip to content

Commit 0e3226e

Browse files
authored
perf: reduce parser precompile workload (#204)
Trim the PrecompileTools workload to core scalar parse/xparse cases and avoid precompiling BigFloat, Dates, SubString, and view inputs. Add inference barriers around rare float widening paths so simple Float64 precompile does not eagerly infer the UInt128/BigInt overflow tree.
1 parent 742c0a0 commit 0e3226e

2 files changed

Lines changed: 11 additions & 9 deletions

File tree

src/floats.jl

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ function parsedigits(conf::AbstractConf{T}, source, pos, len, b, code, options,
312312
while true
313313
if b <= 0x09
314314
if overflows(IntType) && digits > overflowval(IntType)
315-
return _parsedigits(conf, source, pos, len, b + UInt8('0'), code, options, _widen(digits), neg, startpos, overflow_invalid, ndigits, f)
315+
return _parsedigits(conf, source, pos, len, b + UInt8('0'), code, options, Base.inferencebarrier(_widen(digits)), neg, startpos, overflow_invalid, ndigits, f)
316316
elseif ndigits > maxdigits(T)
317317
# if input is way too big, just bail
318318
fastseek!(source, startpos - 1)
@@ -392,7 +392,7 @@ function parsedigits(conf::AbstractConf{T}, source, pos, len, b, code, options,
392392
# now we parse any digits following decimal point (if any); start `frac` at UInt64(0)
393393
# `digits` still receives any fractional digits, `frac` just keeps track of how many digits
394394
# were parsed to combine with any "e123" exponent numbers to determine final exponent value
395-
(overflows(IntType) && digits > overflowval(IntType)) && (digits = _widen(digits))
395+
(overflows(IntType) && digits > overflowval(IntType)) && (digits = Base.inferencebarrier(_widen(digits)))
396396
x, code, pos = parsefrac(conf, source, pos, len, b, code, options, digits, neg, startpos, UInt64(0), overflow_invalid, ndigits, f)
397397

398398
@label done
@@ -434,7 +434,7 @@ function parsefrac(conf::AbstractConf{T}, source, pos, len, b, code, options, di
434434
b = peekbyte(source, pos) - UInt8('0')
435435
b > 0x09 && break
436436
if overflows(IntType) && digits > overflowval(IntType)
437-
return _parsefrac(conf, source, pos, len, b + UInt8('0'), code, options, _widen(digits), neg, startpos, frac, overflow_invalid, ndigits, f)
437+
return _parsefrac(conf, source, pos, len, b + UInt8('0'), code, options, Base.inferencebarrier(_widen(digits)), neg, startpos, frac, overflow_invalid, ndigits, f)
438438
end
439439
end
440440
b += UInt8('0')
@@ -535,7 +535,7 @@ function parseexp(conf::AbstractConf{T}, source, pos, len, b, code, options, dig
535535
@goto done
536536
end
537537
if overflows(ExpType) && exp > overflowval(ExpType)
538-
return _parseexp(conf, source, pos, len, b, code, options, digits, neg, startpos, frac, _widen(exp), negexp, FT, overflow_invalid, ndigits, f)
538+
return _parseexp(conf, source, pos, len, b, code, options, digits, neg, startpos, frac, Base.inferencebarrier(_widen(exp)), negexp, FT, overflow_invalid, ndigits, f)
539539
end
540540
end
541541
@label done

src/precompile.jl

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,20 +5,22 @@ using PrecompileTools
55
# precompile file and potentially make loading faster.
66
options = Parsers.Options()
77
pos = 1
8-
val = "123"
9-
len = length(val)
8+
int_val = "123"
9+
float_val = "123.45"
10+
bool_val = "true"
1011
@compile_workload begin
1112
# all calls in this block will be precompiled, regardless of whether
1213
# they belong to your package or not (on Julia 1.8 and higher)
13-
for T in (String, Int32, Int64, Float64, BigFloat, Dates.Date, Dates.DateTime, Dates.Time, Bool)
14+
for (T, val) in ((String, int_val), (Int32, int_val), (Int64, int_val), (Float64, float_val), (Bool, bool_val))
15+
len = length(val)
1416
for buf in (codeunits(val), Vector(codeunits(val)))
1517
Parsers.xparse(T, buf, pos, len, options)
1618
Parsers.xparse(T, buf, pos, len, options, Any)
1719
end
1820
end
1921

20-
for T in (Int32, Int64, Float64, BigFloat, Dates.Date, Dates.DateTime, Dates.Time, Bool)
21-
for buf in (val, SubString(val, 1:3), Vector(codeunits(val)), view(Vector(codeunits(val)), 1:3))
22+
for (T, val) in ((Int32, int_val), (Int64, int_val), (Float64, float_val), (Bool, bool_val))
23+
for buf in (val, Vector(codeunits(val)))
2224
try
2325
Parsers.parse(T, buf, options)
2426
catch

0 commit comments

Comments
 (0)