Profile and optimize
- Eliminate unnecessary copies of grammar objects - Do cheaper comparisons first in equality methods
This commit is contained in:
parent
68d44fd565
commit
3b388d66cd
7 changed files with 20 additions and 22 deletions
|
|
@ -20,10 +20,10 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
class FirstSet : public rules::RuleFn<set<ISymbol>> {
|
||||
const PreparedGrammar grammar;
|
||||
const PreparedGrammar *grammar;
|
||||
set<ISymbol> visited_symbols;
|
||||
public:
|
||||
explicit FirstSet(const PreparedGrammar &grammar) : grammar(grammar) {}
|
||||
explicit FirstSet(const PreparedGrammar *grammar) : grammar(grammar) {}
|
||||
|
||||
set<ISymbol> apply_to(const ISymbol *rule) {
|
||||
if (visited_symbols.find(*rule) == visited_symbols.end()) {
|
||||
|
|
@ -32,7 +32,7 @@ namespace tree_sitter {
|
|||
if (rule->is_token()) {
|
||||
return set<ISymbol>({ *rule });
|
||||
} else {
|
||||
return apply(grammar.rule(*rule));
|
||||
return apply(grammar->rule(*rule));
|
||||
}
|
||||
} else {
|
||||
return set<ISymbol>();
|
||||
|
|
@ -49,7 +49,7 @@ namespace tree_sitter {
|
|||
|
||||
set<ISymbol> apply_to(const rules::Seq *rule) {
|
||||
auto result = apply(rule->left);
|
||||
if (rule_can_be_blank(rule->left, grammar)) {
|
||||
if (rule_can_be_blank(rule->left, *grammar)) {
|
||||
return set_union(result, apply(rule->right));
|
||||
} else {
|
||||
return result;
|
||||
|
|
@ -58,7 +58,7 @@ namespace tree_sitter {
|
|||
};
|
||||
|
||||
set<ISymbol> first_set(const rules::rule_ptr &rule, const PreparedGrammar &grammar) {
|
||||
return FirstSet(grammar).apply(rule);
|
||||
return FirstSet(&grammar).apply(rule);
|
||||
}
|
||||
|
||||
set<ISymbol> first_set(const ParseItemSet &item_set, const PreparedGrammar &grammar) {
|
||||
|
|
|
|||
|
|
@ -15,9 +15,7 @@ namespace tree_sitter {
|
|||
Item(lhs, rule) {}
|
||||
|
||||
bool LexItem::operator==(const LexItem &other) const {
|
||||
bool lhs_eq = other.lhs == lhs;
|
||||
bool rules_eq = (*other.rule == *rule);
|
||||
return lhs_eq && rules_eq;
|
||||
return (other.lhs == lhs) && other.rule->operator==(*rule);
|
||||
}
|
||||
|
||||
bool LexItem::is_token_start() const {
|
||||
|
|
|
|||
|
|
@ -17,11 +17,11 @@ namespace tree_sitter {
|
|||
lookahead_sym(lookahead_sym) {}
|
||||
|
||||
bool ParseItem::operator==(const ParseItem &other) const {
|
||||
bool lhs_eq = other.lhs == lhs;
|
||||
bool rules_eq = (*other.rule == *rule);
|
||||
bool consumed_sym_counts_eq = (other.consumed_symbol_count == consumed_symbol_count);
|
||||
bool lookaheads_eq = other.lookahead_sym == lookahead_sym;
|
||||
return lhs_eq && rules_eq && consumed_sym_counts_eq && lookaheads_eq;
|
||||
return
|
||||
(other.lhs == lhs) &&
|
||||
(other.consumed_symbol_count == consumed_symbol_count) &&
|
||||
(other.lookahead_sym == lookahead_sym) &&
|
||||
(other.rule->operator==(*rule));
|
||||
}
|
||||
|
||||
int ParseItem::precedence() const {
|
||||
|
|
|
|||
|
|
@ -37,18 +37,18 @@ namespace tree_sitter {
|
|||
};
|
||||
|
||||
class CanBeBlankRecursive : public CanBeBlank {
|
||||
const PreparedGrammar grammar;
|
||||
const PreparedGrammar *grammar;
|
||||
set<rules::ISymbol> visited_symbols;
|
||||
using CanBeBlank::visit;
|
||||
|
||||
public:
|
||||
using CanBeBlank::apply_to;
|
||||
explicit CanBeBlankRecursive(const PreparedGrammar &grammar) : grammar(grammar) {}
|
||||
explicit CanBeBlankRecursive(const PreparedGrammar *grammar) : grammar(grammar) {}
|
||||
|
||||
bool apply_to(const rules::ISymbol *rule) {
|
||||
if (visited_symbols.find(*rule) == visited_symbols.end()) {
|
||||
visited_symbols.insert(*rule);
|
||||
return !rule->is_token() && apply(grammar.rule(*rule));
|
||||
return !rule->is_token() && apply(grammar->rule(*rule));
|
||||
} else {
|
||||
return false;
|
||||
}
|
||||
|
|
@ -60,7 +60,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
bool rule_can_be_blank(const rules::rule_ptr &rule, const PreparedGrammar &grammar) {
|
||||
return CanBeBlankRecursive(grammar).apply(rule);
|
||||
return CanBeBlankRecursive(&grammar).apply(rule);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -20,13 +20,13 @@ namespace tree_sitter {
|
|||
Grammar(grammar),
|
||||
aux_rules({}) {}
|
||||
|
||||
const rule_ptr PreparedGrammar::rule(const ISymbol &symbol) const {
|
||||
const rule_ptr & PreparedGrammar::rule(const ISymbol &symbol) const {
|
||||
return symbol.is_auxiliary() ?
|
||||
aux_rules[symbol.index].second :
|
||||
rules[symbol.index].second;
|
||||
}
|
||||
|
||||
string PreparedGrammar::rule_name(const ISymbol &symbol) const {
|
||||
const string & PreparedGrammar::rule_name(const ISymbol &symbol) const {
|
||||
return symbol.is_auxiliary() ?
|
||||
aux_rules[symbol.index].first :
|
||||
rules[symbol.index].first;
|
||||
|
|
|
|||
|
|
@ -15,8 +15,8 @@ namespace tree_sitter {
|
|||
PreparedGrammar(const Grammar &grammar);
|
||||
|
||||
bool operator==(const PreparedGrammar &other) const;
|
||||
std::string rule_name(const rules::ISymbol &symbol) const;
|
||||
const rules::rule_ptr rule(const rules::ISymbol &symbol) const;
|
||||
const std::string & rule_name(const rules::ISymbol &symbol) const;
|
||||
const rules::rule_ptr & rule(const rules::ISymbol &symbol) const;
|
||||
|
||||
const std::vector<std::pair<std::string, rules::rule_ptr>> aux_rules;
|
||||
};
|
||||
|
|
|
|||
|
|
@ -23,7 +23,7 @@ namespace tree_sitter {
|
|||
}
|
||||
|
||||
size_t ISymbol::hash_code() const {
|
||||
return hash<size_t>()(index) ^ hash<int16_t>()(options);
|
||||
return hash<int>()(index) ^ hash<int16_t>()(options);
|
||||
}
|
||||
|
||||
rule_ptr ISymbol::copy() const {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue