Merge pull request #1907 from mike-lischke/optimizations

[C++] Small optimizations
This commit is contained in:
Terence Parr 2017-06-11 08:54:21 -07:00 committed by GitHub
commit 35cf1ba4ff
4 changed files with 15 additions and 15 deletions

View File

@ -126,6 +126,8 @@ size_t ParserATNSimulator::adaptivePredict(TokenStream *input, size_t decision,
} else {
dfa::DFAState *newState = new dfa::DFAState(std::move(s0_closure)); /* mem-check: managed by the DFA or deleted below */
s0 = addDFAState(dfa, newState);
delete dfa.s0; // Delete existing s0 DFA state, if there's any.
dfa.s0 = s0;
if (s0 != newState) {
delete newState; // If there was already a state with this config set we don't need the new one.

View File

@ -26,35 +26,34 @@ DFA::DFA(atn::DecisionState *atnStartState, size_t decision)
if (static_cast<atn::StarLoopEntryState *>(atnStartState)->isPrecedenceDecision) {
_precedenceDfa = true;
s0 = new DFAState(std::unique_ptr<atn::ATNConfigSet>(new atn::ATNConfigSet()));
_s0Shadow = s0;
s0->isAcceptState = 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.
states = std::move(other.states);
// Manually move s0 pointers.
other.atnStartState = nullptr;
other.decision = 0;
s0 = other.s0;
other.s0 = nullptr;
_s0Shadow = other._s0Shadow;
other._s0Shadow = nullptr;
_precedenceDfa = other._precedenceDfa;
other._precedenceDfa = false;
}
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.
// 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;
bool s0InList = (s0 == nullptr);
for (auto state : states) {
if (state == s0)
s0InList = true;
delete state;
}
if (!s0InList)
delete s0;
}
bool DFA::isPrecedenceDfa() const {

View File

@ -20,10 +20,10 @@ namespace dfa {
/// Set only allows you to see if it's there.
/// 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.
DFAState *s0;
const size_t decision;
size_t decision;
DFA(atn::DecisionState *atnStartState);
DFA(atn::DecisionState *atnStartState, size_t decision);
@ -85,7 +85,6 @@ namespace dfa {
* {@code false}. This is the backing field for {@link #isPrecedenceDfa}.
*/
bool _precedenceDfa;
DFAState *_s0Shadow = nullptr; // ml: assigned when we created s0 ourselves.
};
} // namespace atn

View File

@ -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.
RuleContextNameSuffix() ::= "Context"