Skip to content

Auto initialize in startproc - #74

Merged
nhz2 merged 6 commits into
masterfrom
nz/auto-initialize
Oct 3, 2024
Merged

Auto initialize in startproc#74
nhz2 merged 6 commits into
masterfrom
nz/auto-initialize

Conversation

@nhz2

@nhz2nhz2 commented Sep 16, 2024

Copy link
Copy Markdown
Member

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.

@codecov

codecovBot commented Sep 16, 2024

Copy link
Copy Markdown

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 linesPatch %Lines
src/compression.jl70.00%3 Missing ⚠️
src/decompression.jl70.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.

Comment threadsrc/compression.jl

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
Copy Markdown
MemberAuthor

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.

Comment threadsrc/compression.jl
codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)
if iserror(i_code)
error[] = ErrorException("zstd initialization error")

Copy link
Copy Markdown
MemberAuthor

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
Copy Markdown
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
Copy Markdown
MemberAuthor

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 mkittiSeptember 16, 2024 17:20

@mkittimkitti left a comment

Copy link
Copy Markdown
Member

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.

Comment threadsrc/compression.jl Outdated
Comment threadtest/runtests.jl
Comment threadsrc/compression.jl Outdated
Comment threadsrc/compression.jl
codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)
if iserror(i_code)
error[] = ErrorException("zstd initialization error")

Copy link
Copy Markdown
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?

Comment threadtest/compress_endOp.jl
Comment threadsrc/compression.jl
Comment on lines -86 to -87
reset!(codec.cstream.ibuffer)
reset!(codec.cstream.obuffer)

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

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=#)

Comment threadsrc/compression.jl Outdated
Comment threadsrc/decompression.jl Outdated
Comment threadsrc/compression.jl
throw(OutOfMemoryError())
end
codec.cstream.ptr = ptr
i_code = initialize!(codec.cstream, codec.level)

Copy link
Copy Markdown
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
Copy Markdown
MemberAuthor

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

nhz2 commented Sep 17, 2024

Copy link
Copy Markdown
MemberAuthor

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 mkittiSeptember 17, 2024 19:48
@nhz2

nhz2 commented Sep 26, 2024

Copy link
Copy Markdown
MemberAuthor

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

@mkitti

Copy link
Copy Markdown
Member

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

@ali-ramadhan

Copy link
Copy Markdown

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 freeto 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

@nhz2@mkitti@ali-ramadhan