Skip to content

Speed up image comparison - #10030

Merged
radarhere merged 16 commits into
python-pillow:mainfrom
akx:image-equals-new
Nov 17, 2026
Merged

radarhere merged 16 commits into
python-pillow:mainfrom
akx:image-equals-new

Conversation

@akx

@akx akx commented Sep 20, 2026 •

Copy link
Copy Markdown
Contributor

Closes #7260 (supersedes it).

I added a benchmark, and stacked the tests from the original (adjusted to pass lints, etc.) to be before the reimplementation in the PR, then added some more corner case tests.

I verified, with interactive rebase, that the tests pass just before the new handler is added, and then separately afterwards.

Local benchmark score improvement (unsurprising, and this isn't measuring peak memory usage improvements!):

------------------- benchmark 'compare': 15 tests, 2 sources -------------------
Name (time in ns)                          0001_main Min  0002_new Min      ΔMin
--------------------------------------------------------------------------------
test_equality[1237x811-L-inverted]          122,875.0916      470.7952    -99.6%
test_equality[1237x811-L-equal]             125,959.0499   24,207.9841    -80.8%
test_equality[1237x811-L-one-pixel]         129,332.9988   12,415.9269    -90.4%
test_equality[1237x811-I;16-one-pixel]      343,374.9080   27,542.0025    -92.0%
test_equality[1237x811-LA-inverted]         343,417.0503      479.1538    -99.9%
test_equality[1237x811-I;16-equal]          380,958.0812   55,041.9791    -85.6%
test_equality[1237x811-I;16-inverted]       387,958.9494      479.0956    -99.9%
test_equality[1237x811-LA-one-pixel]        451,375.0318  189,000.0422    -58.1%
test_equality[1237x811-LA-equal]            463,792.0065  378,541.9976    -18.4%
test_equality[1237x811-RGBA-inverted]       727,667.0076      483.2982    -99.9%
test_equality[1237x811-RGB-inverted]        754,792.0104      479.2004    -99.9%
test_equality[1237x811-RGB-equal]           813,374.9943  378,374.9416    -53.5%
test_equality[1237x811-RGB-one-pixel]       823,125.0104  189,290.9640    -77.0%
test_equality[1237x811-RGBA-equal]          874,208.0536  105,833.0527    -87.9%
test_equality[1237x811-RGBA-one-pixel]      899,208.0111   52,874.9079    -94.1%
--------------------------------------------------------------------------------

EDIT: added one more commit for autovectorisation to make RGB (likely very common!) and LA even faster (at least on my hardware):


--------------------- benchmark 'compare': 15 tests, 2 sources ---------------------
Name (time in ns)                          0001_main Min  0005_equalve Min      ΔMin
------------------------------------------------------------------------------------
test_equality[1237x811-L-inverted]          122,875.0916          479.0956    -99.6%
test_equality[1237x811-L-equal]             125,959.0499       24,333.0142    -80.7%
test_equality[1237x811-L-one-pixel]         129,332.9988       12,416.9746    -90.4%
test_equality[1237x811-I;16-one-pixel]      343,374.9080       27,625.0066    -92.0%
test_equality[1237x811-LA-inverted]         343,417.0503          479.2004    -99.9%
test_equality[1237x811-I;16-equal]          380,958.0812       55,207.9873    -85.5%
test_equality[1237x811-I;16-inverted]       387,958.9494          479.2004    -99.9%
test_equality[1237x811-LA-one-pixel]        451,375.0318       43,249.9219    -90.4%
test_equality[1237x811-LA-equal]            463,792.0065       86,874.9339    -81.3%
test_equality[1237x811-RGBA-inverted]       727,667.0076          485.3995    -99.9%
test_equality[1237x811-RGB-inverted]        754,792.0104          483.2982    -99.9%
test_equality[1237x811-RGB-equal]           813,374.9943       86,790.9985    -89.3%
test_equality[1237x811-RGB-one-pixel]       823,125.0104       43,459.0038    -94.7%
test_equality[1237x811-RGBA-equal]          874,208.0536      105,957.9663    -87.9%
test_equality[1237x811-RGBA-one-pixel]      899,208.0111       52,916.9338    -94.1%
------------------------------------------------------------------------------------

@akx
akx marked this pull request as ready for review September 21, 2026 05:20
@akx akx changed the title Add tp_richcompare handler for ImagingCore Speed up image comparison Sep 21, 2026
@akx

This comment was marked as resolved.

@hugovk

This comment was marked as resolved.

@akx

This comment was marked as resolved.

Comment thread src/PIL/Image.py Resolved
Comment thread src/PIL/Image.py Resolved
Comment thread src/PIL/Image.py Resolved
@codspeed

codspeed Bot commented Sep 21, 2026 •

Copy link
Copy Markdown

Merging this PR will improve performance by ×34

3 warnings

⚠️ 3 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 15 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

⚠️ 15 benchmarks with a significant change ran in a different software environment

Their base and head results were measured with a different OS or toolchain, such as compiler, runtime or library versions, so the changes might come from this difference rather than from the code. Open an affected benchmark to see what changed.

See affected benchmarks

⚡ 15 improved benchmarks
✅ 653 untouched benchmarks
⏩ 352 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_equality[1237x811-RGBA-inverted] 53,068.5 µs 61.2 µs ×870
⚡ test_equality[1237x811-RGB-inverted] 49,676 µs 61.1 µs ×810
⚡ test_equality[1237x811-LA-inverted] 29,057.2 µs 61 µs ×480
⚡ test_equality[1237x811-I;16-inverted] 25,210.6 µs 61.6 µs ×410
⚡ test_equality[1237x811-L-inverted] 12,657.4 µs 60.7 µs ×210
⚡ test_equality[1237x811-RGBA-one-pixel] 55.6 ms 3.7 ms ×15
⚡ test_equality[1237x811-RGB-one-pixel] 51.4 ms 3.7 ms ×14
⚡ test_equality[1237x811-I;16-one-pixel] 25.9 ms 1.9 ms ×14
⚡ test_equality[1237x811-L-one-pixel] 12,872.8 µs 981.7 µs ×13
⚡ test_equality[1237x811-LA-one-pixel] 30.1 ms 3.7 ms ×8.1
⚡ test_equality[1237x811-RGBA-equal] 57.9 ms 7.4 ms ×7.8
⚡ test_equality[1237x811-RGB-equal] 53.1 ms 7.4 ms ×7.2
⚡ test_equality[1237x811-I;16-equal] 26.6 ms 3.7 ms ×7.1
⚡ test_equality[1237x811-L-equal] 13.1 ms 1.9 ms ×6.9
⚡ test_equality[1237x811-LA-equal] 31.2 ms 7.4 ms ×4.2

Tip

Curious why performance improved? Comment @codspeedbot explain why performance improved on this PR, or directly use the CodSpeed MCP with your agent.


Comparing akx:image-equals-new (47c923c) with main (3c4e2ef)

Open in CodSpeed

Footnotes

  1. 352 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. ↩

@akx

akx commented Sep 21, 2026

Copy link
Copy Markdown
Contributor Author

And the memory usage comparison that the benchmark doesn't capture shows that comparisons now require no extra allocations:

On a 2053x2039 image:

Mode main == (MiB) new == (MiB) main peak (MiB) new peak (MiB)
1 3.7 0.0 11.8 8.1
CMYK 57.7 0.0 89.8 32.0
F 56.8 0.0 88.8 32.0
HSV 42.0 0.0 74.0 32.0
I 56.8 0.0 88.8 32.0
I;16 28.7 0.0 44.8 16.0
I;16B 39.9 0.0 55.9 16.0
I;16L 29.5 0.0 45.5 16.0
I;16N 28.7 0.0 44.8 16.0
L 15.2 0.0 23.3 8.1
LA 28.7 0.0 60.7 32.0
LAB 53.8 0.0 85.9 32.1
La 28.7 0.0 60.7 32.0
P 16.9 0.0 25.2 8.2
PA 28.8 0.0 61.0 32.1
RGB 42.0 0.0 74.0 32.0
RGBA 56.8 0.0 88.8 32.0
RGBX 56.8 0.0 88.8 32.0
RGBa 56.8 0.0 88.8 32.0
YCbCr 42.0 0.0 74.0 32.0

Comment thread docs/releasenotes/13.0.0.rst Resolved
@akx akx changed the title Speed up image comparison Speed up image comparison and reduce memory usage Sep 25, 2026
This was referenced Sep 28, 2026
@akx
akx force-pushed the image-equals-new branch from a125a44 to 763fdef Compare October 4, 2026 14:09
@akx
akx requested a review from radarhere October 4, 2026 20:14
Comment thread docs/releasenotes/13.0.0.rst Resolved
@akx akx mentioned this pull request Oct 11, 2026
14 tasks done
@aclark4life

aclark4life commented Oct 11, 2026 •

Copy link
Copy Markdown
Member

Hmmm, I think this changes the result of == for palette images (in the public API) e.g.

import io
from PIL import Image

def pal(data, mode):
    im = Image.new("P", (2, 2))
    im.putpalette(data, mode)
    return im

# Palettes that differ only in alpha
print(pal([1, 2, 3, 255] * 256, "RGBA") == pal([1, 2, 3, 0] * 256, "RGBA"))
# main: True, this PR: False

# Same colours, RGB palette vs fully opaque RGBA palette
rgb = pal([1, 2, 3] * 256, "RGB")
rgba = pal([1, 2, 3, 255] * 256, "RGBA")
print(rgb == rgba)
# main: True, this PR: False

# But they don't save the same
a, b = io.BytesIO(), io.BytesIO()
rgb.save(a, "PNG")
rgba.save(b, "PNG")
print(a.getvalue() == b.getvalue())
# False

Both now return False which seems right. The second pair has the same colors but the images aren't interchangeable since they save to different PNG files. Could you please:

  • add tests for these cases so this stays the intended behavior and
  • mention in the release note that comparing images now takes palette alpha and palette mode into account rather than only that "comparison is faster"?

Thank you @akx!

@akx

akx commented Oct 11, 2026

Copy link
Copy Markdown
Contributor Author

@aclark4life Great catch! And ah yeah, that's, in the preimage, us getting bitten by getpalette() actually being getpalette("RGB") (the implicit default having been touched upon in #9834 (comment)) so that comparison dropped alpha altogether.

I'll add a commit to add a couple regression tests and note this in the release notes 👍

Cheers from Arifuku Onsen, Japan, btw :)

@akx
akx force-pushed the image-equals-new branch 3 times, most recently from 4f9ee6a to 8347bc9 Compare October 11, 2026 13:31
Yay295 and others added 5 commits October 18, 2026 09:11
* de-parametrize test_not_equal_mode_1
* add type annotations
* test the Image's behavior, not the Core's
Co-authored-by: Yay295 <Yay295@gmail.com>
Co-authored-by: Andrew Murray <radarhere@users.noreply.github.com>
@akx
akx force-pushed the image-equals-new branch from 36a681c to f66d9a3 Compare October 18, 2026 00:13
@akx
akx force-pushed the image-equals-new branch from 135a982 to 9dca7bc Compare October 18, 2026 00:15
Comment thread src/_imaging.c Resolved
Comment thread Tests/test_image_equality.py Resolved
@akx
akx requested a review from radarhere November 2, 2026 08:23
Comment thread src/_imaging.c Outdated

// Fortunately, all of the modes that have extra bytes in their pixels
// use four bytes for their pixels. RGBX is deliberately not one of them:
// its fourth byte is compared just like Image.tobytes() includes it.

@radarhere radarhere Nov 16, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why RGBX should be treated differently? If anything, I think tobytes() shouldn't include it.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

IIRC, this was to retain compatibility with main (which compared RGBX images by tobytes()ing).

@radarhere
radarhere force-pushed the image-equals-new branch 2 times, most recently from 8951ee6 to 400ddaa Compare November 17, 2026 02:03
@radarhere radarhere changed the title Speed up image comparison and reduce memory usage Speed up image comparison Nov 17, 2026
@radarhere
radarhere enabled auto-merge (squash) November 17, 2026 02:04
@radarhere

Copy link
Copy Markdown
Member

Some of the tests were more about frombuffer than they were about equality, so I removed them.

@radarhere
radarhere merged commit a67669b into python-pillow:main Nov 17, 2026
64 checks passed
@akx

akx commented Nov 17, 2026

Copy link
Copy Markdown
Contributor Author

The frombuffer-related tests were there to make sure the esoteric map/negative-ystep/stride combinations you can achieve with frombuffer() also compared correctly.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants