-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
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
Replace Windows zlib with zlib-ng #8495
Conversation
The changes look OK (although we might want to consider switching to the CMake build at some point as it seems to be more actively maintaned). We probably want to add a flag in Lines 4352 to 4368 in 822ec3d
Lines 299 to 302 in 822ec3d
It is currently reporting zlib version
Can you provide the benchmark itself? (I imagine its only a couple of lines, but it would make it easier to compare with others. Using one of our test images could make it even simpler.) It looks like zlib-ng produces a slightly larger file. This is probably fine, but I would be curious what effect changing the Also, do note that the FreeType library also uses zlib for decompressing font files, although I do not expect any issues there. |
ARM Windows build fails, so I think we should switch to CMake build. For version number, zlib-ng reports compatible zlib version in zlib-compat API. Here is the bench script:import os
import timeit
from PIL import Image
REPEAT = 10
print(f'{Image.__version__ = }')
print(f'{Image.core.zlib_version = }')
def read_png():
img = Image.open('example.png')
img.load()
t = timeit.timeit(read_png, number=REPEAT)
print(f'read PNG: {t:f} (sec)')
img = Image.open('example.png')
img.load()
fp = open(os.devnull, "wb")
t = timeit.timeit(lambda: img.save(fp, format='png'), number=REPEAT)
print(f'on-memory encoding PNG: {t:f} (sec)')
t = timeit.timeit(lambda: img.save('pil_png.png'), number=REPEAT)
print(f'write PNG: {t:f} (sec)')
print(f'PNG size {os.path.getsize("pil_png.png")} bytes') |
for more information, see https://pre-commit.ci
Is it worth re-benchmarking these other forks or finding some newer results? |
@hugovk Maybe yes. But it's quite hard problem and I don't want to go deeper. There are some newer benchmarks: Anyway, the purpose of this PR is free speedup without much effort. So I considered about easy application and portability. There are several alternative to zlib:
So I choose zlib-ng. |
My point being that we should report the zlib-ng version when using it, not the version of the zlib-compat API. This is to make it clear which version is used in tests and bug reports. |
I have created #8500 as an alternative using the CMake build instead, as we can use a newer version that way. I also added a feature flag so that the use of zlib-ng is clear from running
Thanks for that, I've adjusted it slightly to also test at various compression levels. It seems that in compression, zlib-ng usually produces a slightly larger file but in much less time. |
@radarhere |
If we're going to start using zlib-ng, then I'd like to see if we can use it on other platforms as well - I've created nulano#42 to add it on Linux for example. The next Pillow release is scheduled for January 2, so as long as it is merged before then, we should be all good. |
#8500 has now been merged. |
Changes proposed in this pull request:
Here are my quick and dirty benchmark.
(on x64 Windows, read/write PNG 10 times)
Original (with zlib):
With zlib-ng:
It's about 2x speed-up with little effort.
Comparison & benchmark of zlib forks (JAN 2021):
https://aws.amazon.com/blogs/opensource/improving-zlib-cloudflare-and-comparing-performance-with-other-zlib-forks/