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

Auto initialize in startproc #74

Merged
merged 6 commits into from
Oct 3, 2024
Merged

Auto initialize in startproc #74

merged 6 commits into from
Oct 3, 2024

Conversation

nhz2
Copy link
Member

@nhz2 nhz2 commented Sep 16, 2024

Fixes #70. Third time's the charm

This PR avoids the complexity of interacting with GC in #71 by checking if the context is null when startproc is called.
If the context is null startproc initializes the context.

This means finalize is still needed to prevent memory leaks, but there is no issue of use after free.

Copy link

codecov bot commented Sep 16, 2024

Codecov Report

Attention: Patch coverage is 75.00000% with 6 lines in your changes missing coverage. Please review.

Project coverage is 61.35%. Comparing base (e7edfed) to head (60079dd).
Report is 1 commits behind head on master.

Files with missing lines Patch % Lines
src/compression.jl 70.00% 3 Missing ⚠️
src/decompression.jl 70.00% 3 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##           master      #74      +/-   ##
==========================================
- Coverage   61.55%   61.35%   -0.21%     
==========================================
  Files           5        5              
  Lines         372      370       -2     
==========================================
- Hits          229      227       -2     
  Misses        143      143              

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@@ -111,6 +111,9 @@ function TranscodingStreams.startproc(codec::ZstdCompressor, mode::Symbol, error
end

function TranscodingStreams.process(codec::ZstdCompressor, input::Memory, output::Memory, error::Error)
if codec.cstream.ptr == C_NULL
error("startproc must be called before process")
Copy link
Member Author

Choose a reason for hiding this comment

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

This error should also be unreachable in normal operation.

codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)
if iserror(i_code)
error[] = ErrorException("zstd initialization error")
Copy link
Member Author

Choose a reason for hiding this comment

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

These errors are unreachable unless there is some allocation error.

Copy link
Member

Choose a reason for hiding this comment

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

Could you mock the out of memory condition them by using that advanced API that provides the memory allocation functions?

Copy link
Member Author

Choose a reason for hiding this comment

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

I'm not sure if even that would reliably trigger an error specifically here, because memory allocations are happening in ZSTD_createCStream.

@nhz2 nhz2 marked this pull request as ready for review September 16, 2024 17:19
@nhz2 nhz2 requested a review from mkitti September 16, 2024 17:20
Copy link
Member

@mkitti mkitti left a comment

Choose a reason for hiding this comment

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

Interesting approach. We may still want to consider the finalizer.

src/compression.jl Outdated Show resolved Hide resolved
test/runtests.jl Show resolved Hide resolved
src/compression.jl Outdated Show resolved Hide resolved
codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)
if iserror(i_code)
error[] = ErrorException("zstd initialization error")
Copy link
Member

Choose a reason for hiding this comment

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

Could you mock the out of memory condition them by using that advanced API that provides the memory allocation functions?

test/compress_endOp.jl Show resolved Hide resolved
Comment on lines -86 to -87
reset!(codec.cstream.ibuffer)
reset!(codec.cstream.obuffer)
Copy link
Member

Choose a reason for hiding this comment

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

Where does this happen now?

Copy link
Member Author

Choose a reason for hiding this comment

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

This happens in reset!

reset!(cstream.ibuffer)
reset!(cstream.obuffer)

Which is called in startproc

code = reset!(codec.cstream, 0 #=unknown source size=#)

src/compression.jl Outdated Show resolved Hide resolved
src/decompression.jl Outdated Show resolved Hide resolved
throw(OutOfMemoryError())
end
codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)
Copy link
Member

Choose a reason for hiding this comment

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

Also see notes in #73

Should initialize! throw so we can catch it here and transmit the error?

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes, if initialize! is changed to throw, then this needs to catch that error and return :error, but for now initialize! returns an error code on failure.

@nhz2
Copy link
Member Author

nhz2 commented Sep 17, 2024

Interesting approach. We may still want to consider the finalizer.

Yes, having a finalizer should be compatible with this PR, and would also prevent memory leaks.

@nhz2 nhz2 requested a review from mkitti September 17, 2024 19:48
@nhz2
Copy link
Member Author

nhz2 commented Sep 26, 2024

@mkitti Could you review this PR again, I think I addressed your comments.

@mkitti
Copy link
Member

mkitti commented Sep 27, 2024

Overall, it looks fine. I will try to look more closely tonight.

@nhz2 nhz2 merged commit 7bd48b4 into master Oct 3, 2024
11 of 12 checks passed
@nhz2 nhz2 deleted the nz/auto-initialize branch October 3, 2024 23:46
@ali-ramadhan
Copy link

Thank you @nhz2 and @mkitti! This fixes JuliaIO/JLD2.jl#599 which will allow us to use zstd to compress Oceananigans.jl outputs.

Would it be possible to tag a new version of CodecZstd.jl?

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.

Reusing a compressor
3 participants