Skip to content

Commit

Permalink
Update PORTING.md to document zlib-ng memory allocation details.
Browse files Browse the repository at this point in the history
  • Loading branch information
Dead2 committed May 25, 2024
1 parent 658a4d5 commit bbc20a0
Showing 1 changed file with 32 additions and 0 deletions.
32 changes: 32 additions & 0 deletions PORTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,38 @@ Porting applications to use zlib-ng
Zlib-ng can be used/compiled in two different modes, that require some
consideration by the application developer.

Changes from zlib affecting native and compat modes
---------------------------------------------------
Zlib-ng is not as conservative with memory allocation as Zlib is.

Where Zlib's inflate will allocate a lower amount of memory depending on
compression level and window size, zlib-ng will always allocate the maximum
amount of memory and possibly leave parts of it unused.
Zlib-ng's deflate will however allocate a lower amount of memory depending
on compression level and window size.

Zlib-ng also allocates one "big" buffer instead of doing multiple smaller
allocations. This is faster, can lead to better cache locality and reduces
space lost to alignment padding.

At the time of writing, by default zlib-ng allocates the following amounts
of memory on a 64-bit system (except on S390x that requires ~4KiB more):
- Deflate: 350.272 Bytes
- Inflate: 42.112 Bytes

**Advantages:**
- All memory is allocated during DeflateInit or InflateInit functions,
leaving the actual deflate/inflate functions free from allocations.
- Zlib-ng can only fail from memory allocation errors during init.
- Time spent doing memory allocation systemcalls is all done during init,
allowing applications to do prepare this before doing latency-sensitive
deflate/inflate later.
- Can reduce wasted memory due to buffer alignment padding both by OS and zlib-ng.
- Potentially improved memory locality.

**Disadvantages:**
- Zlib-ng allocates a little more memory than zlib does.

zlib-compat mode
----------------
Zlib-ng can be compiled in zlib-compat mode, suitable for zlib-replacement
Expand Down

0 comments on commit bbc20a0

Please sign in to comment.