← Back to Pull requests

Statically link zlib-ng in Linux wheels for 2.5x faster PNG write #10495

Closed
unknown closed this pull request without merging on Nov 13, 2026 11:32 UTC
View on GitHub
unknown unknown commented on Nov 12, 2026 15:33 UTC
Author

Fixes https://github.com/python-pillow/Pillow/issues/10469.

This means the Linux wheels will use zlib-ng from the wheel, not whatever happens to be on the machine, whether that's zlib or zlib-ng. Or nothing at all, as happened in #10469.

Here's benchmarks using the same script from https://github.com/python-pillow/Pillow/pull/8500.

Compared pillow-13.0.0-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl from PyPI, and pillow-13.1.0.dev0-cp313-cp313-manylinux_2_27_aarch64.manylinux_2_28_aarch64.whl from this fork.

<details>
<summary>Script</summary>

from __future__ import annotations

import io
import os
import timeit

from PIL import Image

PATH = "Tests/images/hopper.png"
# PATH = "Tests/images/effect_spread.png"
REPEAT = 1000

print(f"{Image.__version__ = }")
print(f"{Image.core.zlib_version = }")
print(f"Testing {PATH = } with {REPEAT} repetitions")

with open(PATH, "rb") as f:
    data_in = f.read()

reader = io.BytesIO(data_in)
writer = io.BytesIO()

img = Image.open(reader)
img.load()


def read_png():
    reader.seek(0, os.SEEK_SET)
    return Image.open(reader)


def write_png(compress_level):
    writer.seek(0, os.SEEK_SET)
    writer.truncate(0)
    img.save(writer, "PNG", compress_level=compress_level)


t = timeit.timeit(read_png, number=REPEAT * 10)
print(f"read PNG: time = {t:f} (sec)")


results = dict()
for compress_level in range(10):
    t = timeit.timeit(lambda: write_png(compress_level), number=REPEAT)
    print(
        f"write PNG: time = {t:f} (sec); size = {len(writer.getvalue())} bytes; {compress_level = }"
    )
    results[compress_level] = (len(writer.getvalue()), t)

header = f"{Image.core.zlib_version}/{os.path.basename(PATH)}"

print()
print(header)
for size, t in results.values():
    print(t)

print()
print(header)
for size, t in results.values():
    print(size)

</details>

<details>
<summary>Output</summary>

Image.__version__ = '13.0.0'
Image.core.zlib_version = '1.2.11'
Testing PATH = 'Tests/images/hopper.png' with 1000 repetitions
read PNG: time = 0.132248 (sec)
write PNG: time = 0.713620 (sec); size = 49353 bytes; compress_level = 0
write PNG: time = 1.174655 (sec); size = 32403 bytes; compress_level = 1
write PNG: time = 1.308720 (sec); size = 32156 bytes; compress_level = 2
write PNG: time = 1.585752 (sec); size = 31792 bytes; compress_level = 3
write PNG: time = 1.898380 (sec); size = 30715 bytes; compress_level = 4
write PNG: time = 2.115117 (sec); size = 30529 bytes; compress_level = 5
write PNG: time = 2.723728 (sec); size = 30343 bytes; compress_level = 6
write PNG: time = 3.352062 (sec); size = 30277 bytes; compress_level = 7
write PNG: time = 5.432943 (sec); size = 30183 bytes; compress_level = 8
write PNG: time = 7.212371 (sec); size = 30166 bytes; compress_level = 9

1.2.11/hopper.png
0.7136203330010176
1.1746550419993582
1.3087199589990632
1.5857515010011412
1.8983796259999508
2.115117043000282
2.7237282509995566
3.352062335001392
5.432943002999309
7.212370502998965

1.2.11/hopper.png
49353
32403
32156
31792
30715
30529
30343
30277
30183
30166

Image.__version__ = '13.1.0.dev0'
Image.core.zlib_version = '1.3.1.zlib-ng'
Testing PATH = 'Tests/images/hopper.png' with 1000 repetitions
read PNG: time = 0.142703 (sec)
write PNG: time = 0.567993 (sec); size = 49353 bytes; compress_level = 0
write PNG: time = 0.806392 (sec); size = 40354 bytes; compress_level = 1
write PNG: time = 0.889203 (sec); size = 31458 bytes; compress_level = 2
write PNG: time = 0.948347 (sec); size = 31096 bytes; compress_level = 3
write PNG: time = 0.954083 (sec); size = 30836 bytes; compress_level = 4
write PNG: time = 1.040163 (sec); size = 30771 bytes; compress_level = 5
write PNG: time = 1.147940 (sec); size = 30649 bytes; compress_level = 6
write PNG: time = 1.434774 (sec); size = 30217 bytes; compress_level = 7
write PNG: time = 1.954859 (sec); size = 30175 bytes; compress_level = 8
write PNG: time = 2.796883 (sec); size = 30166 bytes; compress_level = 9

1.3.1.zlib-ng/hopper.png
0.5679931670001679
0.8063920000004146
0.8892034170003171
0.9483469169990713
0.9540826680004102
1.040163167001083
1.1479395429996657
1.4347735840001405
1.954859375999149
2.796882626000297

1.3.1.zlib-ng/hopper.png
49353
40354
31458
31096
30836
30771
30649
30217
30175
30166

Image.__version__ = '13.0.0'
Image.core.zlib_version = '1.2.11'
Testing PATH = 'Tests/images/effect_spread.png' with 1000 repetitions
read PNG: time = 0.064376 (sec)
write PNG: time = 0.670181 (sec); size = 49353 bytes; compress_level = 0
write PNG: time = 1.371527 (sec); size = 42199 bytes; compress_level = 1
write PNG: time = 1.412506 (sec); size = 42096 bytes; compress_level = 2
write PNG: time = 1.458894 (sec); size = 41977 bytes; compress_level = 3
write PNG: time = 1.690065 (sec); size = 41011 bytes; compress_level = 4
write PNG: time = 1.806561 (sec); size = 40969 bytes; compress_level = 5
write PNG: time = 1.994798 (sec); size = 40903 bytes; compress_level = 6
write PNG: time = 2.132603 (sec); size = 40896 bytes; compress_level = 7
write PNG: time = 2.306038 (sec); size = 40889 bytes; compress_level = 8
write PNG: time = 2.259252 (sec); size = 40889 bytes; compress_level = 9

1.2.11/effect_spread.png
0.670181292000052
1.3715270839984441
1.4125062499988417
1.4588938760007295
1.6900649589988461
1.8065610009998636
1.9947983750007552
2.1326034180001443
2.306038126000203
2.2592523339990294

1.2.11/effect_spread.png
49353
42199
42096
41977
41011
40969
40903
40896
40889
40889

Image.__version__ = '13.1.0.dev0'
Image.core.zlib_version = '1.3.1.zlib-ng'
Testing PATH = 'Tests/images/effect_spread.png' with 1000 repetitions
read PNG: time = 0.064945 (sec)
write PNG: time = 0.606368 (sec); size = 49353 bytes; compress_level = 0
write PNG: time = 0.857841 (sec); size = 48241 bytes; compress_level = 1
write PNG: time = 1.076261 (sec); size = 41287 bytes; compress_level = 2
write PNG: time = 1.203480 (sec); size = 41168 bytes; compress_level = 3
write PNG: time = 1.168138 (sec); size = 41134 bytes; compress_level = 4
write PNG: time = 1.651922 (sec); size = 41120 bytes; compress_level = 5
write PNG: time = 1.226500 (sec); size = 41118 bytes; compress_level = 6
write PNG: time = 1.307547 (sec); size = 40895 bytes; compress_level = 7
write PNG: time = 1.289162 (sec); size = 40889 bytes; compress_level = 8
write PNG: time = 1.855796 (sec); size = 40889 bytes; compress_level = 9

1.3.1.zlib-ng/effect_spread.png
0.6063679170001706
0.8578407090008113
1.0762609590001375
1.2034801259997039
1.1681377089989837
1.6519223760005843
1.2264999590006482
1.3075469589985005
1.2891624169988063
1.8557955010001024

1.3.1.zlib-ng/effect_spread.png
49353
48241
41287
41168
41134
41120
41118
40895
40889
40889

</details>

Time has same shape as #8500:

modest gain at level 0
growing to about 2 to 2.5x at default level 6

  • about 2.5x at level 9
  • Interestingly, the zlib-ng times at level 9 are 2.79s (#8500, Windows x86_64) versus 2.80s (here, aarch64 in a Docker VM), almost identical on different machines!

    <img width="616" height="378" alt="image" src="https://github.com/user-attachments/assets/7417ee4d-e0b1-487b-80f7-9c480b02598c" />

    File sizes are identical to #8500.

    <img width="617" height="386" alt="image" src="https://github.com/user-attachments/assets/c7a41c94-6a4d-448c-b950-0d72036d62d6" />

    Diff: 650 lines (+487 / -163) across 8 files Reviewer: radarhere Review duration: 1198m
    All Vouch review confidence checks passed
    Residual risk 0.21 = Change Risk (0.52) × (1 − Review Confidence (0.60)). Review depth and reviewer attention state are adequate.
    Model 1 · Change Risk (XGBoost)
    Score: 0.52 · 650 lines diff across 8 files
    0.52
    Model 2 · Review Depth (DistilBERT)
    Depth Score: 0.17 · 1 substantive comment(s)
    0.17
    Model 3 · Review Timing & Attention
    Session adequacy: 100% · Reviewer familiarity: 35%
    60%
    Bedrock Claude 3 · Risk Narration
    (disconnected)
    Groq
    Groq Risk Narration
    AWS Bedrock is currently disconnected. Generate high-confidence PR triage narration on-demand using Groq.
    Model 1 · Change Risk Attribution
    SHAP Weights
    Diff Size diff_size
    0.388
    Shallow Review Depth shallow_review_depth
    0.289
    Real PR Conversation Timeline
    1 comments
    "Review active on #10495"
    general weight: 0.35
    Reviewers
    Assignees
    Unassigned
    Labels
    risk: low confidence: 60.5% reviewed
    Vouch Risk Breakdown
    AI-powered review risk analysis
    Change Risk
    Change Risk · Model 1 (XGBoost)
    Predicted defect probability (0.00–1.00) based on diff churn, modified sensitive files, and author risk history.
    Score: 52% (MEDIUM) · Higher = larger blast radius
    52%
    Review Confidence
    Review Confidence · Models 2 & 3
    Scrutiny score (0.00–1.00) combining DistilBERT semantic review depth with reviewer familiarity and session dwell time.
    Score: 60.5% · Lower = rushed or shallow review
    60.5%
    Residual Risk
    Residual Risk · Core Equation
    Residual = Change × (1 − Confidence)
    Unmitigated risk surviving review. ≥ 0.65 triggers an automated re-queue to protect production.
    Score: 20.5% · Safe to merge
    20.5%