Skip to content

Compressed input (gzip, zstd), single pass over zstd frames, fetch and load_url - #26

Merged
lemire merged 9 commits into
mainfrom
compressed-input
Dec 6, 2026
Merged

lemire merged 9 commits into
mainfrom
compressed-input

Conversation

@lemire

@lemire lemire commented Dec 6, 2026

Copy link
Copy Markdown
Member

What it adds

  • Compressed input. Every function that reads JSON from bytes, a buffer or a file also reads gzip and zstd. They are recognized by their first bytes, which no JSON text starts with. gzip goes through libdeflate 1.27, and zstd through the 1.5.7 single-file decoder, both vendored with their symbols hidden.

  • decompress(data, *, max_size=None, threads=None). It returns the bytes, and max_size guards against decompression bombs. zstd made of several frames is decompressed on several threads.

  • Single pass over zstd frames. These functions read zstd frames without building the decompressed input in full:

    • loads_arrow, load_arrow_file and json_to_msgpack(ndjson=True), through the block API of simdjson_arrow 0.1.7 and simdjson_msgpack 0.1.4;
    • on free-threaded Python, loads_many, parse_many and map_many with threads=.

    Each thread decompresses a block of frames into its own buffer and parses it right away. At the first failure, the input is decompressed in full and the calling thread continues from that block. The results and errors are therefore those of the uncompressed input, and fn is not called twice on a document.

  • fetch(url, ...) and load_url(url, ...). They download with urllib.request and send Accept-Encoding: zstd, gzip. fetch returns the body as the server sent it, and any function above reads it.

  • map_many with threads=. On free-threaded Python 3.14+, it now gives fn deferred reference counting. A lambda made inside a function used to run at the speed of one thread (1 GB/s on 32 threads), because every thread changed its reference count on each call; it now reaches 24.9 GB/s on 64 threads.

  • Stubs and README. The stubs cover the new functions, and the README has the usage and a benchmark section.

Benchmarks (big4, 812 MB NDJSON)

Speeds are in GB/s of JSON, on 64 threads (the 812 MB file is 13 times larger than its zstd form):

input map_many query loads_many loads_arrow
NDJSON 30.2 6.36 20.9
zstd, 256 KiB frames 23.4 6.93 17.4
gzip 3.06 2.35 2.49
  • Decompression alone: 29.1 GB/s on 32 threads, against 0.03 for compression.zstd.
  • Gzipped files: loads reads them 3.6 times as fast as json.loads(gzip.decompress(...)).

Tests

  • macOS: 1134 passed on 3.14 and 1053 on 3.14t.
  • big4 (Linux, GCC 16): 978 passed on 3.14 and 1048 on 3.14t. The new single-pass tests run there.
  • AddressSanitizer (free-threaded build): the compressed and stream tests pass.
  • Wheel workflow: started on this branch, to cover Windows and Pyodide.

libdeflate (MIT) decompresses gzip; zstddeclib.c is zstd's single-file
decoder (BSD). lib/lib_common.h hides libdeflate's symbols on Linux, so
that another libdeflate loaded in the process cannot take their place.
Both read NDJSON in blocks given by a callback (ndjson_blocks).
Every function that reads JSON from bytes, a buffer or a file also reads
gzip and zstd, recognized by their first bytes (no JSON text starts with
them). decompress(data, *, max_size=None, threads=None) returns the bytes;
max_size guards against decompression bombs. zstd made of several frames
is decompressed on several threads.

zstd frames are read in a single pass by loads_arrow, load_arrow_file and
json_to_msgpack(ndjson=True) (through the block API of simdjson_arrow and
simdjson_msgpack), and, on free-threaded Python, by loads_many, parse_many
and map_many with threads=: each thread decompresses a block of frames
into a buffer of its own and parses it at once, so the decompressed input
never exists in full. At the first failure, the input is decompressed in
full and read as before: the results and errors are those of the
uncompressed input.

fetch(url, *, headers=None, timeout=None, max_size=None) downloads with
urllib.request, asking for zstd or gzip, and returns the body as sent;
load_url(url, ...) parses it. urllib is imported at the first call.
libdeflate uses C11 atomics, which MSVC has with /std:c11 and
/experimental:c11atomics. Pyodide has no sockets for the local HTTP
server of test_url.py.
Each call of a Python function takes a reference to it. On free-threaded
Python, a function made by the calling thread (a lambda or a function
defined in another function, a bound method) has a reference count that
the other threads change atomically: map_many with such a function ran at
1 GB/s on 32 threads (big4, 812 MB of NDJSON), at the speed of one
thread. Functions defined at the top of a module already use deferred
reference counting; map_many now asks for it for fn too
(PyUnstable_Object_EnableDeferredRefcount, Python 3.14): 24.9 GB/s on 64
threads. fn is then freed by the garbage collector.
Sections on gzip and zstd input (and the single pass over zstd frames),
on fetch and load_url, and a benchmark section from big4: decompression
against the standard library, a map_many query, loads_many, Arrow and
MessagePack on the 812 MB NDJSON file in five forms, and the 22 files
gzipped. bench_compressed.py makes them.
@lemire
lemire merged commit 9ff01ee into main Dec 6, 2026
43 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant