Repository navigation
Conversation
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
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. |
|
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. |
|
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. |
|
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
left a comment
There was a problem hiding this comment.
I think a more versatile function would be more worth it...
| 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; | ||
| } |
There was a problem hiding this comment.
The native code in Pillow generally uses PyArg_ParseTuple() instead of doing this by hand.
| /* -------------------------------------------------------------------- */ | ||
|
|
||
| static PyObject * | ||
| _is_grayscale_palette(PyObject *self, PyObject *const *args, Py_ssize_t nargs) { |
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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"?
There was a problem hiding this comment.
I mean, isn't this specific to the particular format in BMPs before it is unserialized into a Pillow palette?
|
I've created #10580 as an alternative. It should have the same performance improvement. |
|
@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. |
|
Thanks. #10580 has been merged |
|
@radarhere thank you! and thanks for getting this done quickly. |
Replace BMP's Python grayscale-palette loop with an allocation-free native check and read
colorsonce. 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) usingBytesIO. 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.