GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — bpo-43613: Faster implementation of gzip.compress and gzip.decompress by rhpvorderman · Pull Request #27941 · python/cpython

Conversation

@rhpvorderman

@rhpvorderman rhpvorderman commented Aug 25, 2021 •
edited
Loading

Uh oh!

There was an error while loading. Please reload this page.

Copy link
Copy Markdown
Contributor

This also includes the changes from #25011 for bpo-43612. These make more sense in the context of these changes.

Currently gzip.compress and gzip.decompress are implemented with GzipFile. This is a lot of overhead when a simple in memory compression is needed. As shown in the benchmarks below, the overhead is considerable for datasizes below 4096 bytes (which are probably very common targets for in memory compression and decompression).

This PR changes the implementations to compress and decompress in memory.

I compiled python before and after this change with --enable-optimizations to ensure a fair comparison.
I used this script to benchmark:

import gzip
import pathlib
import statistics
import sys
import timeit

DATA=pathlib.Path(sys.argv[1]).read_bytes()

SIZES = [0, 128, 512, 1024, 4096, 8192, 16384]

def benchmark(bench_string, number=1000, repetitions=10):
    for size in SIZES:
        data = DATA[:size]
        compressed_data = gzip.compress(data)
        timeit_kwargs=dict(globals=dict(**locals(), **globals()),
                           number=number)
        results = [timeit.timeit(bench_string, **timeit_kwargs) for _ in range(repetitions)]
        average = statistics.mean(results)
        print(f"Data size {size}: {round(average * (1_000_000 / number),2)} microseconds average")

if __name__ == "__main__":
    print("gzip compression")
    benchmark("gzip.compress(compressed_data)")
    print()
    print("gzip decompression")
    benchmark("gzip.decompress(compressed_data)")

Before:

gzip compression
Data size 0: 7.92 microseconds average
Data size 128: 12.1 microseconds average
Data size 512: 18.45 microseconds average
Data size 1024: 22.41 microseconds average
Data size 4096: 32.51 microseconds average
Data size 8192: 41.03 microseconds average
Data size 16384: 57.99 microseconds average

gzip decompression
Data size 0: 8.99 microseconds average
Data size 128: 10.26 microseconds average
Data size 512: 12.62 microseconds average
Data size 1024: 13.55 microseconds average
Data size 4096: 21.12 microseconds average
Data size 8192: 30.59 microseconds average
Data size 16384: 61.24 microseconds average

After:

gzip compression
Data size 0: 3.68 microseconds average
Data size 128: 7.64 microseconds average
Data size 512: 14.06 microseconds average
Data size 1024: 17.42 microseconds average
Data size 4096: 27.25 microseconds average
Data size 8192: 37.09 microseconds average
Data size 16384: 53.48 microseconds average

gzip decompression
Data size 0: 1.98 microseconds average
Data size 128: 3.74 microseconds average
Data size 512: 5.36 microseconds average
Data size 1024: 6.72 microseconds average
Data size 4096: 14.1 microseconds average
Data size 8192: 23.57 microseconds average
Data size 16384: 52.72 microseconds average

https://bugs.python.org/issue43613

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

All reactions
rhpvorderman added 19 commits August 25, 2021 10:57
@rhpvorderman
Add test for zlib.compress wbits
608876c
@rhpvorderman
Add wbits argument to zlib.compress
12fb5a5
@rhpvorderman
Use clinic to generate input
1b34510
@rhpvorderman
Update documentation for zlib
4e90311
@rhpvorderman
Add blurb news entry for zlib.compress wbits parameter
9717252
@rhpvorderman
Fix doc typo
d70390e
@rhpvorderman
Remove unnecessary whitespace, add punctionation and complete sentences.
c819e3e
@rhpvorderman
Break line to comply with PEP-7
1f3481f
@rhpvorderman
Update blurb to include :func: reference
8019932
@rhpvorderman
Remove erroneous double backticks
0ea98cf
@rhpvorderman
Faster gzip.compress implementation
d1c86dc
@rhpvorderman
More efficiently decompress gzip files in memory
19a0358
@rhpvorderman
Ensure correct endianness
5155857
@rhpvorderman
Remove redundant line
fa188a6
@rhpvorderman
Fix typos and test errors
4e76cf5
@rhpvorderman
Revert changing default on compress for backwards compatibility
0280c95
@rhpvorderman
Update documentation with gzip speed improvements
8ddee29
@rhpvorderman
Add a blurb for gzip speed improvements
77f79fd
@rhpvorderman
Use + instead of bytes.join() method
ca3e543
@the-knights-who-say-ni the-knights-who-say-ni added the CLA signed label Aug 25, 2021
@bedevere-bot bedevere-bot added the awaiting review label Aug 25, 2021
@rhpvorderman rhpvorderman mentioned this pull request Aug 25, 2021
bpo-43612: Add wbits parameter to zlib.compress #25011
Closed
ambv
ambv reviewed Aug 25, 2021
View reviewed changes
Comment thread Lib/gzip.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ambv
ambv reviewed Aug 25, 2021
View reviewed changes
Comment thread Lib/test/test_zlib.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

ambv
ambv reviewed Aug 25, 2021
View reviewed changes
Comment thread Lib/gzip.py Resolved

Uh oh!

There was an error while loading. Please reload this page.

rhpvorderman and others added 3 commits August 30, 2021 14:23
@rhpvorderman @ambv
Reword docstring for read_gzip_header
f881f7e
Co-authored-by: Łukasz Langa <[email protected]>
@rhpvorderman
Update docstring for gzip.compress
97a8100
@rhpvorderman @ambv
Use subtest for zlib.compress/decompress test.
eeb7766
Co-authored-by: Łukasz Langa <[email protected]>
@rhpvorderman

rhpvorderman commented Aug 30, 2021

Copy link
Copy Markdown
Contributor Author

Thanks for your review and suggestions @ambv! I updated the code.

All reactions

Sorry, something went wrong.

Uh oh!

There was an error while loading. Please reload this page.

@ambv
ambv merged commit ea23e78 into python:main Sep 2, 2021
@bedevere-bot bedevere-bot removed the awaiting review label Sep 2, 2021
This was referenced Oct 18, 2021
bpo-45507: Throw EOF error when gzip headers or trailers are truncated #29023
Closed
bpo-45507: EOFErrors should be thrown for truncated gzip members #29029
Merged
@rhpvorderman
rhpvorderman deleted the bpo-43613 branch November 19, 2021 09:30
@rhpvorderman rhpvorderman mentioned this pull request Oct 17, 2022
Give python-isal a mention in the zlib/gzip documentation #98347
Closed
@gpshead gpshead mentioned this pull request Oct 22, 2026
One-shot decompress() in gzip, bz2, lzma and compression.zstd is quadratic in the number of members #160310
Closed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

@ambv ambv ambv left review comments

Uh oh!

There was an error while loading. Please reload this page.

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

Uh oh!

There was an error while loading. Please reload this page.

4 participants

@rhpvorderman @ambv @the-knights-who-say-ni @bedevere-bot
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.
You are visitor 01402503 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.