Skip to content

Position: share the origin string across a parse's nodes - #18

Merged
revarbat merged 1 commit into
mainfrom
perf/shared-origin
Nov 30, 2026
Merged

revarbat merged 1 commit into
mainfrom
perf/shared-origin

Conversation

@revarbat

Copy link
Copy Markdown
Member

Problem

Every AST node copied its file's path into its own std::string (ParserDriver::toPosition). A path such as /Users/.../BOSL2/shapes3d.scad is longer than any std::string's inline buffer, so each node made its own heap copy. Over BOSL2's 977 test scripts that was 219.6 M copies: 99% of all long-string copies and 37% of all allocations.

Change

  • New oscad::SharedString (include/openscad_cpp_parser/shared_string.hpp): an immutable string whose copies share one buffer. It reads as a const std::string&, so code that only reads it needs no change. The empty string holds no buffer.
  • Position::origin, SourceSegment::origin and ParserDriver::origin_ use it, so a parse allocates its origin once.

Effect (BOSL2's 977 test scripts, via openscad_cpp_evaluator)

before after
instructions 1,036.5 G 935.8 G (−9.7%)
allocations 593 M 374 M (−37%)
bytes allocated 59.0 GB 45.5 GB

All 977 give identical output, exit codes and STL hashes.

Compatibility

A ternary mixing origin with a std::string is now ambiguous and needs .str(). The evaluator needed that at three sites (its PR follows), plus a nanobind caster for its Python bindings.

New test: PositionASTNodes.EveryNodeSharesOneOriginBuffer. All 675 tests pass locally.

🤖 Generated with Claude Code

Every AST node copied its file's path into its own std::string. Running
BOSL2's 977 tests, that was 219.6 M heap copies: 99% of the parser's
long-string copies and 37% of all allocations. Position::origin and
SourceSegment::origin are now a SharedString (new: an immutable string whose
copies share one buffer and read as a const std::string&), so a parse
allocates its origin once.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
@revarbat
revarbat merged commit 105e1eb into main Nov 30, 2026
3 checks passed
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