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

Allow saving of 'recursive' immutable structs #196

Merged
merged 3 commits into from
Aug 13, 2020

Conversation

JonasIsensee
Copy link
Collaborator

When serializing immutable structs, JLD2 attempts to generate an in-line representation for the given struct.
It does so by recursively trying to find the memory layout of all immutable fields.
This is neat for many applications but it fails when encountering recursive structs e.g.

struct RecursiveStruct
    x::RecursiveStruct
    RecursiveStruct() = new()
    RecursiveStruct(x) = new(x)
end

This PR modifies the relevant function hasfielddata and hasdata to look for such recursive structs.
The functions now track which types they already have encountered and terminate the inlineing
when a type signature is encountered again.

closes #189

@giordano
Copy link
Member

giordano commented Jul 4, 2020

I can't comment on the implementation as I'm not familiar with the code of the package, but I tried this PR with Measurements.jl and it seems to work great:

julia> using JLD2, Measurements

julia> x = 3 ± 1
3.0 ± 1.0

julia> y = 2x
6.0 ± 2.0

julia> y - x - x
0.0 ± 0.0

julia> z = 6 ± 2
6.0 ± 2.0

julia> z - x - x
0.0 ± 2.8

julia> @save "output.jld2" x y z

julia> JLD2.jldopen("output.jld2") do io
           io["y"] - io["x"] - io["x"]
       end
0.0 ± 0.0

julia> JLD2.jldopen("output.jld2") do io
           io["z"] - io["x"] - io["x"]
       end
0.0 ± 2.8

{Un,}correlated variables are handled corrected when deserialised, too!

@JonasIsensee JonasIsensee merged commit 7f725c1 into JuliaIO:master Aug 13, 2020
@oschulz
Copy link

oschulz commented Aug 13, 2020

Thanks for this!

@oschulz
Copy link

oschulz commented Aug 13, 2020

Would it be possible to have a new release of JLD2 that includes this? This functionality would be very helpful for us.

@JonasIsensee
Copy link
Collaborator Author

Hi,
I'm trying to prepare a somewhat bigger release.
In particular I would like to include #206 but I am not super happy with that PR just yet.

I'd say the release is no more than 2 weeks away. If I can't fix #206 until then, I'll put out a release without it.
Does that sound alright for you?

@oschulz
Copy link

oschulz commented Aug 13, 2020

Thanks, that sounds great! Two weeks is not a problem.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Can't save a Measurements.Measurement
3 participants