Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 7 additions & 1 deletion include/openscad_cpp_parser/position.hpp
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
#pragma once

#include "openscad_cpp_parser/shared_string.hpp"

#include <string>

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;
Expand Down
71 changes: 71 additions & 0 deletions include/openscad_cpp_parser/shared_string.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
#pragma once

#include <cstddef>
#include <functional>
#include <memory>
#include <ostream>
#include <string>
#include <string_view>

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<const std::string>(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<const std::string> p_;
};

} // namespace oscad

template <> struct std::hash<oscad::SharedString> {
size_t operator()(const oscad::SharedString& s) const noexcept { return std::hash<std::string>{}(s.str()); }
};
2 changes: 1 addition & 1 deletion include/openscad_cpp_parser/source_map.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion src/grammar/driver.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions tests/test_ast_generation.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
Loading