packmem
A list of records that takes a fraction of the memory and can still be filtered, aggregated, appended to and updated, without being unpacked.
= # before: 712 MB
= # after: 20 MB
Charts and the project story: https://hamedmp.github.io/packmem/
packmem stores each field of a record collection as a column, picks a lightweight encoding for it from a sample of the data, and runs filters and aggregates on the encoded form. It is a Rust library with Python and Node packages on top. Status: alpha (0.1.2).
Results
336,776 real flight records (nycflights13, 104 MB as JSON lines) on an Apple M4 Pro, median of five fresh processes at load average 4 to 5. Lower is better everywhere. pandas and polars are run with default settings (inferred types); tuning their column types for memory would narrow the gap.
| Holder | Resident MB | Load s | Count by carrier ms | Group by dest ms | Loop over rows ms |
|---|---|---|---|---|---|
| List of dicts | 712 | 0.61 | 30 | 59 | 31 |
| packmem | 20 | 0.25 | 0.57 | 0.14 | 153 (24 with lazy rows) |
| pandas, Arrow-backed | 215 | 0.09 | 1.4 | 2.1 | 2,600 |
| polars | 170 | 0.04 | 2.4 | 3.3 | 282 |
On Linux (Ryzen 9 7950X3D, Ubuntu 24.04, load average about 1) the same data gives 710 MB for the list and 12 MB for packmem: glibc returns the memory used while loading, which macOS keeps. There, lazy rows match the list's loop speed (31 ms each) instead of beating it, plain iteration is 5.6 times slower and random row access 9 times slower.
Against polars tuned for memory
The table above uses polars with inferred types. With every string column cast to an enum, the
smallest integer types, a rechunk, and its allocator told to return freed memory
(_RJEM_MALLOC_CONF=dirty_decay_ms:0,muzzy_decay_ms:0), polars gets much closer:
| Dataset | List of dicts | polars, default | polars, tuned | packmem |
|---|---|---|---|---|
| Flights, 337 thousand rows | 712 MB | 166 MB | 41 to 57 MB | 19 MB |
| Synthetic web log, 2 million rows | 3,053 MB | 764 MB | 256 to 271 MB | 98 MB |
| Flights repeated 10 times, 3.4 million rows | 7,101 MB | 1,203 MB | 175 to 198 MB | 113 MB |
So packmem holds 1.6 to 2.9 times less than tuned polars, and tuned polars needs the schema to be known and set by hand. On Linux the gap is wider, because packmem's own resident size is lower there: 12 MB against 47 to 71 MB on the flight data, 76 against 245 to 276 MB on the log, and 94 against 185 to 209 MB on the flights repeated 10 times (2.0 to 6.0 times; memory only, measured under CI load).
In return polars loads 4 to 5 times faster from JSON lines and far faster from parquet, and its filters are 2 to 4 times faster at millions of rows. These runs were taken on a busy machine; the memory figures are stable, the timings are not final.
One line changed in two existing tools, outputs identical:
| Tool and task | Time | Peak memory |
|---|---|---|
| sqlite-utils 4.2.1, insert a 104 MB JSON array | 4.85 s to 5.46 s | 604 MB to 67 MB |
| jello 1.6.1, query 104 MB of JSON lines | 2.54 s to 0.57 s | 1,535 MB to 381 MB |
In training-data pipelines
Three vision-language fine-tuning repositories (Qwen3-VL, MiniCPM-V, LLaVA-NeXT) keep their annotations as a list of dicts that every data-loader worker touches. Swapping it for packmem on the real 665,298-record LLaVA mix took one 8-rank node with 4 fork workers per rank from 47.7 GiB to 4.9 GiB on Linux, and a 4-worker epoch on the Mac from 16.6 GB to 2.3 GB under fork and 23.5 GB to 5.5 GB under spawn. Building the dataset is 2 to 4 times slower, because the nested conversation turns are stored as JSON text. These figures cover the annotations only (no images, model or GPU) and none of the repositories has tests covering the patched code. Patches, scripts and outputs are in benchmarks/ml_swaps; the Linux runs' raw traces were lost and only their summaries remain.
Against Hugging Face datasets
packmem replaces Python lists, not Arrow. On the same 665,298 records on Linux, compared with a
memory-mapped datasets.Dataset (packmem 0.1.1, before Table.share()):
- Where
datasetswins: workers share one memory-mapped copy and receive a 2 KB reference rather than the data (node peak 1.8 GB against 2.8 GB under forkserver, first batch in 1.1 s against 4.8 s); reloading from its cache takes 0.02 s against 8.5 s to parse the JSON again; batched access is about twice as fast; a compressed parquet file is smaller at rest. - Where packmem wins: a random row as a dict takes 6 µs against 22 µs, which is the path a
map-style data loader uses; it holds about 2.5 times less than an in-memory Arrow dataset;
and it took the file as it was, where
datasetsneeded a hand-written schema. - Against the list it was 5.6 to 5.7 times less node memory.
If you already use memory-mapped datasets with worker processes, stay there. Memory figures are
solid; the timings were taken on a shared server and are indicative. Details:
benchmarks/ml_swaps/hf-datasets.
Where it loses
- Plain iteration is slower.
for r in rowsrebuilds a dict per record and is about 5 times slower than looping over the original list.rows.lazy()closes the gap but yields mappings, not dicts. - Random row access is about 7 times slower than indexing a list (8.7 ms against 1.3 ms for 10,000 rows).
- Dataframes load faster and win some numeric filters. packmem is not a dataframe; it replaces the list of dicts.
- Group-by on a column with very many distinct values is slow. Counting 436 thousand distinct IP addresses over 2 million rows takes about 310 ms, against 11 ms in polars (and 590 ms for a dict loop over the list).
- Records dominated by a nested list of dicts load slowly. Nested values are stored as JSON
text, so such datasets build 2 to 4 times slower than
json.loadand save less. - Small collections gain little. packmem breaks even with the list at about 300 records and saves 2 to 4 times at 10,000; the large ratios start around 100,000. Memory used while loading stays with the allocator, which is most of what a small table occupies.
- Sort, delete and insert in the middle rebuild the table, so each costs time proportional to its size.
The full study, including the cases above and the method, is in docs/report.md.
Install
pip install packmem # Python 3.10 or later; wheels for Linux, macOS and Windows
npm install packmem # Node, see node/README.md
From a checkout, with Rust:
cd python
pip install maturin
maturin build --release -o dist
pip install dist/*.whl
Use
= # or load_json, read_csv, Table(iterable_of_dicts)
; ; # still a sequence of dicts
# also insert, pop, del, reverse
# runs on the packed data
# decode only the fields you read
+=
# from 0.2.0: reopen in milliseconds, read in place
=
# worker processes read one shared copy
See python/README.md for the full API and its semantics.
The Rust crate exposes the same building blocks:
use ;
let latency = encode;
latency.get;
latency.sum;
latency.count_range;
let agents = encode;
agents.count_where;
How it works
- A small set of encodings that compose: constant, frame of reference, sorted dictionary, run-length, block frame of reference, string dictionary and FSST. Every one keeps O(1) or O(log n) access to a single value.
- A sampling selector estimates the size of each encoding from 64 runs of 256 values and
picks one, weighted by a hint (
memory,balancedorspeed). - Predicates are pushed into the encoding. A string filter over a dictionary column tests each distinct value once and then scans small integer codes.
- Writes go to an uncompressed tail that is packed every 16,384 rows; updates to packed rows go to a small patch map.
The encodings are established work from analytical databases (BtrBlocks, Vortex, FSST). What packmem adds is putting them behind an ordinary container API for program memory.
Reproduce the numbers
python -m venv .venv && .venv/bin/pip install maturin pandas polars pyarrow nycflights13
(cd python && ../.venv/bin/maturin build --release -o dist && ../.venv/bin/pip install dist/*.whl)
mkdir data && (cd data && ../.venv/bin/python ../benchmarks/fetch_data.py)
.venv/bin/python benchmarks/run_all.py data results.jsonl 5
Recorded results, with machine details, are in benchmarks/results. The synthetic-log study behind the report is in experiments.
Repository layout
| Path | Contents |
|---|---|
src/ |
Rust core: integer and string encodings, the selector, the mutable table |
python/ |
Python package (PyO3, built with maturin) and its tests |
benchmarks/ |
Real-data benchmarks, tool-swap runs and recorded results |
experiments/ |
Synthetic-log study: generator, cross-runtime workloads, recorded results |
docs/ |
The technical report |
Contributing
See CONTRIBUTING.md. Security reports: SECURITY.md.
License
Licensed under the Apache License, Version 2.0. Copyright 2026 Finna Labs Inc.
Unless you explicitly state otherwise, any contribution intentionally submitted for inclusion in this project by you, as defined in the Apache-2.0 license, shall be licensed as above, without any additional terms or conditions.