Repository navigation
Read gzip files with python-isal on x86-64 only, and use it for writing on macOS arm64 #188
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -196,6 +196,15 @@ development version | |||||
| on Windows before (and only sometimes). | ||||||
| * Dropped support for Python 3.10 | ||||||
| * Started supporting Python 3.15 | ||||||
| * python-isal is now used for reading gzip files on x86-64 only. On other | ||||||
| architectures, such as ``aarch64``, python-zlib-ng is preferred because it | ||||||
| decompresses faster there. python-isal is still used on all architectures | ||||||
| for writing at compression levels 1 and 2. | ||||||
| * python-isal is now also installed on macOS with Apple Silicon (``arm64``), | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Suggested change
|
||||||
| where it is used for writing gzip files at compression levels 1 and 2. | ||||||
| python-isal 1.9.0 or later is required there because earlier versions used | ||||||
| the portable C code of ISA-L instead of its aarch64 assembly on that | ||||||
| platform. | ||||||
|
|
||||||
| v2.1.0 (2026-06-03) | ||||||
| ~~~~~~~~~~~~~~~~~~~ | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -19,6 +19,8 @@ requires-python = ">=3.11" | |
| dynamic = ["version"] | ||
| dependencies = [ | ||
| 'isal>=1.6.1; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"', | ||
| # macOS on Apple Silicon. python-isal uses ISA-L's aarch64 assembly there since 1.9.0. | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I do not understand this comment and had to read the python-isal changelog. I guess the point is that python-isal on aarch64 was too slow before 1.9.0 to be worth using. I suggest removing this comment entirely and also to just bump the requirement for isal to |
||
| 'isal>=1.9.0; platform.machine == "arm64"', | ||
| 'zlib-ng>=0.4.1; platform.machine == "x86_64" or platform.machine == "AMD64" or platform.machine == "aarch64"', | ||
| 'backports.zstd; python_version < "3.14"', | ||
| ] | ||
|
|
||
| Original file line number | Diff line number | Diff line change | ||||
|---|---|---|---|---|---|---|
|
|
@@ -18,6 +18,7 @@ | |||||
| import stat | ||||||
| import subprocess | ||||||
| import sys | ||||||
| import sysconfig | ||||||
| import tempfile | ||||||
| import threading | ||||||
| import time | ||||||
|
|
@@ -567,6 +568,23 @@ def _open_lz4( | |||||
| ) | ||||||
|
|
||||||
|
|
||||||
| def _use_isal(mode: str, compresslevel: int) -> bool: | ||||||
| """Return whether ISA-L is the preferred gzip backend.""" | ||||||
| if "r" in mode: | ||||||
| # ISA-L decompresses faster than zlib-ng and zlib on x86-64. On ARM | ||||||
| # (aarch64 on Linux, arm64 on macOS), zlib-ng decompresses faster than | ||||||
| # ISA-L, and so does the zlib that Apple ships with macOS. | ||||||
| if sys.platform == "win32": | ||||||
| # The architecture that Python, and so python-isal, was compiled | ||||||
| # for. platform.machine() takes tens of milliseconds on Windows | ||||||
| # and returns the architecture of the CPU instead. | ||||||
|
Comment on lines
+578
to
+580
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I don’t quite understand why |
||||||
| return sysconfig.get_platform() == "win-amd64" | ||||||
| return os.uname().machine in ("x86_64", "amd64") | ||||||
|
ptweezy marked this conversation as resolved.
Resolved
|
||||||
| # Igzip level 0 does not output uncompressed deflate blocks as zlib does | ||||||
| # and level 3 is slower but does not compress better than level 1 and 2. | ||||||
| return compresslevel in (1, 2) | ||||||
|
|
||||||
|
|
||||||
| def _open_gz( | ||||||
| filename: FileOrPath, | ||||||
| mode: str, | ||||||
|
|
@@ -575,7 +593,8 @@ def _open_gz( | |||||
| ): | ||||||
| """ | ||||||
| Open a gzip file. The ISA-L library is preferred when applicable because | ||||||
| it is the fastest. Then zlib-ng which is not as fast, but supports all | ||||||
| it is the fastest: for compression at levels 1 and 2, and for decompression | ||||||
| on x86-64. Then zlib-ng which is not as fast, but supports all | ||||||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Would be nice to add the missing comma while the text is being changed anyway:
Suggested change
|
||||||
| compression levels. After that comes pigz, which can utilize multiple | ||||||
| threads and is more efficient than gzip, even on one core. gzip is chosen | ||||||
| when none of the alternatives are available. Despite it being able to use | ||||||
|
|
@@ -595,9 +614,7 @@ def _open_gz( | |||||
| ) | ||||||
|
|
||||||
| if threads != 0: | ||||||
| # Igzip level 0 does not output uncompressed deflate blocks as zlib does | ||||||
| # and level 3 is slower but does not compress better than level 1 and 2. | ||||||
| if igzip_threaded and (compresslevel in (1, 2) or "r" in mode): | ||||||
| if igzip_threaded and _use_isal(mode, compresslevel): | ||||||
| return igzip_threaded.open( # type: ignore | ||||||
| filename, | ||||||
| mode, | ||||||
|
|
@@ -647,9 +664,7 @@ def _open_reproducible_gzip(filename, mode: str, compresslevel: int): | |||||
| mode=mode, | ||||||
| mtime=0, | ||||||
| ) | ||||||
| # Igzip level 0 does not output uncompressed deflate blocks as zlib does | ||||||
| # and level 3 is slower but does not compress better than level 1 and 2. | ||||||
| if igzip is not None and (compresslevel in (1, 2) or "r" in mode): | ||||||
| if igzip is not None and _use_isal(mode, compresslevel): | ||||||
| gzip_file = igzip.IGzipFile(**kwargs, compresslevel=compresslevel) | ||||||
| elif gzip_ng is not None: | ||||||
| # Zlib-ng level 1 creates much bigger files than zlib level 1. | ||||||
|
|
||||||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -11,13 +11,15 @@ | |
| import shutil | ||
| import subprocess | ||
| import sys | ||
| import sysconfig | ||
| import tempfile | ||
| from contextlib import contextmanager | ||
| from pathlib import Path | ||
| from types import SimpleNamespace | ||
|
|
||
| import pytest | ||
|
|
||
| from xopen import _detect_format_from_content, xopen | ||
| from xopen import _detect_format_from_content, _use_isal, xopen | ||
|
|
||
| if sys.version_info >= (3, 14): | ||
| from compression import zstd | ||
|
|
@@ -432,6 +434,89 @@ def test_write_gzip_no_threads_no_isal(tmp_path, xopen_without_igzip): | |
| assert isinstance(f.raw, gzip.GzipFile) | ||
|
|
||
|
|
||
| def _gzip_backend(f) -> str: | ||
| """Return the top-level module of the gzip file object that xopen returned""" | ||
| if isinstance(f, (io.BufferedReader, io.BufferedWriter)): | ||
| f = f.raw | ||
| return type(f).__module__.split(".")[0] | ||
|
|
||
|
|
||
| def _set_architecture(monkeypatch, platform: str, machine: str) -> None: | ||
| """ | ||
| Set sys.platform, and the machine that _use_isal() gets from os.uname(), | ||
| or from sysconfig.get_platform() on Windows | ||
| """ | ||
| monkeypatch.setattr(sys, "platform", platform) | ||
| if platform == "win32": | ||
| monkeypatch.setattr(sysconfig, "get_platform", lambda: machine) | ||
| else: | ||
| uname = SimpleNamespace(machine=machine) | ||
| # os.uname does not exist on Windows | ||
| monkeypatch.setattr(os, "uname", lambda: uname, raising=False) | ||
|
|
||
|
|
||
| @pytest.mark.parametrize( | ||
| ["platform", "machine", "expected"], | ||
| [ | ||
| ("linux", "x86_64", True), | ||
| ("linux", "aarch64", False), | ||
| ("darwin", "x86_64", True), | ||
| ("darwin", "arm64", False), | ||
| ("freebsd15", "amd64", True), | ||
| ("win32", "win-amd64", True), | ||
| ("win32", "win-arm64", False), | ||
| ], | ||
| ) | ||
| def test_use_isal_depends_on_architecture_for_reading( | ||
| monkeypatch, platform, machine, expected | ||
| ): | ||
| _set_architecture(monkeypatch, platform, machine) | ||
| assert _use_isal("rb", 1) is expected | ||
| assert _use_isal("wb", 1) is True | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("threads", [0, 1]) | ||
| @pytest.mark.parametrize( | ||
| ["machine", "expected"], [("x86_64", "isal"), ("aarch64", "zlib_ng")] | ||
| ) | ||
| def test_gzip_read_backend_depends_on_architecture( | ||
| monkeypatch, threads, machine, expected | ||
| ): | ||
| import xopen # xopen local overrides xopen global variable | ||
|
|
||
| if xopen.igzip is None or xopen.gzip_ng is None: | ||
| pytest.skip("requires python-isal and python-zlib-ng") | ||
| _set_architecture(monkeypatch, "linux", machine) | ||
| with xopen.xopen(TEST_DIR / "file.txt.gz", "rb", threads=threads) as f: | ||
| assert _gzip_backend(f) == expected | ||
| assert f.read() == CONTENT.encode() | ||
|
|
||
|
|
||
| def test_gzip_read_without_zlib_ng_where_isal_is_slower(monkeypatch): | ||
| import xopen # xopen local overrides xopen global variable | ||
|
|
||
| _set_architecture(monkeypatch, "linux", "aarch64") | ||
| monkeypatch.setattr(xopen, "gzip_ng", None) | ||
| with xopen.xopen(TEST_DIR / "file.txt.gz", "rb", threads=0) as f: | ||
| assert type(f) is gzip.GzipFile | ||
| assert f.read() == CONTENT.encode() | ||
|
|
||
|
|
||
| @pytest.mark.parametrize("threads", [0, 1]) | ||
| @pytest.mark.parametrize("machine", ["x86_64", "aarch64"]) | ||
| def test_gzip_write_backend_at_level_1_is_isal(tmp_path, monkeypatch, threads, machine): | ||
| import xopen # xopen local overrides xopen global variable | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yes, but why? |
||
|
|
||
| if xopen.igzip is None: | ||
| pytest.skip("requires python-isal") | ||
| _set_architecture(monkeypatch, "linux", machine) | ||
| path = tmp_path / "out.gz" | ||
| with xopen.xopen(path, "wb", compresslevel=1, threads=threads) as f: | ||
| assert _gzip_backend(f) == "isal" | ||
| f.write(b"hello") | ||
| assert gzip.decompress(path.read_bytes()) == b"hello" | ||
|
|
||
|
|
||
| def test_write_stdout(): | ||
| f = xopen("-", mode="w") | ||
| print("Hello", file=f) | ||
|
|
||
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.