Most of a program's memory is not its data.
packmem keeps your records as packed columns that your code can still index, loop over, update and query, without unpacking anything.
336,776 real flights take 712 MB as a Python list of dicts. In packmem they take 20 MB, from changing one line.
NewIn 0.3
Changelog- Browsers get packed bytes
toBytes and fromBytes in Node and WebAssembly; packmem/http sends bytes to clients that ask and JSON to the rest.
- React hooks
useTable fetches a table into the browser and useRows builds objects only for the rows on screen (useTable from 0.3.2). Both work inside Next.js.
- Lists of records as columns
Conversation turns and other nested records are stored as sub-columns, with pandas-style list and str operations in Python.
- Several threads
Filters, group counts and large file loads use several threads; PACKMEM_THREADS=1 turns it off.
- Arrow, Parquet and compact text
to_arrow, from_arrow, read_parquet and to_parquet in Python; hint="compact" compresses long text with Zstandard.
- Smaller byte headers
Format 4 writes header integers as varints, so small tables take fewer bytes.
- 0.3.1: leaner record columns
Columns of records that are never absent drop their per-row offsets (byte format 5), and fetchTable falls back to JSON when a server's bytes are newer than the client.
- 0.3.2: gzip and a log viewer
packmem/http gzips bytes inside the payload for clients that ask, since hosts like Next.js only compress JSON. demos/log-viewer sends a million log lines to the browser.
Where the memory goes
Parse a file into objects and every value gets wrapped: an object header, a pointer to it, a hash slot for its key, padding to the next boundary. The same 75.5 MB log grew 2.7 to 9 times in five runtimes, and most of the words were zeros, pointers and small counters.
2-core Linux sandbox, synthetic web log, an earlier build than the rest of this page.
- Bookkeeping: zeros, pointers, small integers
- Text
- Other
Java stores pointers as 32-bit values, so its pointer share is understated; Node's text includes strings awaiting garbage collection.
Operating systems already compress memory pages, but a compressed page has to be decompressed before any byte in it is read. The larger saving is not to create the overhead at all.
Pack the columns, query them packed
Each field becomes one column, and every 16,384-row chunk of it gets an encoding picked from a sample of its values, so a column whose values drift changes encoding as it goes. Every encoding keeps fast access to a single value, so nothing is unpacked in bulk, and filters run on the encoded form.
- A list of dicts: Every record is an object, and every value in it is wrapped again: an object header, a pointer, a hash slot.
- One column per field: packmem takes the values out of the records and keeps each field as one column. The wrappers are never created.
- An encoding per column: A sample of each 16,384-row chunk picks the encoding: one constant, a dictionary and small codes, offsets from the minimum.
- Queries read the codes: count(carrier="UA") looks up UA's code once, then compares 4-bit codes. No record is rebuilt to answer it.
- JSON straight from the columns: Matching rows are written as JSON from the columns into the response, without an object per row in between.
Dictionary
carrier has 16 distinct values in 336,776 rows: store them once, and a 4-bit code per row. Counting UA compares codes. A dictionary is kept only where it is smaller than offsets.
Frame of reference
arr_delay is stored as the offset from its minimum in the narrowest width that fits its chunk. Sums and range filters run on the narrow codes.
Constants, runs and blocks
A column with one value (year) is stored once. Long runs become one value and an end. Sorted values get a minimum per block, so a range count is two binary searches.
FSST strings
Frequent substrings become one-byte codes, and each string still decodes on its own.
None of these encodings is new. Analytical databases use them, notably BtrBlocks and Vortex, and the string scheme is FSST. What packmem adds is putting them behind the containers ordinary programs already use: a list of dicts, an array of objects.
On real data, in Python
The flight data is the public nycflights13 set: 19 columns of numbers, short strings and timestamps, with missing values. packmem used less memory than pandas and polars with inferred types, and 1.6 to 2.9 times less than polars tuned for memory, while still behaving like a list.
Tuned polars needs the schema known and set by hand.
- List of dicts
- packmem
The slower rows are the honest cost: plain loops rebuild a dict per record. Lazy rows decode only the fields a loop reads.
- List of dicts
- packmem
At 100 rows packmem used more memory than the list (0.33 against 0.23 MB); from 300 rows on it used less. Below about 10,000 rows most of what packmem holds is load buffers, not data.
- Original
- One line changed
Inside a web server
Load a dataset at start-up, then serve filtered pages, aggregates and exports from memory. The same five routes in Express, Next.js and FastAPI, switched between plain objects and packmem with one environment variable. Every response was identical between the two.
- Plain objects
- packmem
- Plain objects
- packmem
In Node, loading through packmem took 2.1 to 2.2 times as long as the driver's own rows: rows cross into the native code as text.
| Endpoint | Express | Next.js | FastAPI |
|---|---|---|---|
| page of 50 rows | 3.4 ms → 1.6 ms 2.2× faster | 4.8 ms → 2.6 ms 1.8× faster | 104 ms → 1.9 ms 55× faster |
| group by dest | 2.6 ms → 0.87 ms 3.0× faster | 3.1 ms → 1.6 ms 2.0× faster | 60 ms → 1.2 ms 49× faster |
| summary | 2.5 ms → 2.3 ms about the same | 3.1 ms → 3.0 ms about the same | 60 ms → 2.4 ms 25× faster |
| export 58,665 rows | 39 ms → 37 ms about the same | 161 ms → 159 ms about the same | 202 ms → 51 ms 4.0× faster |
| export all 336,776 rows | 167 ms → 174 ms about the same | 834 ms → 837 ms about the same | 617 ms → 278 ms 2.2× faster |
| 8 concurrent exports of 58,665 rows | 271 ms → 197 ms 1.4× faster | 488 ms → 401 ms 1.2× faster | 1,621 ms → 397 ms 4.1× faster |
Python gets both: a tenth of the memory and queries tens of times faster than loops over dicts. In Node the gain is mainly memory and is largest at peak; at rest it is 1.8 to 4.1 times, because V8 already stores objects of one shape compactly.
The same benchmarks on Linux
Everything above ran on an Apple M4 Pro. The same code, data and scripts also ran on a Linux server: AMD Ryzen 9 7950X3D 16-Core Processor, Ubuntu 24.04, load average 0.6 to 1.3. On Linux memory is the resident set size; on macOS the Python figures are the physical footprint and the server figures the resident size of the process tree, so compare ratios rather than megabytes. Every query answer matched the Mac's.
In Python the gap is wider on Linux: the flights take 12 MB against 20 MB on the Mac. About 8 MB of what packmem holds on macOS is load buffers the macOS allocator keeps after loading; glibc returns them. The costs are wider too: a plain loop over every row is 5.6× slower than the list, random rows 9.0× slower, and lazy rows only match the list (31 ms against 31 ms).
Node saves less on Linux than on the Mac. The Postgres rows on Linux need a caveat: loading from Postgres leaves freed V8 heap pages that V8 keeps for a while before returning them. A table loaded from Postgres on Linux held 210 MB until 60 s after loading and 93 MB by 120 s (on the Mac, 170 MB falling to 83 MB within 20 s). The servers were measured at 20 seconds, before that point on Linux, so their Postgres figures above are an upper bound.
- Plain objects
- packmem
| Baseline → packmem | macOS, M4 Pro | Linux, Ryzen 9 |
|---|---|---|
| Python, flights in a list of dicts | 712 MB → 20 MB (36×) | 710 MB → 12 MB (59×) |
| Express server at rest, from the file | 390 → 94 MB (4.1×) | 205 → 100 MB (2.0×) |
| Next.js server at rest, from the file | 411 → 118 MB (3.5×) | 199 → 131 MB (1.5×) |
| FastAPI server at rest, from the file | 731 → 72 MB (10×) | 723 → 58 MB (12×) |
| Express server at rest, from Postgres | 200 → 100 MB (2.0×) | 303 → 228 MB (1.3×) |
| Next.js server at rest, from Postgres | 222 → 125 MB (1.8×) | 240 → 145 MB (1.7×) |
| FastAPI server at rest, from Postgres | 439 → 118 MB (3.7×) | 433 → 111 MB (3.9×) |
What it is for
packmem is for programs that hold many records in memory and keep using them as records: looking them up, filtering, counting, sending them back out as JSON. That shape is everywhere, and in most of those places memory, not processor time, decides the size of the machine, how many workers fit on it, and whether the data can stay in the process at all.
Servers that keep a dataset in memory
Load at start-up, then serve filtered pages, counts and exports from memory. Express settled at 94 MB instead of 390 MB, and every response was identical to the plain version. A smaller footprint is a smaller instance, or more replicas on the same one.
Scripts and command-line tools
Tools that parse a whole file into a list and then query it. jello also finished in 0.57 s instead of 2.54 s. Not every tool gets faster: sqlite-utils used less memory but more time.
Training data for LLMs and vision-language models
IndicativeA fine-tuning loader keeps the annotation list in the main process and gives every worker a copy. We swapped that list for packmem in Qwen3-VL's fine-tuning dataset class and ran it on the real 665,298-record LLaVA mix on an Apple M4 Pro. With spawn workers the first batch arrived in 4.1 s instead of 22.9 s, because the pickled dataset is 309 MB instead of 690 MB. In 0.1.2, rows.share() writes the table to a file once, and each spawn worker loads it from there instead of receiving a pickle: parent and workers peaked at 2.7 GB instead of 4.5 GB, and the first batch arrived in 1.2 s instead of 4.1 s.
Annotations only: no images, model or GPU. Building the dataset took 3.2 times as long, because conversation turns are stored as JSON text. None of the patched repositories has tests for the patched code, so correctness rests on every record equalling json.load's output and on identical batch hashes. This is our patch, not an upstream integration. share() was measured on 20,000 random samples with 4 spawn workers; each worker still holds its own copy of the packed table. Patches, harness and raw outputs.
Against Hugging Face datasets
0.2.0, verified on Linux37% less memory than memory-mapped Arrow, from a file 2.5 times smaller.
Save the packed table once, then packmem.open() it: the parent and every worker read one mapped file, as with Hugging Face datasets. Above a loader serving no data, packmem added 5.4 MB (10 MB with share()) against 243 MB for datasets and 15.1 GB for a list. Most of datasets' extra is not data: each worker imports the library, 56 MB a process against 0.2 MB for packmem. The file is 309 MB against a 785 MB Arrow cache, so less page cache per machine, with no schema to declare. With fork workers the machine held 549 MB against 994 MB, and the first batch arrived in 1.0 s against 1.8 s and 63 s for a list.
Our fine-tuning loader demo shows the same on a Mac with spawn workers: 2.8 MB above the floor with packmem against 228 MB with datasets. The demo and its results.
Where datasets still wins.
Its batched access (ds[i:i+64], ds.iter()) was about twice as fast in our earlier comparison, its compressed Parquet source is smaller at rest, and parsing JSON into packmem is slower than json.load the first time.
packmem 0.2.0 and datasets 5.2.0 from PyPI, torch 2.15.0+cpu, one shared Linux server, medians of 3 runs per arm. Annotations only: no images, model or GPU. Memory held to within about 1 MB across runs; the server's load was 10 to 27, so timings are indicative. share() keeps its file in /dev/shm, which is RAM; the whole-machine figure counts the pages the processes touched. Every variant produced identical batches. Write-up, scripts and raw results.
Edge functions and small containers
A Cloudflare Worker gets 128 MB, WebAssembly included, and packmem/wasm runs the same table, queries and streamed JSON there. In the Workers runtime run locally by wrangler, the rows as objects grew it by 70 MB; packmem held them in 7.7 MB of WebAssembly memory (3.4 MB packed). A filtered group count took 7.8 ms against 11 ms, and exports were level.
Building the table took 11 times as long, because rows go from objects through JSON into WebAssembly, and that leaves garbage: the runtime as a whole grew 50 MB. Local workerd neither collects garbage on demand nor enforces the 128 MB limit, so this is not a capacity test on Cloudflare. packmem 0.1.1, load average 3.0 to 5.6. Script and results.
Data processing before the dataframe
Rows from a database cursor, a CSV file or an API arrive as records, and most pipeline code handles them as records. from_cursor, read_csv and, in Node, fromPgCopy pack them without a dict or object per row; COPY loading in Node took 448 ms against 436 ms for plain node-postgres rows. When a step turns into analytics over millions of rows, a dataframe is the better tool.
What changes when the scaffolding goes
Most work on memory makes data smaller at rest: compressed files, compressed pages, compact formats on the wire. What remains is the scaffolding a running program builds around every value. In the Python heap we sampled, 77% of the words were zeros, pointers and small integers. packmem never builds it, and the data stays usable as it is.
That moves decisions that are usually settled by buying hardware: the instance a service needs to keep its dataset in memory, whether a lookup table has to move out to Redis, whether a dataset fits in an edge function at all.
For training jobs it matters more, because host memory is multiplied by every process that holds the data. Fork workers should share the parent's memory, but Python's reference counts write to every object a worker touches, so the shared pages get copied anyway: each fork worker above peaked at 2.8 GB with the list and 400 MB with packmem, whose columns are a few large buffers rather than millions of objects. On one node with eight ranks of four workers each, that is thirty-two such copies.
The rest of an LLM application has the same shape: retrieval chunks and their metadata, evaluation results, agent traces and chat logs, held as lists of JSON records by long-running processes. We have not measured those yet, and one part of them is the weak case today: when most of a record is a nested list of messages, packmem keeps that part as JSON text, so it saves less and loads slower than json.load.
And where it is the wrong tool, see where it loses.
packmem, polars, pandas and Arrow
They solve different problems. The rows below are about holding records inside an application, which is what packmem is for, plus the analytics features where the dataframes are clearly ahead.
| Feature | packmem | polars | pandas | Arrow |
|---|---|---|---|---|
| Use it as a list of dicts or an array of objects | Yesindex, loop, append, update, sort | Norows on request | Partlyrow by row is slow | Noimmutable tables |
| Small in memory without choosing types by hand | Yesencoding picked per chunk | No8.6× packmem's memory on the flights | No28× packmem's memory on the flights | No11× packmem's memory on the flights |
| Filters and counts run on compressed data | Yes | Partlyon Categorical and Enum, opt-in | Partlyon category, opt-in | Partlyon dictionary arrays, opt-in |
| Missing keys stay missing when written back as JSON | Yes | Nobecome null | Nobecome null | Nobecome null |
| JSON responses streamed from the columns | Yesin chunks, from a filtered view | Partlywhole string or file | Partlywhole string or file | Nono JSON writer |
| Python, Node.js and Cloudflare Workers | YesWebAssembly build for Workers | Partlyno Workers build | PartlyPython only | Yes |
| Joins, window functions and expressions | Nofilter, count, sum, group count | Yes | Yes | Partlyjoins and group-by, no windows |
| Multi-threaded loading and queries | Noloads 6× slower than polars | Yes | No | Yes |
| Nested lists and objects as typed columns | Partlykept as JSON text | Yes | NoPython objects | Yes |
| Worker processes share one copy | Yesopen() maps one saved file; Node worker threads share one table | Partlyvia memory-mapped IPC files | No | Yesmemory-mapped files |
| Parquet and Arrow IPC | No | Yes | Yes | Yes |
Checked against packmem 0.2.0 and the polars, pandas and pyarrow documentation, November 2026. Memory notes are the flights measurements above; Arrow's is pandas with its Arrow backend, the closest we measured.
Use packmem when
Your program keeps records as records, such as a server's dataset, a script's list or a training loader's annotations, and memory sets the limit. Change the container and keep the code.
Use polars when
Analytics: joins, windows and large group-bys over millions of rows, Parquet in and out, every core busy. Once the work is analysis rather than holding records, use a dataframe.
Use pandas when
The notebook ecosystem: plotting, statsmodels, scikit-learn and the large body of existing pandas code, on data that fits comfortably in memory.
Use Arrow when
The format between systems: handing columnar data to another process or language without copying, Parquet and Flight, and building query engines.
Where it loses
- Plain loops over every row are slower. Each record is rebuilt on demand: 4.9× slower than the list in Python. Lazy rows fix loops that read a few fields; rewriting the loop as a query fixes it entirely.
- Random access to whole rows costs more: 6.7× slower for 10,000 random rows.
- Dataframes still win analytics. polars loads 6 times faster than packmem, and pandas is faster on some float filters (0.14 ms against 0.43 ms).
- Group-by over very many distinct values is slow. 436 thousand distinct IP addresses over 2 million rows took 311 ms, against 10 ms to 62 ms in polars.
- Small tables gain nothing. At 100 rows packmem used more memory than the list (0.33 against 0.23 MB); from 300 rows on it used less.
- Building a table from JavaScript objects is slow in WebAssembly. In the Workers runtime, 1,000,000 rows took 772 ms against 72 ms for an array, and the objects left behind count until the runtime collects them.
- Hugging Face datasets is faster for batched access. Slices and batched iteration over its Arrow columns were about twice as fast as packmem's in our comparison on 665,298 LLaVA records, and its compressed Parquet source is smaller at rest.
- A one-line swap can cost time. sqlite-utils used 9.0× less memory but took 5.46 s against 4.85 s.
- Sort, insert and delete rebuild the table, in Python and in Node, so each costs time proportional to its size. In Node, fromPg takes 2.2 times as long as plain node-postgres rows; fromPgCopy, which streams COPY output into the native parser, took 448 ms against 436 ms.
Use it
pip install packmemimport packmem
# was: rows = [json.loads(line) for line in open("events.jsonl")]
rows = packmem.load_jsonl("events.jsonl")
rows.count(status=404)
rows.group_count("country", path__prefix="/api/")
rows.where(status=200).to_json()A list-of-dicts replacement for Python 3.10+: index, iterate, append, update, sort, query, and JSON straight from the columns.
0.3.4 alphaOn PyPI, npm and crates.io, with prebuilt Python and Node builds for Linux (glibc and musl), macOS and Windows. Apache-2.0.
How these numbers were made
- Python and server results on an Apple M4 Pro, 48 GB, macOS, Python 3.12.10, Node v22.22.3. Medians of fresh processes. Load average 3.7 to 5.2 for the Python runs and 3 to 10 for the server runs, so treat differences under about 15% as noise.
- Server memory is measured after a forced garbage collection and 20 seconds idle; measured one second after loading, the Node servers read up to 83% higher.
- Every packmem query and response was checked against the plain-container answer.
- Everything is reproducible from the repository:
benchmarks/run_all.py,demos/bench/bench.mjs,experiments/. The full write-up is docs/report.md.