spillsort
Sort more items than fit in memory: sorted runs spill to disk and merge as they are read.
Install with pip or uv:
pip install spillsort
Or add the Rust crate:
cargo add spillsort
Introduction
This library sorts like sorted(items, key=...), but in a fixed amount of memory.
Each item is keyed and encoded into bytes once, as it is added.
When the memory fills, a background thread sorts it and writes it to a temporary file while you keep adding.
Sorting merges those files with what is still in memory, so items come out in key order, and items with equal keys come out in the order they were added.
The engine is Rust, and it is also a Rust crate that needs no Python.
Quick Start
Give a key function and sort items, once:
from spillsort import Sorter
reads = [("chr2", 500, "read1"), ("chr1", 900, "read2"), ("chr1", 100, "read3"), ("chr1", 900, "read4")]
with Sorter(lambda read: (read[0], read[1])) as sorter:
ordered = list(sorter.sort(reads))
assert ordered == [("chr1", 100, "read3"), ("chr1", 900, "read2"), ("chr1", 900, "read4"), ("chr2", 500, "read1")]
Add items one at a time as they arrive, bound the memory with max_bytes, sort on more threads with threads, and choose where to spill with tmp_dir:
import random
from tempfile import TemporaryDirectory
numbers = [random.random() for _ in range(100_000)]
with TemporaryDirectory() as tmp_dir, Sorter(max_bytes=2**20, threads=4, tmp_dir=tmp_dir) as sorter:
for number in numbers:
sorter.add(number)
ordered = list(sorter.sort())
assert ordered == sorted(numbers)
Items are pickled while they wait unless a codec turns them into bytes and back:
class Line:
def encode(self, item: str, /) -> bytes:
return item.encode()
def decode(self, data: bytes, /) -> str:
return data.decode()
with Sorter(lambda line: int(line.split("\t")[1]), codec=Line()) as sorter:
lines = list(sorter.sort(["chr1\t300\n", "chr1\t20\n", "chr1\t1000\n"]))
assert lines == ["chr1\t20\n", "chr1\t300\n", "chr1\t1000\n"]
If you already have the bytes, add them with their key, and no Python runs per item. Write the sorted bytes to any binary file in large chunks, merged on another thread while the file writes:
import io
with Sorter() as sorter:
sorter.add_encoded(("chr2", 5), b"chr2\t5\t9\n")
sorter.add_encoded(("chr1", 70), b"chr1\t70\t80\n")
sorter.add_encoded(("chr1", 8), b"chr1\t8\t9\n")
out = io.BytesIO()
sorter.write_to(out)
assert out.getvalue() == b"chr1\t8\t9\nchr1\t70\t80\nchr2\t5\t9\n"
From Rust, push keyed byte records and read them back merged:
use spillsort::{Config, Sorter, key};
fn main() -> Result<(), spillsort::Error> {
let mut sorter = Sorter::new(Config::default().max_bytes(64 << 20))?;
for (refname, start) in [("chr2", 5), ("chr1", 70), ("chr1", 8)] {
let mut sort_key = Vec::new();
key::push_str(&mut sort_key, refname);
key::push_int(&mut sort_key, start);
sorter.push(&sort_key, format!("{refname}\t{start}\n").as_bytes())?;
}
let mut out = Vec::new();
sorter.finish()?.write_values(&mut out)?;
assert_eq!(out, b"chr1\t8\nchr1\t70\nchr2\t5\n");
Ok(())
}
See the API documentation for the crate.
Keys
Keys compare exactly as Python compares them, even ints and floats of any size, and NaN sorts last:
with Sorter() as sorter:
ordered = list(sorter.sort([2**64, float("nan"), 1.5, True, -0.5, 3]))
assert ordered[:-1] == [-0.5, True, 1.5, 3, 2**64]
Development and Testing
See the contributing guide for more information.
Metadata
Release files for spillsort 0.1.1
For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.
Source distribution (sdist)
| File | Size | Uploaded | |
|---|---|---|---|
| spillsort-0.1.1.tar.gz | 54.5 kB | Details |
Built distributions (wheels)
Total release size: 6.0 MB
Release files / spillsort-0.1.1.tar.gz
| Download URL | spillsort-0.1.1.tar.gz |
|---|---|
| Size | 54.5 kB |
| Tags | Source |
|
SHA-256 checksum How to use checksums |
6fd8a3cd8093118dda97e9c38f8f581e479dd8e15648acc5939f76070dee8968
|
|
BLAKE2b-256 checksum How to use checksums |
f7fe61e469f8d74aa89e75d368249bd63d3a40e3e0caf31348be5d39302467b4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-win_amd64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-win_amd64.whl |
|---|---|
| Size | 271.4 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Windows x86-64 |
|
SHA-256 checksum How to use checksums |
7d5c85dbaee7045718a18b5cd0014c52e6b40683a7e783f742d5f466361e76c4
|
|
BLAKE2b-256 checksum How to use checksums |
442a5b54cb107174ec98c84ae01184673ffeb57c8905d929f14d582b52d6c9f2
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 612.0 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux musl 1.2+ x86-64 |
|
SHA-256 checksum How to use checksums |
62c2ce1f5b4435633dc97bd4571a43df1409a5f75af40efa19b870843ba8b83b
|
|
BLAKE2b-256 checksum How to use checksums |
9f19eb6405614ddaac3d3a93e20d9f53e41072fa76f10c9efb113adf074ed81c
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 576.3 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux musl 1.2+ ARM64 |
|
SHA-256 checksum How to use checksums |
613090cdf4012efff9c848cee82b7fcf6891a79222b02a9b49490c05dad5d9d3
|
|
BLAKE2b-256 checksum How to use checksums |
d281ae5e893158453bac33b47291144f5796ec11ad52c2a4b4f6e2914041626d
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-manylinux_2_28_aarch64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 399.1 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux glibc 2.28+ ARM64 |
|
SHA-256 checksum How to use checksums |
7201554f859bbb2c8675030720bcfbe53c5a4568be1e7562d1bfe1627d5a3c9c
|
|
BLAKE2b-256 checksum How to use checksums |
f886a353d5849b5c814231fc86935a2080589f0b20c282b6f8c6264f888cfeb6
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 399.2 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading Linux glibc 2.17+ x86-64 |
|
SHA-256 checksum How to use checksums |
dd386ed0caead30137011ae9c8bbe6327e9614d659a2d8780cf0dbb6df2e6817
|
|
BLAKE2b-256 checksum How to use checksums |
258202f33fca26aa951d032be0adc42885bcd91e9899a5cc5240e1acdf83e3c4
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-macosx_11_0_arm64.whl |
|---|---|
| Size | 355.9 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
af143e927c12dfc632c54c6b13b8f30f767f73643030869f023313ef7f0092a3
|
|
BLAKE2b-256 checksum How to use checksums |
b3221c8a079321098928d7b7272372d91a1dc232c372f4d55f781c6518b8e6e1
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl
| Download URL | spillsort-0.1.1-cp314-cp314t-macosx_10_12_x86_64.whl |
|---|---|
| Size | 366.7 kB |
| Tags | CPython 3.14 CPython 3.14 free-threading macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
fff422027a632a7d68e1c288e9b881880486b874d8da4b57d7c149f0593317f3
|
|
BLAKE2b-256 checksum How to use checksums |
390fe35dfe682718d2edddb3ac4b205a112d70edd2f4540c7052691799d043a7
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-win_amd64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-win_amd64.whl |
|---|---|
| Size | 274.8 kB |
| Tags | CPython 3.11 Windows x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
76f7452b18c7d75e4f8ac47182ce831126816f94e78029f2639ff575b45983ba
|
|
BLAKE2b-256 checksum How to use checksums |
34b5ee7fff9a9f4ad24b0a4337a4e551aa200cab02b4b93bf764fef773a49817
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-musllinux_1_2_x86_64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-musllinux_1_2_x86_64.whl |
|---|---|
| Size | 613.9 kB |
| Tags | CPython 3.11 Linux musl 1.2+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
5c80c897d847a17ee39a2ae4670ff9169c0364de01db14e14c2294e132d9dc63
|
|
BLAKE2b-256 checksum How to use checksums |
8b0927dbd7bda08ca84287bf589e0688e330c0cd6d1016c5e814e3477dd88254
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-musllinux_1_2_aarch64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-musllinux_1_2_aarch64.whl |
|---|---|
| Size | 578.3 kB |
| Tags | CPython 3.11 Linux musl 1.2+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
45758e1e389fa0de61967a8e29adccf92856987a8649e65b19ee00aef8cab268
|
|
BLAKE2b-256 checksum How to use checksums |
0506a0e7e47e6c4b2130011835927e06ed5160320f45590e3e4721e73c3d42fe
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-manylinux_2_28_aarch64.whl |
|---|---|
| Size | 400.8 kB |
| Tags | CPython 3.11 Linux glibc 2.28+ ARM64 abi3 |
|
SHA-256 checksum How to use checksums |
a239d59340996ea78dbe46d894b9eb5891e9455ec4ed7a666a1b540f8da2dcd5
|
|
BLAKE2b-256 checksum How to use checksums |
434f75dbb1544bde3a54e3a07814c481c76f0c44cbbcb9273f6822f126e78fd5
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-manylinux_2_17_x86_64.manylinux2014_x86_64.whl |
|---|---|
| Size | 401.3 kB |
| Tags | CPython 3.11 Linux glibc 2.17+ x86-64 abi3 |
|
SHA-256 checksum How to use checksums |
ac25e8dcfd10ccc85aa5e9c1a9df3af8b93368e37093fd5526f770e917b0467f
|
|
BLAKE2b-256 checksum How to use checksums |
4a36693fd10c86a640e7000fab2da04654fb341b03f142dfecec1c57d201a10b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-macosx_11_0_arm64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-macosx_11_0_arm64.whl |
|---|---|
| Size | 359.4 kB |
| Tags | CPython 3.11 abi3 macOS 11.0+ ARM64 |
|
SHA-256 checksum How to use checksums |
979f5d8df5abfc8cc4dbbe8244836c996a4926d05596d831202a364d510b92a8
|
|
BLAKE2b-256 checksum How to use checksums |
e3a5b57d9506b5d5d5ef7a2b3530ee7cbb7d1996a66d28187f2d90a014449325
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency logRelease files / spillsort-0.1.1-cp311-abi3-macosx_10_12_x86_64.whl
| Download URL | spillsort-0.1.1-cp311-abi3-macosx_10_12_x86_64.whl |
|---|---|
| Size | 366.0 kB |
| Tags | CPython 3.11 abi3 macOS 10.12+ x86-64 |
|
SHA-256 checksum How to use checksums |
f362f082889b6645844d452a8af33646f42b1ccf1ca15718a5a1205667c7710d
|
|
BLAKE2b-256 checksum How to use checksums |
280397358071e4b008a7f8db1df1dfe451d9ae7ebe672d6734915cc205d8277b
|
| Upload date | |
|
Uploaded using Trusted Publishing? What is trusted publishing? |
Yes |
| Uploaded via |
twine/7.0.0 CPython/3.13.14
|
Provenance
Provenance describes where a file came from. On PyPI, provenance is shared via attestations, which provide a verifiable record of the build or publishing details. View details, limitations and caveats.
PyPI Publish Attestation
PyPI verified that this artifact, at this checksum, originated from the publisher listed below.
Signed by GitHub Actions, verified by PyPI on Oct 16, 2026.
Transparency log