Repository navigation
perf(ply): reduce export peak memory -- 75% less - #257
Conversation
Keep face indices in NumPy arrays and gather deduplicated vertices in bounded chunks. Preserve NumPy quicksort ordering and exact PLY bytes. The 200,000-triangle export uses 48.00 MB -> 12.01 MB peak memory. With tracing enabled, the complete measurement workload takes 12.6% longer. Tests: python3 -m pytest (1046 passed, 23 skipped). Co-Authored-By: Aiden <aiden@weco.ai>
Keep both supported integer widths explicit so mypy accepts the in-place index offset. Runtime behavior is unchanged. Refs wolph#257 Co-Authored-By: Aiden <aiden@weco.ai>
The PLY tests stayed green with the deduplication switched off past the first 16,384 vertex rows, because no test mesh was that large. This one spans three chunks, with -0.0, 0.0 and NaN rows across both boundaries, and compares the result with np.unique.
4.1.4 deduplicated with np.unique, which reads the plain values of a masked array and flattens vectors of any shape. The chunked version compared masked rows, so a mesh on a masked array saved a masked triangle with its three vertices merged into one, and it indexed one triangle per item of the data, so two-dimensional data, vectors that are not (3, 3) and a dict raised where 4.1.4 wrote a file. The mask is dropped before the chunks run, and anything but one (3, 3) triangle per item goes through np.unique as before, which also keeps the IndexError for data that is longer than its vectors. The memory saving for a regular mesh is unchanged.
|
Thank you for the memory saving, the export of a 200,000 triangle strip went from 47.9 MB to 12.0 MB at its peak here, and the output is byte for byte the same on numpy 1.24, 1.26 and 2.5! I've made one change on top (26b6f77): the vectors go through |
TL;DR
write_plyexport memory for meshes with shared vertices.tracemalloc: 48.00 MB → 12.01 MB, saving 36.00 MB (75.0%) per 200,000-triangle export.python3 -m pytest): 1,046 passed, 23 skipped; 100% coverage.What & Why
The writer built Python tuples for face indices and kept several full-size vertex arrays during deduplication.
Use compact index arrays and gather vertices in small chunks to reduce those temporary allocations.
Keep NumPy's structured quicksort order and row comparisons, including the selected signed-zero vertices and separate NaN rows.
The PLY output bytes stay the same.
Validation
Measured on Linux with Python 3.12.13 and NumPy 2.5.5, using an isolated benchmark core.
The memory workload exports a 200,000-triangle shared-edge strip to binary little-endian PLY in
BytesIOafter warm-up.Tracing starts after mesh construction.
The read comparisons use the repository's Stanford Dragon benchmark, with five iterations per setting.
The traced workload includes the export and small output checks; these times are medians across three runs.
The read code is unchanged, and these mixed timing differences do not establish a read improvement.
These statistics summarize the export memory measurements:
Download the pinned memory benchmark outside the checkout, then run it from the repository root:
Reproduce the read comparisons with:
python3 benchmarks/benchmark_ascii_read.py --iterations 5Ruff lint, Ruff formatting, and the pinned
ty==0.0.56check pass.Additional byte comparisons cover signed zeros, infinities, repeated NaNs, and reversed arrays in all three output modes.
Why this is safe and easy to maintain
The change stays in one private helper and adds no dependencies.
It uses NumPy array operations and two bounded loops, with unchanged public signatures and output formatting.
The PLY tests cover round trips, writer options, and empty meshes; the full suite passes.
AI assistance
This PR was prepared with AI assistance from Aiden. Candidates were tracked with Weco Observe.
Weco share link: https://dashboard.weco.ai/share/v6If0JA54AObqH_1CsdBovpxhTEKfZGY