From 10cc75d5033a82c9c0e8f983558aee5fefca19ab Mon Sep 17 00:00:00 2001 From: yhirose Date: Mon, 18 May 2020 01:24:53 -0400 Subject: [PATCH] Performance improvement. Close #82 --- peglib.h | 35 ++++++++++++++++++----------------- 1 file changed, 18 insertions(+), 17 deletions(-) diff --git a/peglib.h b/peglib.h index d99239e..6369e61 100644 --- a/peglib.h +++ b/peglib.h @@ -928,13 +928,13 @@ public: auto &sv = *value_stack[value_stack_size]; if (!sv.empty()) { sv.clear(); - sv.tags.clear(); + if (!sv.tags.empty()) { sv.tags.clear(); } } sv.s_ = nullptr; sv.n_ = 0; sv.choice_count_ = 0; sv.choice_ = 0; - sv.tokens.clear(); + if (!sv.tokens.empty()) { sv.tokens.clear(); } } auto &sv = *value_stack[value_stack_size++]; @@ -962,7 +962,7 @@ public: capture_scope_stack.emplace_back(std::map()); } else { auto &cs = capture_scope_stack[capture_scope_stack_size]; - cs.clear(); + if (!cs.empty()) { cs.clear(); } } capture_scope_stack_size++; } @@ -1070,8 +1070,7 @@ public: c.pop(); c.pop_capture_scope(); }); - const auto &rule = *ope; - auto len = rule.parse(s, n, chldsv, c, dt); + auto len = ope->parse(s, n, chldsv, c, dt); if (success(len)) { if (!chldsv.empty()) { for (size_t i = 0; i < chldsv.size(); i++) { @@ -1224,8 +1223,7 @@ public: c.pop(); c.pop_capture_scope(); }); - const auto &rule = *ope_; - auto len = rule.parse(s, n, chldsv, c, dt); + auto len = ope_->parse(s, n, chldsv, c, dt); if (success(len)) { c.set_error_pos(s); return static_cast(-1); @@ -1255,7 +1253,11 @@ public: class LiteralString : public Ope, public std::enable_shared_from_this { public: - LiteralString(const std::string &s, bool ignore_case) + LiteralString(std::string &&s, bool ignore_case) + : lit_(s), ignore_case_(ignore_case), init_is_word_(false), + is_word_(false) {} + + LiteralString(const std::string& s, bool ignore_case) : lit_(s), ignore_case_(ignore_case), init_is_word_(false), is_word_(false) {} @@ -1614,11 +1616,11 @@ inline std::shared_ptr dic(const std::vector &v) { return std::make_shared(v); } -inline std::shared_ptr lit(const std::string &s) { +inline std::shared_ptr lit(std::string &&s) { return std::make_shared(s, false); } -inline std::shared_ptr liti(const std::string &s) { +inline std::shared_ptr liti(std::string &&s) { return std::make_shared(s, true); } @@ -2378,8 +2380,8 @@ inline size_t parse_literal(const char *s, size_t n, SemanticValues &sv, } if (is_word) { - auto ope = std::make_shared(c.wordOpe); - auto len = ope->parse(s + i, n - i, dummy_sv, dummy_c, dummy_dt); + NotPredicate ope(c.wordOpe); + auto len = ope.parse(s + i, n - i, dummy_sv, dummy_c, dummy_dt); if (fail(len)) { return static_cast(-1); } i += len; } @@ -2452,10 +2454,9 @@ inline size_t TokenBoundary::parse_core(const char *s, size_t n, any &dt) const { c.in_token = true; auto se = make_scope_exit([&]() { c.in_token = false; }); - const auto &rule = *ope_; - auto len = rule.parse(s, n, sv, c, dt); + auto len = ope_->parse(s, n, sv, c, dt); if (success(len)) { - sv.tokens.push_back(std::make_pair(s, len)); + sv.tokens.emplace_back(std::make_pair(s, len)); if (c.whitespaceOpe) { auto l = c.whitespaceOpe->parse(s + len, n - len, sv, c, dt); @@ -2519,7 +2520,7 @@ inline size_t Holder::parse_core(const char *s, size_t n, SemanticValues &sv, if (success(len)) { if (!outer_->ignoreSemanticValue) { - sv.emplace_back(val); + sv.emplace_back(std::move(val)); sv.tags.emplace_back(str2tag(outer_->name.c_str())); } } else { @@ -2561,7 +2562,7 @@ inline size_t Reference::parse_core(const char *s, size_t n, SemanticValues &sv, std::vector> args; for (auto arg : args_) { arg->accept(vis); - args.push_back(vis.found_ope); + args.emplace_back(std::move(vis.found_ope)); } c.push_args(std::move(args));