Skip to content

perf(ply): reduce export peak memory -- 75% less - #257

Merged
wolph merged 2 commits into
wolph:developfrom
dexhunter:perf/ply-export-memory
Nov 17, 2026
Merged

wolph merged 2 commits into
wolph:developfrom
dexhunter:perf/ply-export-memory

Conversation

@dexhunter

Copy link
Copy Markdown
Contributor

TL;DR

  • Reduce write_ply export memory for meshes with shared vertices.
  • Keep face indices in NumPy arrays and build deduplication results in chunks.
  • Peak memory measured with tracemalloc: 48.00 MB → 12.01 MB, saving 36.00 MB (75.0%) per 200,000-triangle export.
  • Tests (python3 -m pytest): 1,046 passed, 23 skipped; 100% coverage.
  • With tracing enabled, the full measurement workload takes 3.179 → 3.580 seconds, or 12.6% longer.

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 BytesIO after warm-up.
Tracing starts after mesh construction.
The read comparisons use the repository's Stanford Dragon benchmark, with five iterations per setting.

Situation Before After Change
PLY export peak memory, 200,000 triangles 48,002,559.2 bytes 12,006,665 bytes 35,995,894.2 bytes saved; 75.0% less
Complete memory workload runtime, tracing enabled 3.179 s 3.580 s 12.6% longer
Python ASCII STL read, 871,414 triangles 7.01 s 7.09 s 1.1% slower in this comparison
ASCII STL read with speedups, 871,414 triangles 1.27 s 1.25 s 1.6% faster in this comparison

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:

Statistic Value
Old -> new (mean) 48,002,559 -> 12,006,665 (75.0% less peak_memory_bytes)
95% CI of the new/old ratio (bootstrap, 10,000 resamples) 0.250125–0.250126 (74.9874–74.9875% less peak_memory_bytes)
Mean ± SD of the change per input -35,995,894 ± 7.155
95% CI of the mean change (t-interval) -35,995,903 to -35,995,885
Number of fixed inputs (n) 5 (3 runs)

Download the pinned memory benchmark outside the checkout, then run it from the repository root:

mkdir -p /tmp/ply-memory/harness
gh api -H 'Accept: application/vnd.github.raw+json' repos/dexhunter/numpy-stl/git/blobs/6e99d22eddba52b99a48c6e6e852cbbffe9a9fe6 > /tmp/ply-memory/harness/write_ply_peak_mem.py
PYTHONPATH="$PWD" python3 /tmp/ply-memory/harness/write_ply_peak_mem.py

Reproduce the read comparisons with:

python3 benchmarks/benchmark_ascii_read.py --iterations 5

Ruff lint, Ruff formatting, and the pinned ty==0.0.56 check 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

dexhunter and others added 2 commits November 16, 2026 22:39
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>
@wolph
wolph merged commit cb2f7cf into wolph:develop Nov 17, 2026
13 checks passed
wolph added a commit that referenced this pull request Nov 17, 2026
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.
wolph added a commit that referenced this pull request Nov 17, 2026


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.
wolph added a commit that referenced this pull request Nov 17, 2026
@wolph

wolph commented Nov 17, 2026

Copy link
Copy Markdown
Owner

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 np.asarray before the chunks run, because a mesh on a masked array kept its mask in the row comparison and saved a masked triangle with its three vertices merged into one. Data that isn't one (3, 3) triangle per item, such as a 2-D array, still goes through np.unique as it did in 4.1.4, where the chunked version raised. I've also added a test that spans three chunks (0175ad0), since the suite stayed green with the deduplication switched off past the first 16,384 rows. The memory saving for a regular mesh is unchanged.

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