Skip to content

Commit 24d2a49

Browse files
Merge pull request #64 from c42f/c42f/fix-serialize-drop_expr-rgfs
Fix serialization of RGFs with dropped body expressions
2 parents 1598da8 + d41842e commit 24d2a49

File tree

3 files changed

+34
-11
lines changed

3 files changed

+34
-11
lines changed

src/RuntimeGeneratedFunctions.jl

Lines changed: 28 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -319,20 +319,39 @@ function closures_to_opaque(ex::Expr, return_type = nothing)
319319
return Expr(head, Any[closures_to_opaque(x, return_type) for x in args]...)
320320
end
321321

322-
# We write an explicit deserialize() here to trigger caching of the body on a
323-
# remote node when using Serialialization.jl (in Distributed.jl and elsewhere)
322+
# We write an explicit serialize() and deserialize() here to manage caching of
323+
# the body on a remote node when using Serialialization.jl (in Distributed.jl
324+
# and elsewhere)
325+
function Serialization.serialize(s::AbstractSerializer,
326+
rgf::RuntimeGeneratedFunction{argnames, cache_tag,
327+
context_tag, id, B}) where {
328+
argnames,
329+
cache_tag,
330+
context_tag,
331+
id,
332+
B
333+
}
334+
body = _lookup_body(cache_tag, id)
335+
Serialization.serialize_type(s,
336+
RuntimeGeneratedFunction{argnames, cache_tag, context_tag,
337+
id, B})
338+
serialize(s, body)
339+
end
340+
324341
function Serialization.deserialize(s::AbstractSerializer,
325342
::Type{
326343
<:RuntimeGeneratedFunction{argnames, cache_tag,
327-
context_tag, id}}) where {
328-
argnames,
329-
cache_tag,
330-
context_tag,
331-
id
332-
}
344+
context_tag, id, B}}) where {
345+
argnames,
346+
cache_tag,
347+
context_tag,
348+
id,
349+
B
350+
}
333351
body = deserialize(s)
334352
cached_body = _cache_body(cache_tag, id, body)
335-
RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id}(cached_body)
353+
f = RuntimeGeneratedFunction{argnames, cache_tag, context_tag, id}(cached_body)
354+
B === Nothing ? drop_expr(f) : f
336355
end
337356

338357
@specialize

test/runtests.jl

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -177,5 +177,8 @@ end
177177

178178
proj = dirname(Base.active_project())
179179
buf = IOBuffer(read(`$(Base.julia_cmd()) --startup-file=no --project=$proj "serialize_rgf.jl"`))
180-
deserialized_f = deserialize(buf)
180+
deserialized_f, deserialized_g = deserialize(buf)
181181
@test deserialized_f(11) == "Hi from a separate process. x=11"
182+
@test deserialized_f.body isa Expr
183+
@test deserialized_g(12) == "Serialization with dropped body. y=12"
184+
@test deserialized_g.body isa Nothing

test/serialize_rgf.jl

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,5 +6,6 @@ using Serialization
66
RuntimeGeneratedFunctions.init(@__MODULE__)
77

88
f = @RuntimeGeneratedFunction(:(x -> "Hi from a separate process. x=$x"))
9+
g = drop_expr(@RuntimeGeneratedFunction(:(y -> "Serialization with dropped body. y=$y")))
910

10-
serialize(stdout, f)
11+
serialize(stdout, (f, g))

0 commit comments

Comments
 (0)