Skip to content

Apply restrict optimizations to ImagingQuantize's output loop - #10006

Merged
radarhere merged 2 commits into
python-pillow:mainfrom
akx:quant-restrict
Sep 16, 2026
Merged

radarhere merged 2 commits into
python-pillow:mainfrom
akx:quant-restrict

Conversation

@akx

@akx akx commented Sep 16, 2026

Copy link
Copy Markdown
Contributor

Follows up on #9752 (comment) and #9740.

None of the other loops in the quantization "frontend" were eligible for restrict, but this does speed up quantization a little bit, more so when the quantization method itself is fast (such as FastOctree).

Note the below local benchmark is with larger images than the default. CodSpeed probably won't show comparisons here since the test IDs change.

------------------------ benchmark 'quantize': 5 tests, 2 sources -----------------------
Name (time in ms)                            0001_5b0d565 Min  0002_b945920 Min      ΔMin
-----------------------------------------------------------------------------------------
test_quantize[2474x1622-RGBA-FASTOCTREE]              12.9134           11.4034    -11.7%
test_quantize[2474x1622-L-MAXCOVERAGE]                79.4019           77.5946     -2.3%
test_quantize[2474x1622-L-MEDIANCUT]                 141.6351          140.1765     -1.0%
test_quantize[2474x1622-RGB-MEDIANCUT]               392.6901          382.9610     -2.5%
test_quantize[2474x1622-RGB-MAXCOVERAGE]           4,097.8388        4,015.4798     -2.0%
-----------------------------------------------------------------------------------------

I also noticed MAXCOVERAGE is painfully slow with images with a large number of distinct colors, but I think that would require a more thorough rework. By and large, the quantization code (and its hash table code) is from 1998 or so, written with machines from that era in mind.

Comment thread Tests/benchmarks.py
@akx
akx marked this pull request as ready for review September 16, 2026 08:48
@radarhere
radarhere merged commit b046cd8 into python-pillow:main Sep 16, 2026
57 checks passed
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.

2 participants