From 007ca8b6ef1ba3932817c5d94e70d6db34a0edd4 Mon Sep 17 00:00:00 2001 From: ptweezy Date: Thu, 15 Oct 2026 15:04:01 -0400 Subject: [PATCH] Import fewer modules in igzip and igzip_threaded argparse and shutil are only needed by the command line interface, and multiprocessing was only used for cpu_count, which os.cpu_count replaces. Importing igzip_threaded takes 4.8 ms instead of 11.1 ms on Python 3.14. --- CHANGELOG.rst | 3 +++ src/isal/igzip.py | 4 ++-- src/isal/igzip_threaded.py | 6 +----- tests/test_igzip.py | 13 +++++++++++++ 4 files changed, 19 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.rst b/CHANGELOG.rst index 4b200d3c..eda5929b 100644 --- a/CHANGELOG.rst +++ b/CHANGELOG.rst @@ -10,6 +10,9 @@ Changelog version 1.8.1-dev ----------------- ++ Speed up ``import isal.igzip`` and ``import isal.igzip_threaded`` by + importing ``argparse`` and ``shutil`` only when the command line interface + runs, and by dropping the ``multiprocessing`` import. + Updated ISA-L to 2.32.1. + Python 3.10 and PyPy 3.10 are no longer supported. Added support for PyPy 3.12 and Python 3.15. diff --git a/src/isal/igzip.py b/src/isal/igzip.py index af1b4712..d1b0e1a9 100644 --- a/src/isal/igzip.py +++ b/src/isal/igzip.py @@ -24,12 +24,10 @@ """Similar to the stdlib gzip module. But using the Intel Storage Accelaration Library to speed up its methods.""" -import argparse import builtins import gzip import io import os -import shutil import struct import sys import time @@ -255,6 +253,7 @@ def decompress(data): def _argument_parser(): + import argparse # Only the command line interface needs it parser = argparse.ArgumentParser() parser.description = ( "A simple command line interface for the igzip module. " @@ -302,6 +301,7 @@ def _argument_parser(): def main(): + import shutil # Only the command line interface needs it args = _argument_parser().parse_args() compresslevel = args.compresslevel or _COMPRESS_LEVEL_TRADEOFF diff --git a/src/isal/igzip_threaded.py b/src/isal/igzip_threaded.py index 355b7f72..1e1912cb 100644 --- a/src/isal/igzip_threaded.py +++ b/src/isal/igzip_threaded.py @@ -7,7 +7,6 @@ import builtins import io -import multiprocessing import os import queue import struct @@ -52,10 +51,7 @@ def open(filename, mode="rb", compresslevel=igzip._COMPRESS_LEVEL_TRADEOFF, try: threads = len(os.sched_getaffinity(0)) except: # noqa: E722 - try: - threads = multiprocessing.cpu_count() - except: # noqa: E722 - threads = 1 + threads = os.cpu_count() or 1 if "r" in mode: gzip_file = io.BufferedReader( _ThreadedGzipReader(filename, block_size=block_size)) diff --git a/tests/test_igzip.py b/tests/test_igzip.py index c0661197..2774c1c7 100644 --- a/tests/test_igzip.py +++ b/tests/test_igzip.py @@ -15,6 +15,7 @@ import re import shutil import struct +import subprocess import sys import tempfile import zlib @@ -261,6 +262,18 @@ def test_compress_infile_out_file_no_name(tmp_path, capsysbinary): assert output[4:8] == b"\x00\x00\x00\x00" # No timestamp set. +def test_import_does_not_load_cli_modules(): + # Each of these modules takes milliseconds to import + code = ("import sys; before = set(sys.modules); " + "import isal.igzip, isal.igzip_threaded; " + "print(*(set(sys.modules) - before))") + result = subprocess.run([sys.executable, "-c", code], check=True, + capture_output=True, text=True) + new_modules = set(result.stdout.split()) + assert "isal.igzip_threaded" in new_modules + assert not new_modules & {"argparse", "shutil", "multiprocessing"} + + def test_decompress(): assert igzip.decompress(COMPRESSED_DATA) == DATA