Skip to content

Commit 501cc88

Browse files
committed
Try gc preserve for ptrstring
1 parent cba1b4a commit 501cc88

2 files changed

Lines changed: 33 additions & 18 deletions

File tree

src/lazy.jl

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -270,17 +270,19 @@ function applyobject(keyvalfunc, x::LazyValues)
270270
b == UInt8('}') && return pos + 1
271271
while true
272272
# parsestring returns key as a PtrString
273-
key, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
274-
@nextbyte
275-
if b != UInt8(':')
276-
error = ExpectedColon
277-
@goto invalid
273+
GC.@preserve buf begin
274+
key, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
275+
@nextbyte
276+
if b != UInt8(':')
277+
error = ExpectedColon
278+
@goto invalid
279+
end
280+
pos += 1
281+
@nextbyte
282+
# we're now positioned at the start of the value
283+
val = _lazy(buf, pos, len, b, opts)
284+
ret = keyvalfunc(key, val)
278285
end
279-
pos += 1
280-
@nextbyte
281-
# we're now positioned at the start of the value
282-
val = _lazy(buf, pos, len, b, opts)
283-
ret = keyvalfunc(key, val)
284286
# if ret is an EarlyReturn, then we're short-circuiting
285287
# parsing via e.g. selection syntax, so return immediately
286288
ret isa StructUtils.EarlyReturn && return ret
@@ -732,8 +734,11 @@ function Base.show(io::IO, x::LazyValue)
732734
show(io, MIME"text/plain"(), la)
733735
end
734736
elseif T == JSONTypes.STRING
735-
str, _ = parsestring(x)
736-
Base.print(io, "JSON.LazyValue(", repr(convert(String, str)), ")")
737+
buf = getbuf(x)
738+
GC.@preserve buf begin
739+
str, _ = parsestring(x)
740+
Base.print(io, "JSON.LazyValue(", repr(convert(String, str)), ")")
741+
end
737742
elseif T == JSONTypes.NULL
738743
Base.print(io, "JSON.LazyValue(nothing)")
739744
else # bool/number

src/parse.jl

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -277,8 +277,11 @@ function applyvalue(f, x::LazyValues, null)
277277
f(arr)
278278
return pos
279279
elseif type == JSONTypes.STRING
280-
str, pos = parsestring(x)
281-
f(convert(String, str))
280+
buf = getbuf(x)
281+
GC.@preserve buf begin
282+
str, pos = parsestring(x)
283+
f(convert(String, str))
284+
end
282285
return pos
283286
elseif type == JSONTypes.NUMBER
284287
num, pos = parsenumber(x)
@@ -343,9 +346,12 @@ end
343346

344347
function StructUtils.lift(style::StructStyle, ::Type{T}, x::LazyValues, tags=(;)) where {T}
345348
type = gettype(x)
349+
buf = getbuf(x)
346350
if type == JSONTypes.STRING
347-
ptrstr, pos = parsestring(x)
348-
str, _ = StructUtils.lift(style, T, ptrstr, tags)
351+
GC.@preserve buf begin
352+
ptrstr, pos = parsestring(x)
353+
str, _ = StructUtils.lift(style, T, ptrstr, tags)
354+
end
349355
return str, pos
350356
elseif type == JSONTypes.NUMBER
351357
num, pos = parsenumber(x)
@@ -448,7 +454,9 @@ end
448454
Base.@nexprs $N i -> begin
449455
if typ == JSONTypes.OBJECT
450456
# consume key
451-
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
457+
GC.@preserve buf begin
458+
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
459+
end
452460
@nextbyte
453461
if b != UInt8(':')
454462
error = ExpectedColon
@@ -483,7 +491,9 @@ end
483491
while true
484492
if typ == JSONTypes.OBJECT
485493
# consume key
486-
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
494+
GC.@preserve buf begin
495+
_, pos = @inline parsestring(LazyValue(buf, pos, JSONTypes.STRING, opts, false))
496+
end
487497
@nextbyte
488498
if b != UInt8(':')
489499
error = ExpectedColon

0 commit comments

Comments
 (0)