Skip to content

Speed up BMP grayscale palette detection - #10574

Closed
nietras wants to merge 3 commits into
python-pillow:mainfrom
nietras:perf-bmp-grayscale-palette
Closed

nietras wants to merge 3 commits into
python-pillow:mainfrom
nietras:perf-bmp-grayscale-palette

Conversation

@nietras

@nietras nietras commented Nov 20, 2026

Copy link
Copy Markdown

Replace BMP's Python grayscale-palette loop with an allocation-free native check and read colors once. Preserve palette padding and monochrome behavior.

Local Windows/CPython 3.12.10 benchmark, 256-entry grayscale BMPs, open()+load()+tobytes(): 4.0x faster for 32x32 (45.9 -> 11.3 us) and 3.0x for 320x320 (52.7 -> 17.5 us) using BytesIO. Warm-file gains: 1.8x and 1.4x respectively.

Validation: 77 targeted BMP tests and 59 selftests pass; native hot-path calls succeed with CPython allocations disabled.

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
@nietras

nietras commented Nov 20, 2026

Copy link
Copy Markdown
Author

First time contributor here. Feel free to make edits or even doing a different PR for this with what might be a better optimization for this, my main concern is that I would like this to be improved since it has a real impact for our ML workloads.

@radarhere

Copy link
Copy Markdown
Member

Hi. Did you use AI for this? Could you attach examples of the images you used for your speed tests?

Could you explain a bit further why opening small BMP images is such a priority for you? The natural instinct is to say that they're small, so they're likely already pretty quick. This PR replaces 8 lines of Python with 44 lines of C, which isn't exactly simpler.

@nietras

nietras commented Nov 20, 2026

Copy link
Copy Markdown
Author

Yes I used AI but heavily edited for best perf. I can't share exact images used but the PR description basically says how you can test. I understand the change involves more code, I also tried using memory slicings (no C) but most of these still involve allocations and its 2-3x slower than the C code change. Although still 10x faster than the original loop. So perhaps one could fall back to that if this PR is deemed to complex. I would think the check might be useful for other image format plugins?

We train models on small images via pytorch and use PIL for loading. Overhead for loading is very high for these palette based bmp images. And we have to use palette due to compatibility with Windows APIs (GDI+).

A simple test image is attached below.

grayscale-32x32.bmp

@nietras

nietras commented Nov 20, 2026

Copy link
Copy Markdown
Author

Also of the 44 lines of C code 34 are parameter checks, its a basic simple for loop otherwise :) I don't now if all those checks are necessary and best practice here. If there is a simple fast zero-allocation way of doing check in Python I am all ears :)

@akx akx left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think a more versatile function would be more worth it...

Comment thread src/_imaging.c
Comment on lines +4266 to +4281
if (nargs != 3) {
PyErr_SetString(PyExc_TypeError, "is_grayscale_palette requires 3 arguments");
return NULL;
}
if (!PyBytes_Check(args[0])) {
PyErr_SetString(PyExc_TypeError, "palette must be bytes");
return NULL;
}
colors = PyLong_AsSsize_t(args[1]);
if (colors == -1 && PyErr_Occurred()) {
return NULL;
}
padding = PyLong_AsSsize_t(args[2]);
if (padding == -1 && PyErr_Occurred()) {
return NULL;
}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

The native code in Pillow generally uses PyArg_ParseTuple() instead of doing this by hand.

Comment thread src/_imaging.c
/* -------------------------------------------------------------------- */

static PyObject *
_is_grayscale_palette(PyObject *self, PyObject *const *args, Py_ssize_t nargs) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Palettes are a separate concept in Pillow -- I'd name this somehow differently, since what it does is, uh... read a bytes argument that is expected to be RGB/RGBX u8 values, and checks that they're in strictly ascending order?

Copy link
Copy Markdown
Author

Choose a reason for hiding this comment

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

all the existing comments and code call this palette? and this is also what its called in file format, I think, would it help if it said palette + "colors" or "color bytes"?

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I mean, isn't this specific to the particular format in BMPs before it is unserialized into a Pillow palette?

@radarhere

Copy link
Copy Markdown
Member

I've created #10580 as an alternative. It should have the same performance improvement.

@nietras

nietras commented Nov 22, 2026 •

Copy link
Copy Markdown
Author

@radarhere your alternative looks good too and still shaves of those 35 us per load like mine. Feel free to proceed as you wish with this and close this PR. Or let me know if I should do something.

@radarhere

Copy link
Copy Markdown
Member

Thanks. #10580 has been merged

@radarhere radarhere closed this Nov 22, 2026
@nietras

nietras commented Nov 22, 2026

Copy link
Copy Markdown
Author

@radarhere thank you! and thanks for getting this done quickly.

@nietras
nietras deleted the perf-bmp-grayscale-palette branch November 22, 2026 10:55
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