-
-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Improve memory usage and performance for tobytes()
#9938
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
akx
wants to merge
5
commits into
python-pillow:main
Choose a base branch
from
akx:tobytes-buf-size
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
de41424
Add Encoder.optimal_bufsize protocol
akx 64507dc
Document PyImaging_RawEncoderNew
akx 09f7646
Add optimal_bufsize for raw encoder
akx e1fe8b2
Wire up e.optimal_bufsize, with copious comments
akx de42638
Add changelog for improved raw encode
akx File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,91 @@ | ||
| from __future__ import annotations | ||
|
|
||
| import pytest | ||
|
|
||
| from PIL import Image | ||
|
|
||
| TYPE_CHECKING = False | ||
| if TYPE_CHECKING: | ||
| from typing import Any | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| "mode", | ||
| ("1", "L", "P", "LA", "I", "F", "I;16", "RGB", "RGBA", "CMYK"), | ||
| ) | ||
| def test_raw_encoder_optimal_bufsize(mode: str) -> None: | ||
| # Test the raw encoder does know the exact buffer size. | ||
| im = Image.new(mode, (300, 200)) | ||
| encoder = Image._getencoder(mode, "raw", mode) | ||
| encoder.setimage(im.im, (0, 0) + im.size) | ||
| assert encoder.optimal_bufsize == len(im.tobytes()) | ||
|
|
||
|
|
||
| def test_raw_encoder_optimal_bufsize_stride() -> None: | ||
| im = Image.new("RGB", (100, 50)) | ||
| encoder = Image._getencoder("RGB", "raw", ("RGB", 400)) | ||
| encoder.setimage(im.im, (0, 0) + im.size) | ||
| assert encoder.optimal_bufsize == 400 * 50 | ||
| encoder.encode(400) # The encoder's internals change here | ||
| # ... but the result for this should remain the same. | ||
| assert encoder.optimal_bufsize == 400 * 50 | ||
|
|
||
|
|
||
| def test_encoder_optimal_bufsize_unknown() -> None: | ||
| im = Image.new("1", (64, 64)) | ||
|
|
||
| # the size is not known before the image has been assigned | ||
| encoder = Image._getencoder("1", "raw", "1") | ||
| assert encoder.optimal_bufsize == 0 | ||
|
|
||
| # nor for encoders whose output size depends on the pixel data | ||
| encoder = Image._getencoder("1", "xbm", "1") | ||
| encoder.setimage(im.im, (0, 0) + im.size) | ||
| assert encoder.optimal_bufsize == 0 | ||
|
|
||
|
|
||
| class EncoderSpy: | ||
| def __init__(self, encoder: Any, *, bufsizes: list[int]) -> None: | ||
| self._encoder = encoder | ||
| self.bufsizes = bufsizes | ||
|
|
||
| def __getattr__(self, name: str) -> Any: | ||
| return getattr(self._encoder, name) | ||
|
|
||
| def encode(self, bufsize: int) -> tuple[int, int, bytes]: | ||
| self.bufsizes.append(bufsize) | ||
| return self._encoder.encode(bufsize) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("mode", ("L", "RGB")) | ||
| @pytest.mark.parametrize( | ||
| "size", | ||
| ( | ||
| (20000, 1), | ||
| (1, 20000), | ||
| (3, 3), | ||
| (500, 500), | ||
| ), | ||
| ) | ||
| def test_tobytes_exact_buffer_and_single_pass( | ||
| mode: str, | ||
| size: tuple[int, int], | ||
| monkeypatch: pytest.MonkeyPatch, | ||
| ) -> None: | ||
| """ | ||
| Test that the raw encoder gets asked exactly the correct size, | ||
| and ends up doing its work in a single pass. | ||
| """ | ||
| bufsizes: list[int] = [] | ||
| get_encoder = Image._getencoder | ||
| monkeypatch.setattr( | ||
| Image, | ||
| "_getencoder", | ||
| lambda *args: EncoderSpy(get_encoder(*args), bufsizes=bufsizes), | ||
| ) | ||
|
|
||
| im = Image.new(mode, size) | ||
| n_bands = len(im.getbands()) | ||
| expected_size = size[0] * size[1] * n_bands | ||
| assert len(im.tobytes()) == expected_size | ||
| assert bufsizes == [expected_size] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This isn't referring to an existing PR, right? You're presuming it will happen?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
No existing PR, it's just a note for a future implementer, be that me or someone else 😅