Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[C++] Small optimizations #1907

Merged
merged 4 commits into from
Jun 11, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions runtime/Cpp/runtime/src/atn/ParserATNSimulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,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.
Expand Down
21 changes: 10 additions & 11 deletions runtime/Cpp/runtime/src/dfa/DFA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
5 changes: 2 additions & 3 deletions runtime/Cpp/runtime/src/dfa/DFA.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down