Repository navigation
Compressed input (gzip, zstd), single pass over zstd frames, fetch and load_url - #26
Merged
Merged
Conversation
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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
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.
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, andmax_sizeguards 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_fileandjson_to_msgpack(ndjson=True), through the block API of simdjson_arrow 0.1.7 and simdjson_msgpack 0.1.4;loads_many,parse_manyandmap_manywiththreads=.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
fnis not called twice on a document.fetch(url, ...)andload_url(url, ...). They download withurllib.requestand sendAccept-Encoding: zstd, gzip.fetchreturns the body as the server sent it, and any function above reads it.map_manywiththreads=. On free-threaded Python 3.14+, it now givesfndeferred 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):
map_manyqueryloads_manyloads_arrowcompression.zstd.loadsreads them 3.6 times as fast asjson.loads(gzip.decompress(...)).Tests