Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Saving array length for undef array issues #47

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/BSON.jl
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ end

function applychildren!(f::Function, x::BSONArray)::BSONArray
for i = 1:length(x)
isassigned(x, i) || continue
x[i] = f(x[i])
end
return x
Expand Down
37 changes: 28 additions & 9 deletions src/extensions.jl
Original file line number Diff line number Diff line change
Expand Up @@ -67,18 +67,37 @@ lower(x::Vector{UInt8}) = x
reinterpret_(::Type{T}, x) where T =
T[_x for _x in reinterpret(T, x)]

function lower(x::Array)
ndims(x) == 1 && !isbitstype(eltype(x)) && return Any[x...]
BSONDict(:tag => "array", :type => eltype(x), :size => Any[size(x)...],
:data => isbitstype(eltype(x)) ? reinterpret_(UInt8, reshape(x, :)) : Any[x...])

function collect_any(xs)
ys = Vector{Any}(undef, length(xs))
for i = 1:length(xs)
isassigned(xs, i) && (ys[i] = xs[i])
end
return ys
end


function lower(x::Array{T,N}) where {T,N}
isone(N) && !isbitstype(T) && return collect_any(x)
BSONDict(:tag => "array", :type => T, :size => Any[size(x)...],
:data => isbitstype(T) ? reinterpret_(UInt8, reshape(x, :)) : collect_any(x))
end


tags[:array] = d ->
isbitstype(d[:type]) ?
sizeof(d[:type]) == 0 ?
fill(d[:type](), d[:size]...) :
reshape(reinterpret_(d[:type], d[:data]), map(normalize_typeparams, d[:size])...) :
Array{d[:type]}(reshape(d[:data], d[:size]...))
if isbitstype(d[:type])
if sizeof(d[:type]) == 0
fill(d[:type](), d[:size]...)
else
reshape(reinterpret_(d[:type], d[:data]), d[:size]...)
end
else
a = Array{d[:type]}(undef, d[:size]...)
for i in 1:length(d[:data])
isassigned(d[:data], i) && (a[i] = d[:data][i])
end
a
end

# Structs

Expand Down
18 changes: 12 additions & 6 deletions src/read.jl
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,21 @@ end
function parse_array(io::IO)::BSONArray
len = read(io, Int32)
ps = BSONArray()

len_set = false
while (tag = read(io, BSONType)) ≠ eof
# Note that arrays are dicts with the index as the key
while read(io, UInt8) != 0x00
nothing
end
push!(ps, parse_tag(io::IO, tag))
# The first index in dict is "length" => length(x)
index_or_length = parse_cstr(io)
val = parse_tag(io::IO, tag)
if index_or_length == "length"
resize!(ps, Int(val))
len_set = true
else
i = Base.parse(Int, index_or_length) + 1
len_set || resize!(ps, i)
ps[i] = val
end
end

ps
end

Expand Down
2 changes: 1 addition & 1 deletion src/write.jl
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ end

bson_primitive(io::IO, doc::BSONDict) = bson_doc(io, doc)
bson_primitive(io::IO, x::BSONArray) =
bson_doc(io, [Base.string(i-1) => v for (i, v) in enumerate(x)])
bson_doc(io, vcat(["length" => length(x)],[Base.string(i-1) => x[i] for i = 1:length(x) if isassigned(x, i)]))

# Lowering

Expand Down
26 changes: 26 additions & 0 deletions test/runtests.jl
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,29 @@ using BSON
using Test

roundtrip_equal(x) = BSON.roundtrip(x) == x
function roundtrip_equal(x::AbstractArray)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Special casing roundtrip_equal for just these couple of types seems like it could lead to confusion. I think it'd be better to define an undef_equal(x, y) for arrays and explicitly test undef_equal(BSON.roundtrip(x), x) or similar.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Bump @Codyk12 can you address this comment?

result = BSON.roundtrip(x)
all(eachindex(result, x)) do I
if !isassigned(x, I)
!isassigned(result, I)
else
x[I] == result[I]
end
end
end

function roundtrip_equal(x::Dict{String,Array{Real,2}})
result = BSON.roundtrip(x)
keys(result) == keys(x)
all(keys(x)) do key
if isassigned(x[key])
x[key] == result[key]
else
isassigned(x[key]) == isassigned(result[key])
end
end
end


mutable struct Foo
x
Expand Down Expand Up @@ -33,6 +56,8 @@ end
@test roundtrip_equal(Array)
@test roundtrip_equal([1,2,3])
@test roundtrip_equal(rand(2,3))
@test roundtrip_equal(Array{Real}(undef, 2,3))
@test roundtrip_equal(Array{String}(undef, 2,3))
@test roundtrip_equal(Array{Real}(rand(2,3)))
@test roundtrip_equal(1+2im)
@test roundtrip_equal(Nothing[])
Expand All @@ -41,6 +66,7 @@ end
@test roundtrip_equal(fill(S(), (1,3)))
@test roundtrip_equal(Set([1,2,3]))
@test roundtrip_equal(Dict("a"=>1))
@test roundtrip_equal(Dict("a"=>Array{Real}(undef, 2,3)))
@test roundtrip_equal(T(()))
end

Expand Down