-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Support serializing ObjectRef * Use custom RaySerializer * Move CoreWorker wrapper to support GetObjectRefs * Add function for creation of Buffer nullptr * Create RayObject argument with inlined refs * Test using object ref argument * Serialize with header * Implement reset_state * Refactor * Rename file to ray_serializer.jl * Add serialize_to_bytes/deserialize_from_bytes tests * Add RaySerializer tests * deserialize_from_bytes one liner * Add note about indirect testing of serialized header * Add hash support for ObjectRef --------- Co-authored-by: Dave Kleinschmidt <dave.f.kleinschmidt@gmail.com>
- Loading branch information
1 parent
9004702
commit 5c8bfe6
Showing
12 changed files
with
184 additions
and
19 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
mutable struct RaySerializer{I<:IO} <: AbstractSerializer | ||
# Fields required by all AbstractSerializers | ||
io::I | ||
counter::Int | ||
table::IdDict{Any,Any} | ||
pending_refs::Vector{Int} | ||
|
||
# Inlined object references encountered during serializing | ||
object_refs::Set{ObjectRef} | ||
|
||
function RaySerializer{I}(io::I) where I<:IO | ||
return new(io, 0, IdDict(), Int[], Set{ObjectRef}()) | ||
end | ||
end | ||
|
||
RaySerializer(io::IO) = RaySerializer{typeof(io)}(io) | ||
RaySerializer(bytes::Vector{UInt8}) = RaySerializer{IOBuffer}(IOBuffer(bytes; write=true)) | ||
|
||
function Base.getproperty(s::RaySerializer, f::Symbol) | ||
if f === :object_ids | ||
return Set(getproperty.(s.object_refs, :oid)) | ||
else | ||
return getfield(s, f) | ||
end | ||
end | ||
|
||
function Serialization.reset_state(s::RaySerializer) | ||
empty!(s.object_refs) | ||
return invoke(reset_state, Tuple{AbstractSerializer}, s) | ||
end | ||
|
||
function Serialization.serialize(s::RaySerializer, obj_ref::ObjectRef) | ||
push!(s.object_refs, obj_ref) | ||
return invoke(serialize, Tuple{AbstractSerializer, ObjectRef}, s, obj_ref) | ||
end | ||
|
||
# As we are just throwing away the Serializer we can just avoid collecting the inlined | ||
# object references | ||
function serialize_to_bytes(x) | ||
bytes = Vector{UInt8}() | ||
io = IOBuffer(bytes; write=true) | ||
s = Serializer(io) | ||
writeheader(s) | ||
serialize(s, x) | ||
return bytes | ||
end | ||
|
||
deserialize_from_bytes(bytes::Vector{UInt8}) = deserialize(Serializer(IOBuffer(bytes))) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,14 +1,28 @@ | ||
function serialize_deserialize(x) | ||
io = IOBuffer() | ||
serialize(io, x) | ||
seekstart(io) | ||
return deserialize(io) | ||
end | ||
|
||
@testset "ObjectRef" begin | ||
@testset "basic" begin | ||
hex_str = "f" ^ (2 * 28) | ||
obj_ref = ObjectRef(hex_str) | ||
@test obj_ref == ObjectRef(hex_str) | ||
@test Ray.hex_identifier(obj_ref) == hex_str | ||
@test obj_ref == ObjectRef(hex_str) | ||
@test hash(obj_ref) == hash(ObjectRef(hex_str)) | ||
end | ||
|
||
@testset "show" begin | ||
hex_str = "f" ^ (2 * 28) | ||
obj_ref = ObjectRef(hex_str) | ||
@test sprint(show, obj_ref) == "ObjectRef(\"$hex_str\")" | ||
end | ||
|
||
@testset "serialize/deserialize" begin | ||
obj_ref1 = ObjectRef(ray_jll.FromRandom(ray_jll.ObjectID)) | ||
obj_ref2 = serialize_deserialize(obj_ref1) | ||
@test obj_ref1 == obj_ref2 | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,62 @@ | ||
@testset "RaySerializer" begin | ||
@testset "byte constructor" begin | ||
bytes = Vector{UInt8}() | ||
s = Ray.RaySerializer(bytes) | ||
@test isempty(bytes) | ||
|
||
serialize(s, 1) | ||
@test !isempty(bytes) | ||
end | ||
|
||
@testset "object_ids property" begin | ||
s = Ray.RaySerializer(IOBuffer()) | ||
@test s.object_ids isa Set{<:ray_jll.ObjectID} | ||
end | ||
|
||
@testset "inlined object refs" begin | ||
oids = [ray_jll.FromRandom(ray_jll.ObjectID) for _ in 1:3] | ||
obj_refs = map(ObjectRef, oids) | ||
x = [1, 2, obj_refs...] | ||
|
||
s = Ray.RaySerializer(IOBuffer()) | ||
serialize(s, x) | ||
|
||
@test s.object_refs == Set(obj_refs) | ||
@test s.object_ids == Set(oids) | ||
end | ||
|
||
@testset "reset_state" begin | ||
obj_ref = ObjectRef(ray_jll.FromRandom(ray_jll.ObjectID)) | ||
s = Ray.RaySerializer(IOBuffer()) | ||
serialize(s, obj_ref) | ||
@test !isempty(s.object_refs) | ||
|
||
Serialization.reset_state(s) | ||
@test isempty(s.object_refs) | ||
end | ||
end | ||
|
||
@testset "serialize_to_bytes / deserialize_from_bytes" begin | ||
@testset "roundtrip" begin | ||
x = [1, 2, 3] | ||
bytes = Ray.serialize_to_bytes(x) | ||
@test bytes isa Vector{UInt8} | ||
@test !isempty(bytes) | ||
|
||
result = Ray.deserialize_from_bytes(bytes) | ||
@test typeof(result) == typeof(x) | ||
@test result == x | ||
end | ||
|
||
# TODO: Investigate if want to include the serialization header | ||
@testset "serialize with header" begin | ||
x = 123 | ||
bytes = Ray.serialize_to_bytes(x) | ||
|
||
s = Serializer(IOBuffer(bytes)) | ||
b = Int32(read(s.io, UInt8)::UInt8) | ||
@test b == Serialization.HEADER_TAG | ||
Serialization.readheader(s) # Throws if header not present | ||
@test deserialize(s) == x | ||
end | ||
end |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters