Skip to content

Speed up Image.resample()#9739

Open
akx wants to merge 2 commits into
python-pillow:mainfrom
akx:faster-resample
Open

Speed up Image.resample()#9739
akx wants to merge 2 commits into
python-pillow:mainfrom
akx:faster-resample

Conversation

@akx

@akx akx commented Jun 30, 2026

Copy link
Copy Markdown
Contributor

Refs #9649, #9675, #9736, #9737, #9738 (same series, same techniques).

Follows up on #1881.

@codspeed-hq

codspeed-hq Bot commented Jun 30, 2026

Copy link
Copy Markdown

Merging this PR will improve performance by 14.75%

⚠️ 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

⚡ 23 improved benchmarks
✅ 308 untouched benchmarks

Performance Changes

Benchmark BASE HEAD Efficiency
test_scale[1024x1024-L-0.8-BOX] 21.5 ms 17.1 ms +25.55%
test_scale[1024x1024-L-2.14-BOX] 87.6 ms 70.9 ms +23.58%
test_scale[1024x1024-L-0.8-BILINEAR] 27.3 ms 22.9 ms +19.12%
test_scale[1024x1024-L-0.8-HAMMING] 27.5 ms 23.1 ms +18.95%
test_scale[1024x1024-I-2.14-BOX] 92.4 ms 78 ms +18.49%
test_scale[1024x1024-L-2.14-BILINEAR] 109.9 ms 93.2 ms +17.95%
test_scale[1024x1024-L-2.14-HAMMING] 110.4 ms 93.6 ms +17.86%
test_scale[1024x1024-RGB-0.8-BOX] 43.1 ms 37.2 ms +15.73%
test_scale[1024x1024-RGB-2.14-BOX] 184.5 ms 161.3 ms +14.39%
test_scale[1024x1024-I-0.8-BOX] 22.6 ms 19.7 ms +14.38%
test_scale[1024x1024-I-2.14-BILINEAR] 116.6 ms 102.2 ms +14.11%
test_scale[1024x1024-I-2.14-HAMMING] 117.1 ms 102.7 ms +14.05%
test_scale[1024x1024-L-0.8-BICUBIC] 39 ms 34.5 ms +12.82%
test_scale[1024x1024-RGB-0.8-BILINEAR] 54.8 ms 48.9 ms +12.13%
test_scale[1024x1024-L-2.14-BICUBIC] 154.9 ms 138.2 ms +12.1%
test_scale[1024x1024-RGB-0.8-HAMMING] 55 ms 49.1 ms +11.98%
test_scale[1024x1024-LA-0.8-BOX] 49.4 ms 44.2 ms +11.85%
test_scale[1024x1024-RGB-2.14-BILINEAR] 226.9 ms 203.7 ms +11.4%
test_scale[1024x1024-RGB-2.14-HAMMING] 227.4 ms 204.2 ms +11.37%
test_scale[1024x1024-LA-2.14-BOX] 207.9 ms 187.2 ms +11.02%
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

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


Comparing akx:faster-resample (e4450cf) with main (6590b1b)

Open in CodSpeed

@akx

akx commented Jun 30, 2026

Copy link
Copy Markdown
Contributor Author

Well, those assertion failures are surprising... 🤔

No, they're not, I'm just a dummy and I copy-pasted an image8 assertion where we needed image32. However, the actually surprising thing is that Windows tests didn't assert 🤔

@akx akx force-pushed the faster-resample branch from 3aece98 to 834abe6 Compare June 30, 2026 09:56
@akx akx force-pushed the faster-resample branch from f1ce5c3 to e4450cf Compare June 30, 2026 09:59
@akx akx marked this pull request as ready for review June 30, 2026 10:37
Comment thread src/libImaging/Resample.c
for (yy = 0; yy < imOut->ysize; yy++) {
for (xx = 0; xx < imOut->xsize; xx++) {
// Verify caller allocated distinct buffers.
assert(imIn->image8 != imOut->image8);

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.

Just for my understanding, why does this need an assert, when similar changes you've made elsewhere don't?

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.

I added defensive assert()s here since the call chain is more involved than with the other optimizations in this series (where it's plain to see that there's an ImagingNewDirty just nearby).

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.

3 participants