Skip to content

Commit

Permalink
Performance improvement
Browse files Browse the repository at this point in the history
  • Loading branch information
yhirose committed Jan 7, 2022
1 parent 578eec7 commit b85606c
Showing 1 changed file with 22 additions and 12 deletions.
34 changes: 22 additions & 12 deletions peglib.h
Original file line number Diff line number Diff line change
Expand Up @@ -459,11 +459,15 @@ inline constexpr unsigned int operator"" _(const char *s, size_t l) {
/*
* Semantic values
*/
class Context;

struct SemanticValues : protected std::vector<std::any> {
SemanticValues() = default;
SemanticValues(Context *c): c_(c) {}

// Input text
const char *path = nullptr;
const char *ss = nullptr;
std::function<const std::vector<size_t> &()> source_line_index;

// Matched string
std::string_view sv() const { return sv_; }
Expand Down Expand Up @@ -553,6 +557,9 @@ struct SemanticValues : protected std::vector<std::any> {
friend class Holder;
friend class PrecedenceClimbing;

const std::vector<size_t> &source_line_index() const;

Context *c_ = nullptr;
std::string_view sv_;
size_t choice_count_ = 0;
size_t choice_ = 0;
Expand Down Expand Up @@ -773,7 +780,6 @@ struct ErrorInfo {
/*
* Context
*/
class Context;
class Ope;
class Definition;

Expand Down Expand Up @@ -882,7 +888,7 @@ class Context {
SemanticValues &push() {
assert(value_stack_size <= value_stack.size());
if (value_stack_size == value_stack.size()) {
value_stack.emplace_back(std::make_shared<SemanticValues>());
value_stack.emplace_back(std::make_shared<SemanticValues>(this));
} else {
auto &vs = *value_stack[value_stack_size];
if (!vs.empty()) {
Expand All @@ -898,15 +904,6 @@ class Context {
auto &vs = *value_stack[value_stack_size++];
vs.path = path;
vs.ss = s;
vs.source_line_index = [&]() -> const std::vector<size_t> & {
if (source_line_index.empty()) {
for (size_t pos = 0; pos < l; pos++) {
if (s[pos] == '\n') { source_line_index.push_back(pos); }
}
source_line_index.push_back(l);
}
return source_line_index;
};

return vs;
}
Expand Down Expand Up @@ -2469,6 +2466,19 @@ inline size_t parse_literal(const char *s, size_t n, SemanticValues &vs,
return i;
}

inline const std::vector<size_t> &SemanticValues::source_line_index() const {
if (!c_) {
std::vector<size_t>();
}
if (c_->source_line_index.empty()) {
for (size_t pos = 0; pos < c_->l; pos++) {
if (c_->s[pos] == '\n') { c_->source_line_index.push_back(pos); }
}
c_->source_line_index.push_back(c_->l);
}
return c_->source_line_index;
}

inline void Context::set_error_pos(const char *a_s, const char *literal) {
if (log) {
if (error_info.error_pos <= a_s) {
Expand Down

0 comments on commit b85606c

Please sign in to comment.