Make .quantize(palette=...) use exact colors when possible - #9829
Conversation
7708c8a to
3eb5509
Compare
Merging this PR will degrade performance by 23.08%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ❌ | test_quantize_to_palette[1237x811-exact-flower2-P-none] |
11.1 ms | 17.2 ms | -35.4% |
| ❌ | test_quantize_to_palette[1237x811-exact-synthetic-P-none] |
11.1 ms | 16.9 ms | -34.63% |
| ❌ | test_quantize_to_palette[1237x811-exact-flower2-PA-none] |
16.1 ms | 21.3 ms | -24.61% |
| ❌ | test_quantize_to_palette[1237x811-exact-synthetic-PA-none] |
16 ms | 21.1 ms | -23.82% |
| ❌ | test_quantize_to_palette[1237x811-exact-flower2-P-floyd-steinberg] |
37.2 ms | 45.5 ms | -18.35% |
| ❌ | test_quantize_to_palette[1237x811-exact-synthetic-P-floyd-steinberg] |
37.1 ms | 45.2 ms | -17.9% |
| ❌ | test_quantize_to_palette[1237x811-exact-flower2-PA-floyd-steinberg] |
43 ms | 49.7 ms | -13.42% |
| ❌ | test_quantize_to_palette[1237x811-exact-synthetic-PA-floyd-steinberg] |
43 ms | 49.4 ms | -12.96% |
Tip
Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.
Comparing akx:palettize-fixes (7fa4a06) with main (c8c74c8)
Footnotes
-
335 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
This affects `.quantize(palette=...)`. Refs python-pillow#1852
They were used nowhere else. Keeping them together, as a private API, allows for future improvements to the palette cache's shape.
|
I think we'll have to accept some performance degradation in exchange for actually making conversions accurate... |
Stacked on #9831 so we're measuring the right things...
Refs #1852 (fixes the reproducer bug, but we're still not necessarily selecting the mathematically nearest color when quantizing to a given palette).
topalette()conversion function to a separate file for ease of understandingtopalette()function to multiple functions for less nesting and ease of reading... and then the main thing:
ImagingPaletteCache*functions are made private, since the structure of the cache was undocumented and private anyway; we need to add a new flag in the cache entries to have a better guess of when we might have an exact match for a color.