Merge pull request #1907 from mike-lischke/optimizations
[C++] Small optimizations
This commit is contained in:
commit
35cf1ba4ff
|
@ -126,6 +126,8 @@ size_t ParserATNSimulator::adaptivePredict(TokenStream *input, size_t decision,
|
||||||
} else {
|
} else {
|
||||||
dfa::DFAState *newState = new dfa::DFAState(std::move(s0_closure)); /* mem-check: managed by the DFA or deleted below */
|
dfa::DFAState *newState = new dfa::DFAState(std::move(s0_closure)); /* mem-check: managed by the DFA or deleted below */
|
||||||
s0 = addDFAState(dfa, newState);
|
s0 = addDFAState(dfa, newState);
|
||||||
|
|
||||||
|
delete dfa.s0; // Delete existing s0 DFA state, if there's any.
|
||||||
dfa.s0 = s0;
|
dfa.s0 = s0;
|
||||||
if (s0 != newState) {
|
if (s0 != newState) {
|
||||||
delete newState; // If there was already a state with this config set we don't need the new one.
|
delete newState; // If there was already a state with this config set we don't need the new one.
|
||||||
|
|
|
@ -26,35 +26,34 @@ DFA::DFA(atn::DecisionState *atnStartState, size_t decision)
|
||||||
if (static_cast<atn::StarLoopEntryState *>(atnStartState)->isPrecedenceDecision) {
|
if (static_cast<atn::StarLoopEntryState *>(atnStartState)->isPrecedenceDecision) {
|
||||||
_precedenceDfa = true;
|
_precedenceDfa = true;
|
||||||
s0 = new DFAState(std::unique_ptr<atn::ATNConfigSet>(new atn::ATNConfigSet()));
|
s0 = new DFAState(std::unique_ptr<atn::ATNConfigSet>(new atn::ATNConfigSet()));
|
||||||
_s0Shadow = s0;
|
|
||||||
s0->isAcceptState = false;
|
s0->isAcceptState = false;
|
||||||
s0->requiresFullContext = false;
|
s0->requiresFullContext = false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
DFA::DFA(DFA &&other) : atnStartState(std::move(other.atnStartState)), decision(std::move(other.decision)) {
|
DFA::DFA(DFA &&other) : atnStartState(other.atnStartState), decision(other.decision) {
|
||||||
// Source states are implicitly cleared by the move.
|
// Source states are implicitly cleared by the move.
|
||||||
states = std::move(other.states);
|
states = std::move(other.states);
|
||||||
|
|
||||||
// Manually move s0 pointers.
|
other.atnStartState = nullptr;
|
||||||
|
other.decision = 0;
|
||||||
s0 = other.s0;
|
s0 = other.s0;
|
||||||
other.s0 = nullptr;
|
other.s0 = nullptr;
|
||||||
_s0Shadow = other._s0Shadow;
|
|
||||||
other._s0Shadow = nullptr;
|
|
||||||
|
|
||||||
_precedenceDfa = other._precedenceDfa;
|
_precedenceDfa = other._precedenceDfa;
|
||||||
|
other._precedenceDfa = false;
|
||||||
}
|
}
|
||||||
|
|
||||||
DFA::~DFA() {
|
DFA::~DFA() {
|
||||||
// ml: s0 can be set either in our constructor or by external code, so we need a way to track our own creation.
|
bool s0InList = (s0 == nullptr);
|
||||||
// We could use a shared pointer again (and force that on all external assignments etc.) or just shadow it.
|
|
||||||
// I hesitate moving s0 to the normal states list as this might conflict with consumers of that list.
|
|
||||||
delete _s0Shadow;
|
|
||||||
|
|
||||||
for (auto state : states) {
|
for (auto state : states) {
|
||||||
|
if (state == s0)
|
||||||
|
s0InList = true;
|
||||||
delete state;
|
delete state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!s0InList)
|
||||||
|
delete s0;
|
||||||
}
|
}
|
||||||
|
|
||||||
bool DFA::isPrecedenceDfa() const {
|
bool DFA::isPrecedenceDfa() const {
|
||||||
|
|
|
@ -20,10 +20,10 @@ namespace dfa {
|
||||||
/// Set only allows you to see if it's there.
|
/// Set only allows you to see if it's there.
|
||||||
|
|
||||||
/// From which ATN state did we create this DFA?
|
/// From which ATN state did we create this DFA?
|
||||||
atn::DecisionState *const atnStartState;
|
atn::DecisionState *atnStartState;
|
||||||
std::unordered_set<DFAState *, DFAState::Hasher, DFAState::Comparer> states; // States are owned by this class.
|
std::unordered_set<DFAState *, DFAState::Hasher, DFAState::Comparer> states; // States are owned by this class.
|
||||||
DFAState *s0;
|
DFAState *s0;
|
||||||
const size_t decision;
|
size_t decision;
|
||||||
|
|
||||||
DFA(atn::DecisionState *atnStartState);
|
DFA(atn::DecisionState *atnStartState);
|
||||||
DFA(atn::DecisionState *atnStartState, size_t decision);
|
DFA(atn::DecisionState *atnStartState, size_t decision);
|
||||||
|
@ -85,7 +85,6 @@ namespace dfa {
|
||||||
* {@code false}. This is the backing field for {@link #isPrecedenceDfa}.
|
* {@code false}. This is the backing field for {@link #isPrecedenceDfa}.
|
||||||
*/
|
*/
|
||||||
bool _precedenceDfa;
|
bool _precedenceDfa;
|
||||||
DFAState *_s0Shadow = nullptr; // ml: assigned when we created s0 ourselves.
|
|
||||||
};
|
};
|
||||||
|
|
||||||
} // namespace atn
|
} // namespace atn
|
||||||
|
|
|
@ -1024,7 +1024,7 @@ ContextRuleListIndexedGetterDecl(r) ::= <<
|
||||||
|
|
||||||
>>
|
>>
|
||||||
|
|
||||||
LexerRuleContext() ::= "antlr4::RuleContext"
|
LexerRuleContext() ::= "RuleContext"
|
||||||
|
|
||||||
// The rule context name is the rule followed by a suffix; e.g. r becomes rContext.
|
// The rule context name is the rule followed by a suffix; e.g. r becomes rContext.
|
||||||
RuleContextNameSuffix() ::= "Context"
|
RuleContextNameSuffix() ::= "Context"
|
||||||
|
|
Loading…
Reference in New Issue