Skip to content

perf(stl): speed up ASCII float conversion -- 6.6% faster - #254

Open
dexhunter wants to merge 1 commit into
wolph:developfrom
dexhunter:perf/ascii-float-conversion
Open

dexhunter wants to merge 1 commit into
wolph:developfrom
dexhunter:perf/ascii-float-conversion

Conversation

@dexhunter

Copy link
Copy Markdown
Contributor

TL;DR

  • Speed up float conversion in BaseStl._write_ascii for Python ASCII mesh saves.
  • Use map(float, ...) and skip explicit vertex tuples while keeping the same output formatting.
  • A 20,000-triangle save workload takes 185.42 ms → 173.25 ms: 12.17 ms saved, with a 6.6% median speed gain.
  • python -m pytest tests/test_ascii.py --basetemp=tmp: 17 passed, 3 skipped.

What & Why

The writer used Python generator expressions to convert each normal and vertex component to a float.
It also built a tuple for each vertex before passing its values to the formatter.

Use map for these conversions and pass vertex values directly to the existing formatter.
Keep normal conversion before vertex access, the same nine significant digits, and the same write order.

Validation

Measured on Linux with Python 3.12.13 and NumPy 2.5.4, using an isolated benchmark core.
The save workload writes a 20,000-triangle surface to BytesIO through Mesh.save(mode=Mode.ASCII), plus seven small regression cases.
The read rows use the repository's Stanford Dragon benchmark, with five iterations per setting.

Situation Before After Change
Python ASCII save, 20,000 triangles plus regression cases 185.42 ms 173.25 ms 12.17 ms saved; 6.6% median speed gain
Python ASCII read, 871,414 triangles 6.99 s 7.21 s 3.1% slower in this comparison
ASCII read with speedups, 871,414 triangles 1.26 s 1.24 s 1.6% faster in this comparison

These statistics summarize the save measurements:

Statistic Value
Median new/old time ratio 0.934 (6.6% faster)
95% CI of the median (bootstrap, 10,000 resamples) 0.927–0.935 (6.5–7.3% faster)
Mean ± SD of the ratio 0.932 ± 0.010
P95 ratio (slowest 5% of pairs) 0.947
Number of old/new pairs (n) 21 (3 runs)

The read path is unchanged; the read measurements show mixed variation, and this PR claims no read improvement.

Read benchmark reproduction:

python benchmarks/benchmark_ascii_read.py --iterations 5

The full suite (python -m pytest) passed with 1,032 passed and 23 skipped after installing the package with test dependencies.
The initial source-only environment lacked distribution metadata and failed two version tests; installing the package resolved both failures.
Ruff lint, Ruff formatting, and the pinned ty==0.0.56 check passed.

Why this is safe and easy to maintain

The patch changes four expressions and adds no branches or helpers.
It preserves float conversion, output bytes, names, and optional C writer dispatch.
ASCII tests cover round trips and tiny coordinates; exact-output checks also cover empty meshes, Unicode names, signed zero, and text-stream errors.

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/hTONguMVsnWUU1pW-8IgLbU60AaqeXCw

Use map for normal and vertex float conversions. Skip explicit vertex
tuples while preserving nine-digit formatting and write order.

Co-Authored-By: Aiden <aiden@weco.ai>

This branch has not been deployed

No deployments
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.

1 participant