Skip to content

perf: drop redundant per-frame array copies; streaming/perf analysis - #32

Merged
jameskermode merged 1 commit into
masterfrom
perf/read-copies
Jun 9, 2026
Merged

jameskermode merged 1 commit into
masterfrom
perf/read-copies

Conversation

@jameskermode

Copy link
Copy Markdown
Member

Background — investigating the extxyz-ng performance comparison

The Rust port (extxyz-ng) markets ~4× speed and "streaming for large trajectories". I profiled our read path to see what we can actually learn. Two findings reframe the question:

1. We already match it on streaming / memory. iread_dicts opens the file once and yields one Frame at a time; the C extxyz_read_ll allocates exactly one frame and frees it right after conversion. Reading an N-frame trajectory holds one frame in memory at a time — constant memory. (extxyz-ng's multi-frame reader, by contrast, desyncs after a few non-trivial frames and can't read a real trajectory at all — see PR #30's benchmark.)

2. The single-frame gap is the C parser, not Python. On a 20k-atom frame:

time
read_dicts overall 7.21 ms
C read_frame_dicts (parse + convert) 6.83 ms
Python post-processing 0.38 ms (~5%)

Controlled experiments isolate the costs further:

  • pos:R:3 only (floats, no strings): 4.74 ms — already ≈ extxyz-ng's total (4.5 ms). The dominant cost is pcre2_match per line.
  • adding species:S:1: 6.93 ms — the string column adds ~2.2 ms (per-cell malloc+strcpy in C and per-cell decode in Python), vs only ~0.37 ms for an equivalent int column.

This PR

Removes the only worthwhile Python-side win the profile revealed: _read_frame_dict copied each column into a structured array and back out again (two redundant copies). Since read_frame_dicts frees the C buffers before returning, the arrays it hands back are already owned — so we use them directly. ~3% faster (7.21 → 6.99 ms) and a simpler code path. Pure-Python path unchanged.

Verified: core suite (both backends) + ase-extxyz plugin suite green.

What we are not doing (and why)

The remaining levers don't pass the profile's cost/benefit bar pre-release:

  • atof/STR_INCR micro-opts — negligible for typical short numeric fields.
  • String-column cost (~26%) is the real lever, but cutting it needs a contiguous fixed-width buffer representation in C (so Python can np.frombuffer instead of decoding 20k cells in a loop). That touches the C data model, free_dict, the writer, the buffer-aliasing path, and py_to_c_dict — high blast radius for ~1.5–2 ms.
  • Beating pcre2_match would mean a hand-rolled per-line tokenizer — a substantial rewrite.

Given we already win on correctness and memory and match streaming, these larger rewrites aren't justified for this release; documenting them here as future options.

🤖 Generated with Claude Code

`read_frame_dicts` already frees the C buffers before returning, so the numpy
arrays it hands back are independently owned. `_read_frame_dict` was then
copying every column into a structured array and copying it straight back out
again (`data[name] = value` then `.copy()`) — two redundant copies per column.
Use the returned arrays directly for the C-extension path; the pure-Python path
still needs the structured array from np.fromregex/genfromtxt and is unchanged.

Measured ~3% faster on a 20k-atom single-frame read (7.21 -> 6.99 ms min) and a
simpler code path. Profiling shows Python post-processing is only ~5% of the
total — the rest is the C parser (pcre2_match per line) — so this is the only
worthwhile Python-side win; see PR description for the full analysis.

Core suite (both backends) and the ase-extxyz plugin suite pass unchanged.

Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
@jameskermode
jameskermode merged commit 6269817 into master Jun 9, 2026
26 checks passed
@bernstei

bernstei commented Jun 9, 2026

Copy link
Copy Markdown
Contributor

out of curiosity, what do your quoted results imply about the 4x faster claim?

@jameskermode

Copy link
Copy Markdown
Member Author

I recently added JIT compilation of the regex which make us closer. It was more like 1.6x faster, but with the further optimisations I'm merging now (single string buffer, faster float parser for easy cases) we'll be quite a big faster than the extxyz-ng and ca. 4x faster than the built in ASE extcyz. I will stop there, I think.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants