-
Notifications
You must be signed in to change notification settings - Fork 435
Add zlib compressor and decompressor #1766
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
Conversation
Motivation: v2 should support compression. Modifications: - Add a Zlib compressor and decompressor supporting deflate and gzip. - This is loosely based on the code we had in v1 but refactored to fit slightly different requirements. Result: Can compress and decompress messages using deflate/gzip
| /// This compressor is only suitable for compressing whole messages at a time. Callers | ||
| /// must ``initialize()`` the compressor before using it and must ``reset()`` it between | ||
| /// subsequent calls to ``compress(_:into:)``. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there a reason why we couldn't simplify this API by calling stream/deflateInit in the initialiser, and doing the logic of reset() before calling deflate in compress(_:into:)? It would make it impossible to make mistakes (by forgetting to call these methods) while using the compressor.
Same question about the Decompressor below.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Unfortunately not, initialising in the init results in a Z_STREAM_ERROR when you call compress (same goes for the decompress). The same is true if you use a static function to create and then initialize and return the initialized compressor/decompressor. I don't understand why that's the case though. In v1 we did do this in init but the compressor/decompressor were both classes but I'd rather save the allocation.
W.r.t. reset(), that seems like a reasonable idea. I'm not sure what the performance implications are but the docs say it does not free or reallocate internal state so should be okay to do.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ah, that's unfortunate about the init.
W.r.t reset() - if the performance was to be affected by running it always in the defer block, we could instead add another flag (isDirty or something like that, signalling that we've already called (de)compress) and if that's set at the beginning of the reset() call, then we reset just before doing the rest of the logic.
That way we'd be saving the cost of one reset() (the one that would otherwise happen after the last (de)compression) - but well, I don't know if we care enough about this for now :D
Motivation: v2 should support compression. Modifications: - Add a Zlib compressor and decompressor supporting deflate and gzip. - This is loosely based on the code we had in v1 but refactored to fit slightly different requirements. Result: Can compress and decompress messages using deflate/gzip
Motivation:
v2 should support compression.
Modifications:
Result:
Can compress and decompress messages using deflate/gzip