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

Close objects in h5* immediate functions #754

Merged
merged 1 commit into from
Dec 3, 2020
Merged

Conversation

jmert
Copy link
Contributor

@jmert jmert commented Dec 3, 2020

The try-catch blocks make sure to close the file, but we might as well close the objects themselves deterministically rather than waiting for the GC and/or libhdf5's H5F_CLOSE_STRONG to reap them. This should also be a net performance improvement in h5writeattr even ignoring the subtler close issue just due to opening the parent object only once.

This was discovered as a way to improve h5read performance in #752 (comment) and #752 (comment).

Co-authored-by: Gerhard Aigner <gerhard.aigner@gmail.com>
@jmert
Copy link
Contributor Author

jmert commented Dec 3, 2020

Failures on Julia nightly are unrelated to this PR and are an upstream problem.

@musm
Copy link
Member

musm commented Dec 3, 2020

Makes sense.

@musm
Copy link
Member

musm commented Dec 3, 2020

Are we missing a close on https://github.com/JuliaIO/HDF5.jl/blob/master/src/HDF5.jl#L1452 dtype as well?

Edit: No we're not.

@musm musm merged commit 5961753 into JuliaIO:master Dec 3, 2020
@jmert jmert deleted the close_early branch December 3, 2020 16:40
@@ -536,6 +536,7 @@ function h5read(filename, name::AbstractString; pv...)
try
obj = getindex(fid, name; pv...)
dat = read(obj)
close(obj)
Copy link
Member

Choose a reason for hiding this comment

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

should these actually be in the finally block?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Technically, they might need a set of nested try-catch blocks: you can't close obj if getindex failed, but then you do want to close it if read fails.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Though allowing obj to escape the try-catch block if read fails is basically what the status quo was — GC or libhdf5's close semantics should eventually take care of the object.

Copy link
Member

Choose a reason for hiding this comment

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

I'm not 100% confident we are following the correct close semantics in all our usages of the try--finally blocks, many of them are legacy, but I believe are mostly correct. Ideally, we could minimize their usage to only essential places, since try finally blocks are slow calls.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Yeah, I haven't thought hard about the try-finally use yet, but IIUC they're most essential in cases like this were a file handle is being opened and closed. I agree in trying to see if more uses can be removed elsewhere, though.

Copy link
Member

Choose a reason for hiding this comment

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

Though allowing obj to escape the try-catch block if read fails is basically what the status quo was — GC or libhdf5's close semantics should eventually take care of the object.

If read fails, close(obj) won't execute, and then we fall back to closing via libhdf5's or GC's close semantics, which were demonstrated to cause some regressions. This is an edge case and so the performance isn't critical.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Sorry, yeah, I guess I only implied it, but that's what I meant.

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.

2 participants