From 6c92b422704f3523e04010ca118f588848d612e6 Mon Sep 17 00:00:00 2001 From: Revar Desmera Date: Sun, 29 Nov 2026 23:58:56 -0800 Subject: [PATCH] Position: share the origin string across a parse's nodes 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 --- include/openscad_cpp_parser/position.hpp | 8 ++- include/openscad_cpp_parser/shared_string.hpp | 71 +++++++++++++++++++ include/openscad_cpp_parser/source_map.hpp | 2 +- src/grammar/driver.hpp | 2 +- tests/test_ast_generation.cpp | 9 +++ 5 files changed, 89 insertions(+), 3 deletions(-) create mode 100644 include/openscad_cpp_parser/shared_string.hpp diff --git a/include/openscad_cpp_parser/position.hpp b/include/openscad_cpp_parser/position.hpp index 2180c13..22c1e04 100644 --- a/include/openscad_cpp_parser/position.hpp +++ b/include/openscad_cpp_parser/position.hpp @@ -1,13 +1,19 @@ #pragma once +#include "openscad_cpp_parser/shared_string.hpp" + #include namespace oscad { // One Position per node encodes a full source span via start/end byte // offsets (0-based), plus the 1-indexed line/column of the span's start. +// +// `origin` is shared: every node of one file holds the same buffer, where a +// std::string copied the file's path into each of them (99% of the parser's +// long-string copies, 37% of all allocations running BOSL2's tests). struct Position { - std::string origin; + SharedString origin; int line = 1; int column = 1; int start_offset = 0; diff --git a/include/openscad_cpp_parser/shared_string.hpp b/include/openscad_cpp_parser/shared_string.hpp new file mode 100644 index 0000000..d30562d --- /dev/null +++ b/include/openscad_cpp_parser/shared_string.hpp @@ -0,0 +1,71 @@ +#pragma once + +#include +#include +#include +#include +#include +#include + +namespace oscad { + +// An immutable string whose copies share one buffer: copying is a reference +// count bump, never a copy of the characters. For a value copied far more +// often than it is built -- a file name stamped on every AST node of that +// file, a string Value passed through function calls -- where std::string +// would copy the characters at every step once they outgrow its inline +// buffer (22 bytes in libc++, 15 in libstdc++ and MSVC). +// +// Reads as a `const std::string&`, so code that only reads a string takes +// one unchanged. The empty string holds no buffer at all. +class SharedString { +public: + SharedString() = default; + SharedString(std::string s) // NOLINT(google-explicit-constructor): a drop-in for std::string + : p_(s.empty() ? nullptr : std::make_shared(std::move(s))) {} + SharedString(const char* s) : SharedString(std::string(s)) {} // NOLINT(google-explicit-constructor) + SharedString(std::string_view s) : SharedString(std::string(s)) {} // NOLINT(google-explicit-constructor) + + const std::string& str() const { return p_ ? *p_ : empty_(); } + operator const std::string&() const { return str(); } // NOLINT(google-explicit-constructor) + operator std::string_view() const { return str(); } // NOLINT(google-explicit-constructor) + + size_t size() const { return str().size(); } + size_t length() const { return str().size(); } + bool empty() const { return !p_; } + const char* c_str() const { return str().c_str(); } + const char* data() const { return str().data(); } + std::string::const_iterator begin() const { return str().begin(); } + std::string::const_iterator end() const { return str().end(); } + char operator[](size_t i) const { return str()[i]; } + + friend bool operator==(const SharedString& a, const SharedString& b) { + return a.p_ == b.p_ || a.str() == b.str(); + } + friend bool operator!=(const SharedString& a, const SharedString& b) { return !(a == b); } + friend bool operator<(const SharedString& a, const SharedString& b) { return a.str() < b.str(); } + friend bool operator==(const SharedString& a, const std::string& b) { return a.str() == b; } + friend bool operator==(const std::string& a, const SharedString& b) { return a == b.str(); } + friend bool operator!=(const SharedString& a, const std::string& b) { return a.str() != b; } + friend bool operator!=(const std::string& a, const SharedString& b) { return a != b.str(); } + friend bool operator==(const SharedString& a, const char* b) { return a.str() == b; } + friend bool operator!=(const SharedString& a, const char* b) { return a.str() != b; } + friend std::string operator+(const std::string& a, const SharedString& b) { return a + b.str(); } + friend std::string operator+(const SharedString& a, const std::string& b) { return a.str() + b; } + friend std::string operator+(const char* a, const SharedString& b) { return a + b.str(); } + friend std::string operator+(const SharedString& a, const char* b) { return a.str() + b; } + friend std::ostream& operator<<(std::ostream& os, const SharedString& s) { return os << s.str(); } + +private: + static const std::string& empty_() { + static const std::string e; + return e; + } + std::shared_ptr p_; +}; + +} // namespace oscad + +template <> struct std::hash { + size_t operator()(const oscad::SharedString& s) const noexcept { return std::hash{}(s.str()); } +}; diff --git a/include/openscad_cpp_parser/source_map.hpp b/include/openscad_cpp_parser/source_map.hpp index 2760741..866bf14 100644 --- a/include/openscad_cpp_parser/source_map.hpp +++ b/include/openscad_cpp_parser/source_map.hpp @@ -12,7 +12,7 @@ namespace oscad { // A contiguous run of one origin's content within a SourceMap's combined // buffer. Mirrors source_map.py::SourceSegment. struct SourceSegment { - std::string origin; + SharedString origin; // shared with every Position mapped back to it int start_line = 1; int start_column = 1; std::string content; diff --git a/src/grammar/driver.hpp b/src/grammar/driver.hpp index 100153b..de8ae0e 100644 --- a/src/grammar/driver.hpp +++ b/src/grammar/driver.hpp @@ -36,7 +36,7 @@ class ParserDriver { public: explicit ParserDriver(std::string origin) : origin_(std::move(origin)) {} - std::string origin_; + SharedString origin_; // shared by every Position this parse makes NodeList result; bool hadError = false; // Copied from the ambient StrictCommaScope at parse start (api.cpp), so diff --git a/tests/test_ast_generation.cpp b/tests/test_ast_generation.cpp index 8108fe2..f2225ca 100644 --- a/tests/test_ast_generation.cpp +++ b/tests/test_ast_generation.cpp @@ -35,6 +35,15 @@ TEST(PositionASTNodes, CustomOrigin) { EXPECT_EQ(ast[0]->position().origin, "test.scad"); } +TEST(PositionASTNodes, EveryNodeSharesOneOriginBuffer) { + // A path past every std::string inline buffer, so a copy would show. + const std::string path = "/a/path/long/enough/to/need/the/heap/test.scad"; + auto ast = parseAst("x = 42;\ny = x + 1;", path); + ASSERT_EQ(ast.size(), 2u); + EXPECT_EQ(ast[0]->position().origin, path); + EXPECT_EQ(ast[0]->position().origin.data(), ast[1]->position().origin.data()); +} + TEST(PositionASTNodes, LineColumn) { auto ast = parseSrc("x = 42;"); EXPECT_EQ(ast[0]->position().line, 1);