Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions README.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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
Comment on lines +199 to +201

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* 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
* python-isal is now used for reading gzip files on x86-64 only. On other
architectures, such as ``aarch64``, python-zlib-ng decompresses faster.
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``),

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
* python-isal is now also installed on macOS with Apple Silicon (``arm64``),
* python-isal is now a dependency even on macOS with Apple Silicon (``arm64``),

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)
~~~~~~~~~~~~~~~~~~~
Expand Down
2 changes: 2 additions & 0 deletions pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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 >=1.9.0 for all other platforms as well.

'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"',
]
Expand Down
29 changes: 22 additions & 7 deletions src/xopen/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import stat
import subprocess
import sys
import sysconfig
import tempfile
import threading
import time
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don’t quite understand why platform.machine() is mentioned here. Why even say how long it takes if it’s the wrong call to use anyway?

return sysconfig.get_platform() == "win-amd64"
return os.uname().machine in ("x86_64", "amd64")
Comment thread
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,
Expand All @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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
on x86-64. Then zlib-ng which is not as fast, but supports all
on x86-64. Then zlib-ng, which is not as fast, but supports all

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
Expand All @@ -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,
Expand Down Expand Up @@ -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.
Expand Down
87 changes: 86 additions & 1 deletion tests/test_xopen.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The 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)
Expand Down
Loading