Every memory figure after the first benchmark read 0 MB
Benchmarking the pandas-to-Polars rewrite, a same-process peak-memory delta read zero for everything that ran after ingestion. Each measured call now runs in its own forked process.
0 MBpeak memory the first harness reported for every benchmark after the first
Written up
Context
In August I rewrote PricePoint’s slow paths from pandas to Polars and vectorised NumPy: CSV ingestion, the rolling and lag features, and the price-leadership cross-correlation. The benchmark script ran old and new versions on the project’s real data, five retailer CSVs totalling 791 MB and 9.5 million rows, on a 16-CPU Linux machine with 8 GB of RAM. On that machine memory mattered as much as time, so every benchmark reports peak resident memory beside wall time.
Symptom
The first version read resource.getrusage(RUSAGE_SELF).ru_maxrss before and after each call in the benchmark process and reported the difference. Ingestion ran first and pushed the process to several gigabytes. Every benchmark after it came out at exactly 0 MB, including pandas group-by transforms that obviously allocate.
Root cause
ru_maxrss is a high-water mark for the whole process, and it only goes up. Once ingestion had pushed it to several gigabytes, no smaller call could move it, so every later delta was zero. The counter was correct about the process; it was answering a different question from the one I was asking.
The usual replacement, a background thread sampling RSS while the call runs, also reported about 0 MB for pandas operations known to allocate hundreds of megabytes. Long calls into pandas’ compiled code hold the GIL, so the sampling thread rarely ran during the allocations it was meant to catch. It under-measured precisely the old implementations the comparison was about.
Fix
Each measured call now runs in its own child process started with fork. The child reads ru_maxrss immediately before and after the call, so each measurement has its own high-water mark and cannot inherit one from an earlier benchmark. Fork shares the parent’s memory copy-on-write, so the input DataFrames are not pickled across. Only the elapsed time, the memory delta and the result’s row count come back through a queue; sending a DataFrame of several hundred megabytes back would have distorted the measurement it was part of.
Source: PricepointDynamics docs/benchmarks.md at commit 9c6cf1c. The old feature code was run only on the 10% sample; its full-scale time in that file is a ×10 extrapolation and is labelled as one.
The working harness made the rewrite look worse in one place. The Polars features use more memory than pandas on the sample, and 4.4 GB at full scale on an 8 GB machine. With the zeros, that would not have shown. The speed gains (151.7× on the feature sample) were never in doubt; time is the easy half of a benchmark.
Verification
Speed was only worth having if the output stayed the same, so the old pandas add_temporal_features was kept in the test module as a reference, and the Polars version is compared with it on multi-group data, on series with date gaps and on single-row groups. Those tests pass, and they establish one thing: the new code computes what the old code computed.
That included the old code’s mistakes. The rolling windows contained the current row, and the series key, (canonical_name, supermarket), merged pack sizes. Both were reproduced faithfully, both were target leakage, and neither was found until a naive baseline exposed them in November (see the forecasting entry). An equivalence test checks sameness, not correctness, and a rewrite is the cheapest moment to ask the second question.
Status
Closed for the measurement. The harness is Linux-specific in two ways: it depends on fork, and it divides ru_maxrss by 1,000 because Linux reports kilobytes. macOS reports bytes, so run unchanged on a Mac, every figure would be a thousand times too large.