Repository navigation
Position: share the origin string across a parse's nodes - #18
Merged
Merged
Conversation
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>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Problem
Every AST node copied its file's path into its own
std::string(ParserDriver::toPosition). A path such as/Users/.../BOSL2/shapes3d.scadis longer than anystd::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
oscad::SharedString(include/openscad_cpp_parser/shared_string.hpp): an immutable string whose copies share one buffer. It reads as aconst std::string&, so code that only reads it needs no change. The empty string holds no buffer.Position::origin,SourceSegment::originandParserDriver::origin_use it, so a parse allocates its origin once.Effect (BOSL2's 977 test scripts, via openscad_cpp_evaluator)
All 977 give identical output, exit codes and STL hashes.
Compatibility
A ternary mixing
originwith astd::stringis 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