Profile-guided CPU and peak RSS optimization in real codebases

Performance pull requests fail in two predictable ways: the author optimizes code that was never on the critical path, or the benchmark runs sequentially on a noisy machine and reports a speedup that disappears under thermal drift or breaks edge-case behavior.

I built oa to enforce a stricter loop across Rust, C, C++20, and Python repositories: profile CPU and live heap first, test every candidate edit in balanced interleaved A/B pairs (ABBA) with bootstrap 95% confidence intervals and Mann-Whitney U p-values, verify bit-identical SHA-256 stdout across option matrices, and run upstream unit tests plus sanitizers or differential fuzzing before keeping any change.

1. Cutting Peak RSS by 56.6% to 72.6% in bootandy/dust

Walking /usr/share (205,521 files, 8,532 directories) in bootandy/dust #664 took 42.5 ms and peaked at 67.2 MiB of resident memory. Profiling and heap inspection pointed to three allocations scaling linearly with total file count rather than output tree size:

Folding non-top-k files directly into parent byte counters, skipping inode tracking when nlink == 1, and caching NodeKind dropped Peak RSS from 67.2 MiB to 29.1 MiB (-56.6% default) and from 60.2 MiB to 16.5 MiB (-72.6% with -D), while cutting wall time by 1.56x (default), 2.10x (-D), and 2.33x (-t). All 90 cargo tests pass and stdout SHA-256 matches identically across 16 CLI flag combinations.

2. Removing O(N*M) array subtraction in jqlang/jq

In jqlang/jq #3768, profiling $a - $b on large JSON arrays showed almost all CPU time inside binop_minus's nested loop calling jv_equal on every pair. Small arrays stay fast with linear scans, so replacing the general path unconditionally would regress tiny inputs.

Adding a gated open-addressed hash set over canonical jv hashes for larger subtractions preserved jq's exact JSON equality rules (including -0.0 == 0.0, NaN semantics, and jv reference counting) and sped up subtraction from 535.6 ms to 4.3 ms on numeric arrays (124.36x), 69.76x on strings, and 25.29x on objects, passing all 9 upstream test suites cleanly under ASan, LSan, and UBSan.

3. Streaming C++20 relinker passes and consteval x86-64 decoding

In boykopovar/AnyPS5 #28125 (approved), the AMD64-only instruction matcher previously decoded an entire binary into an intermediate vector before matching patterns. Streaming directly over decoded instructions and dispatching on opcode cut Peak RSS by 67.0% (164.9 MiB to 54.4 MiB) and sped up scanning 3.26x to 5.03x.

In boykopovar/AnyPS5 #28223, replacing repeated branch chains in X64InstructionDecoder with 256-entry consteval lookup tables and a single-pass DecodeInstruction() sped up decoding a 3.9 MiB .text section (~9M instructions) by 1.43x (Decode) and 1.70x (DecodeInstruction). A 472,032-vector differential oracle against main found zero metadata mismatches and surfaced a latent XOP map-9 (0x8F .. 0xF7) check bug on main. During review, guarding fast-path ModRM reads strictly behind hasModRm eliminated speculative reads past single-byte opcodes like 0x90, and adding the XOP map-9 fixture verified that reverting the decoder fails decoder_length_tests.

4. What review caught and rejected

Not every fast patch should land. In sharkdp/bat #4154 (merged for v0.26.0), switching preprocessor transforms to Cow<'_, str> and adding a printable-ASCII printer fast path delivered 1.80x on wrapped output and 1.32x on --show-all with zero behavioral risk. Follow-up sharkdp/bat #4165 applied --heap-track to startup and output I/O, replacing eager compilation of 129 builtin globset regex DFAs with compile-time FastMatcher / ASCII prefilters, lazily deserializing integrated themes, recycling line buffers, and buffering owned stdout/pager writes (flushing per input file so I/O cycle detection stays intact). That cut plain/piped runtime 2.97x (11.49 to 3.90 ms) and peak-live heap 88.7% (1042.0 to 117.7 KiB), and sped up full-style output 1.70x with -37.0% peak-live heap.

In Delgan/loguru #1557 (merged), the maintainer asked to split the branch into individually benchmarked commits. Four commits landed (direct datetime construction with timezone caching in aware_now, co_filename caching, removing a @contextmanager generator in _log, and ISO/HMS formatter fast paths), while the fifth commit replacing _protected_lock() with a custom context manager class was dropped. In CPython, an asynchronous exception such as KeyboardInterrupt arriving between lock acquisition and __enter__ returning can skip __exit__ on custom context managers and leave a lock held. Isolating each commit let the safe 4/5 ship with 22/22 green checks instead of holding up or risking the whole change.

Measured results summary

PR Language Wall time (median) Peak RSS Status
sharkdp/bat #4154 Rust 49.42 -> 26.88 ms (1.80x) Unchanged Merged (v0.26.0)
sharkdp/bat #4165 Rust 11.49 -> 3.90 ms (2.97x plain), 1.70x full -88.7% / -37.0% peak-live heap Open
Delgan/loguru #1557 Python 4 commits, 1.04x to 1.16x each -2.9% Merged
bootandy/dust #664 Rust 1.56x (default), 2.10x (-D), 2.33x (-t) -56.6% to -72.6% Open
jqlang/jq #3768 C 535.6 -> 4.3 ms (124.36x) -0.8% Open
AnyPS5 #28125 C++20 617.3 -> 122.6 ms (3.26x to 5.03x) 164.9 -> 54.4 MiB (-67.0%) Approved
AnyPS5 #28223 C++20 109.66 -> 63.87 ms (1.70x) -0.3% to +1.3% Approved

Full reproduction commands, profiler diff charts, and local showcases (including rejected dead ends and regression guards) are published in oa/BENCHMARKS.md.