Improve memory usage and performance for tobytes() - #9938
Conversation
|
Oh, and for proof, with this reproducer gist: and |
Merging this PR will improve performance by ×2
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | test_pack[1237x811-RGBA] |
9.7 ms | 3.9 ms | ×2.5 |
| ⚡ | test_pack[1237x811-L] |
2.4 ms | 1 ms | ×2.3 |
| ⚡ | test_pack[1237x811-LA] |
6.3 ms | 3.6 ms | +73.95% |
| ⚡ | test_pack[1237x811-RGB] |
9.8 ms | 5.8 ms | +69.47% |
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:tobytes-buf-size (de42638) with main (c3078e7)2
Footnotes
-
335 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. ↩
-
No successful run was found on
main(23cbae1) during the generation of this report, so c3078e7 was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
This comment was marked as outdated.
This comment was marked as outdated.
| Py_ssize_t bufsize = row * state->ysize; | ||
| // Cap the size to the whole number of rows that fits an `int`. | ||
| // TODO: this needs to be changed when encoders' buffer sizes become `ssize_t`, | ||
| // like decoders did in ca1cf5925. |
There was a problem hiding this comment.
This isn't referring to an existing PR, right? You're presuming it will happen?
There was a problem hiding this comment.
No existing PR, it's just a note for a future implementer, be that me or someone else 😅
Closes #9220 (supersedes it).
This is an extension of my comment on #9220 (comment):
optimal_bufsizeproperty, the number of bytes needed in a byte buffer to encompass the whole encoding.Image.tobytes()now checks the optimal bufsize after it initializes the encoder, and passes it through if known; otherwise, the same old logic (the larger ofMAXBLOCK, or the memory required to encode a single 8-bit RGBA row) is used.tobytes()is being used in surprising places around Pillow (see the changelog fragment), so this should have pleasant effects all around...