Skip to content

Speed up ImageMath - #10072

Open
akx wants to merge 2 commits into
python-pillow:mainfrom
akx:fast-math
Open

akx wants to merge 2 commits into
python-pillow:mainfrom
akx:fast-math

Conversation

@akx

@akx akx commented Sep 28, 2026 •

Copy link
Copy Markdown
Contributor

This implements the usual hoist + restrict optimizations (and documents the contracts for that) for ImageMath. It also fixes float division to vectorize (yes, we divide by zero first, and only then check if we made an oopsie).

Something I didn't address here is that ImageMath apparently allocates a dummy image for scalar operations, which is wasteful memory-wise and slower, but fixing that would involve a new category of scalar operations in the C code (maybe some 300? lines?).

On my machine, some operations speed up by half, some not much:

---------------------- benchmark 'imagemath': 17 tests, 2 sources ----------------------
Name (time in us)                           0001_aec4583 Min  0003_708f006 Min      ΔMin
----------------------------------------------------------------------------------------
test_imagemath_unary[1237x811-F-neg]                223.9590          222.7500     -0.5%
test_imagemath_unary[1237x811-F-abs]                225.2080          226.5830     +0.6%
test_imagemath_binary[1237x811-F-add]               243.0420          265.0840     +9.1%
test_imagemath_binary[1237x811-F-min]               259.8750          269.5000     +3.7%
test_imagemath_binary[1237x811-F-mul]               260.7090          251.8330     -3.4%
test_imagemath_binary[1237x811-F-lt]                262.6670          248.5000     -5.4%
test_imagemath_scalar[1237x811-F]                   438.1670          490.8330    +12.0%
test_imagemath_unary[1237x811-I-neg]                449.2090          220.8330    -50.8%
test_imagemath_unary[1237x811-I-invert]             450.2920          213.6660    -52.5%
test_imagemath_unary[1237x811-I-abs]                459.2500          218.5840    -52.4%
test_imagemath_binary[1237x811-I-mul]               492.0410          275.5840    -44.0%
test_imagemath_binary[1237x811-I-add]               494.1250          275.1670    -44.3%
test_imagemath_binary[1237x811-I-min]               495.2500          264.0410    -46.7%
test_imagemath_binary[1237x811-I-lt]                497.1660          266.7920    -46.3%
test_imagemath_scalar[1237x811-I]                   670.4170          451.7910    -32.6%
test_imagemath_binary[1237x811-F-div]               703.7920          251.7500    -64.2%
test_imagemath_binary[1237x811-I-div]               711.5000          705.5410     -0.8%
----------------------------------------------------------------------------------------

@akx
akx marked this pull request as ready for review September 28, 2026 09:09
@codspeed

codspeed Bot commented Sep 28, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 38.12%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

⚡ 7 improved benchmarks
✅ 633 untouched benchmarks
⏩ 338 skipped benchmarks1

Performance Changes

Benchmark BASE HEAD Efficiency
⚡ test_imagemath_unary[1237x811-I-abs] 23.2 ms 15.8 ms +46.39%
⚡ test_imagemath_unary[1237x811-I-neg] 22.7 ms 15.7 ms +44.65%
⚡ test_imagemath_binary[1237x811-I-lt] 29.1 ms 20.8 ms +40.35%
⚡ test_imagemath_binary[1237x811-I-min] 29.8 ms 21.3 ms +39.83%
⚡ test_imagemath_binary[1237x811-I-add] 28.8 ms 20.7 ms +39.02%
⚡ test_imagemath_binary[1237x811-I-mul] 29.1 ms 22.1 ms +31.54%
⚡ test_imagemath_scalar[1237x811-I] 33 ms 26.2 ms +26.16%

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:fast-math (e06ae60) with main (685e78a)

Open in CodSpeed

Footnotes

  1. 338 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 branch has not been deployed

No deployments
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.

2 participants