Skip to content

Commit

Permalink
[C++] Devirtualize hand rolled RTTI for performance
Browse files Browse the repository at this point in the history
  • Loading branch information
jcking committed Mar 30, 2022
1 parent de8b934 commit 9aaf0c7
Show file tree
Hide file tree
Showing 89 changed files with 165 additions and 445 deletions.
8 changes: 2 additions & 6 deletions runtime/Cpp/runtime/src/RuleContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,11 @@ using namespace antlr4;
using namespace antlr4::atn;
using namespace antlr4::tree;

RuleContext::RuleContext() {
RuleContext::RuleContext() : ParseTree(ParseTreeType::RULE) {
InitializeInstanceFields();
}

RuleContext::RuleContext(RuleContext *parent_, size_t invokingState_) {
RuleContext::RuleContext(RuleContext *parent_, size_t invokingState_) : ParseTree(ParseTreeType::RULE) {
InitializeInstanceFields();
this->parent = parent_;
this->invokingState = invokingState_;
Expand Down Expand Up @@ -138,10 +138,6 @@ std::string RuleContext::toString(Recognizer *recog, RuleContext *stop) {
return toString(recog->getRuleNames(), stop);
}

ParseTreeType RuleContext::getTreeType() const {
return ParseTreeType::RULE;
}

void RuleContext::InitializeInstanceFields() {
invokingState = INVALID_INDEX;
}
Expand Down
2 changes: 0 additions & 2 deletions runtime/Cpp/runtime/src/RuleContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,6 @@ namespace antlr4 {

virtual std::string toString(const std::vector<std::string> &ruleNames, RuleContext *stop);

tree::ParseTreeType getTreeType() const final;

bool operator == (const RuleContext &other) { return this == &other; } // Simple address comparison.

private:
Expand Down
10 changes: 8 additions & 2 deletions runtime/Cpp/runtime/src/atn/ATNState.h
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ namespace atn {
/// Track the transitions emanating from this ATN state.
std::vector<ConstTransitionPtr> transitions;

ATNState() = default;
ATNState() = delete;

ATNState(ATNState const&) = delete;

Expand All @@ -114,14 +114,20 @@ namespace atn {

virtual bool isNonGreedyExitState() const;
virtual std::string toString() const;
virtual ATNStateType getStateType() const = 0;

ATNStateType getStateType() const { return _stateType; }

protected:
explicit ATNState(ATNStateType stateType) : _stateType(stateType) {}

private:
/// Used to cache lookahead during parsing, not used during construction.

misc::IntervalSet _nextTokenWithinRule;
std::atomic<bool> _nextTokenUpdated { false };

ATNStateType _stateType;

friend class ATN;
};

Expand Down
8 changes: 2 additions & 6 deletions runtime/Cpp/runtime/src/atn/ActionTransition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,11 @@
using namespace antlr4::atn;

ActionTransition::ActionTransition(ATNState *target, size_t ruleIndex)
: Transition(target), ruleIndex(ruleIndex), actionIndex(INVALID_INDEX), isCtxDependent(false) {
: Transition(TransitionType::ACTION, target), ruleIndex(ruleIndex), actionIndex(INVALID_INDEX), isCtxDependent(false) {
}

ActionTransition::ActionTransition(ATNState *target, size_t ruleIndex, size_t actionIndex, bool isCtxDependent)
: Transition(target), ruleIndex(ruleIndex), actionIndex(actionIndex), isCtxDependent(isCtxDependent) {
}

TransitionType ActionTransition::getTransitionType() const {
return TransitionType::ACTION;
: Transition(TransitionType::ACTION, target), ruleIndex(ruleIndex), actionIndex(actionIndex), isCtxDependent(isCtxDependent) {
}

bool ActionTransition::isEpsilon() const {
Expand Down
2 changes: 0 additions & 2 deletions runtime/Cpp/runtime/src/atn/ActionTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,6 @@ namespace atn {

ActionTransition(ATNState *target, size_t ruleIndex, size_t actionIndex, bool isCtxDependent);

TransitionType getTransitionType() const override;

virtual bool isEpsilon() const override;

virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override;
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/ArrayPredictionContext.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,15 +16,11 @@ ArrayPredictionContext::ArrayPredictionContext(Ref<const SingletonPredictionCont

ArrayPredictionContext::ArrayPredictionContext(std::vector<Ref<const PredictionContext>> parents,
std::vector<size_t> returnStates)
: PredictionContext(calculateHashCode(parents, returnStates)), parents(std::move(parents)), returnStates(std::move(returnStates)) {
: PredictionContext(PredictionContextType::ARRAY, calculateHashCode(parents, returnStates)), parents(std::move(parents)), returnStates(std::move(returnStates)) {
assert(this->parents.size() > 0);
assert(this->returnStates.size() > 0);
}

PredictionContextType ArrayPredictionContext::getContextType() const {
return PredictionContextType::ARRAY;
}

bool ArrayPredictionContext::isEmpty() const {
// Since EMPTY_RETURN_STATE can only appear in the last position, we don't need to verify that size == 1.
return returnStates[0] == EMPTY_RETURN_STATE;
Expand Down
2 changes: 0 additions & 2 deletions runtime/Cpp/runtime/src/atn/ArrayPredictionContext.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,6 @@ namespace atn {

ArrayPredictionContext(std::vector<Ref<const PredictionContext>> parents, std::vector<size_t> returnStates);

PredictionContextType getContextType() const override;

virtual bool isEmpty() const override;
virtual size_t size() const override;
virtual Ref<const PredictionContext> getParent(size_t index) const override;
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/AtomTransition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,7 @@
using namespace antlr4::misc;
using namespace antlr4::atn;

AtomTransition::AtomTransition(ATNState *target, size_t label) : Transition(target), _label(label) {
}

TransitionType AtomTransition::getTransitionType() const {
return TransitionType::ATOM;
AtomTransition::AtomTransition(ATNState *target, size_t label) : Transition(TransitionType::ATOM, target), _label(label) {
}

IntervalSet AtomTransition::label() const {
Expand Down
2 changes: 0 additions & 2 deletions runtime/Cpp/runtime/src/atn/AtomTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,8 +19,6 @@ namespace atn {

AtomTransition(ATNState *target, size_t label);

TransitionType getTransitionType() const override;

virtual misc::IntervalSet label() const override;
virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override;

Expand Down
12 changes: 0 additions & 12 deletions runtime/Cpp/runtime/src/atn/BasicBlockStartState.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions runtime/Cpp/runtime/src/atn/BasicBlockStartState.h
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,7 @@ namespace atn {

class ANTLR4CPP_PUBLIC BasicBlockStartState final : public BlockStartState {
public:
virtual ATNStateType getStateType() const override;

BasicBlockStartState() : BlockStartState(ATNStateType::BLOCK_START) {}
};

} // namespace atn
Expand Down
12 changes: 0 additions & 12 deletions runtime/Cpp/runtime/src/atn/BasicState.cpp

This file was deleted.

3 changes: 1 addition & 2 deletions runtime/Cpp/runtime/src/atn/BasicState.h
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,7 @@ namespace atn {

class ANTLR4CPP_PUBLIC BasicState final : public ATNState {
public:
virtual ATNStateType getStateType() const override;

BasicState() : ATNState(ATNStateType::BASIC) {}
};

} // namespace atn
Expand Down
12 changes: 0 additions & 12 deletions runtime/Cpp/runtime/src/atn/BlockEndState.cpp

This file was deleted.

2 changes: 1 addition & 1 deletion runtime/Cpp/runtime/src/atn/BlockEndState.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ namespace atn {
public:
BlockStartState *startState = nullptr;

virtual ATNStateType getStateType() const override;
BlockEndState() : ATNState(ATNStateType::BLOCK_END) {}
};

} // namespace atn
Expand Down
6 changes: 0 additions & 6 deletions runtime/Cpp/runtime/src/atn/BlockStartState.cpp

This file was deleted.

3 changes: 3 additions & 0 deletions runtime/Cpp/runtime/src/atn/BlockStartState.h
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,9 @@ namespace atn {
class ANTLR4CPP_PUBLIC BlockStartState : public DecisionState {
public:
BlockEndState *endState = nullptr;

protected:
using DecisionState::DecisionState;
};

} // namespace atn
Expand Down
5 changes: 3 additions & 2 deletions runtime/Cpp/runtime/src/atn/DecisionState.h
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,10 @@ namespace atn {
int decision = -1;
bool nonGreedy = false;

DecisionState() = default;

virtual std::string toString() const override;

protected:
using ATNState::ATNState;
};

} // namespace atn
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/EpsilonTransition.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@ EpsilonTransition::EpsilonTransition(ATNState *target) : EpsilonTransition(targe
}

EpsilonTransition::EpsilonTransition(ATNState *target, size_t outermostPrecedenceReturn)
: Transition(target), _outermostPrecedenceReturn(outermostPrecedenceReturn) {
: Transition(TransitionType::EPSILON, target), _outermostPrecedenceReturn(outermostPrecedenceReturn) {
}

size_t EpsilonTransition::outermostPrecedenceReturn() const {
return _outermostPrecedenceReturn;
}

TransitionType EpsilonTransition::getTransitionType() const {
return TransitionType::EPSILON;
}

bool EpsilonTransition::isEpsilon() const {
return true;
}
Expand Down
1 change: 0 additions & 1 deletion runtime/Cpp/runtime/src/atn/EpsilonTransition.h
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ namespace atn {
* @since 4.4.1
*/
size_t outermostPrecedenceReturn() const;
TransitionType getTransitionType() const override;

virtual bool isEpsilon() const override;
virtual bool matches(size_t symbol, size_t minVocabSymbol, size_t maxVocabSymbol) const override;
Expand Down
8 changes: 7 additions & 1 deletion runtime/Cpp/runtime/src/atn/LexerAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ namespace atn {
/// Gets the serialization type of the lexer action.
/// </summary>
/// <returns> The serialization type of the lexer action. </returns>
virtual LexerActionType getActionType() const = 0;
LexerActionType getActionType() const { return _actionType; }

/// <summary>
/// Gets whether the lexer action is position-dependent. Position-dependent
Expand Down Expand Up @@ -60,6 +60,12 @@ namespace atn {
}

virtual std::string toString() const = 0;

protected:
explicit LexerAction(LexerActionType actionType) : _actionType(actionType) {}

private:
const LexerActionType _actionType;
};

} // namespace atn
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/LexerChannelAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,13 @@
using namespace antlr4::atn;
using namespace antlr4::misc;

LexerChannelAction::LexerChannelAction(int channel) : _channel(channel) {
LexerChannelAction::LexerChannelAction(int channel) : LexerAction(LexerActionType::CHANNEL), _channel(channel) {
}

int LexerChannelAction::getChannel() const {
return _channel;
}

LexerActionType LexerChannelAction::getActionType() const {
return LexerActionType::CHANNEL;
}

bool LexerChannelAction::isPositionDependent() const {
return false;
}
Expand Down
5 changes: 0 additions & 5 deletions runtime/Cpp/runtime/src/atn/LexerChannelAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,11 +33,6 @@ namespace atn {
/// <returns> The channel to use for the <seealso cref="Token"/> created by the lexer. </returns>
int getChannel() const;

/// <summary>
/// {@inheritDoc} </summary>
/// <returns> This method returns <seealso cref="LexerActionType#CHANNEL"/>. </returns>
virtual LexerActionType getActionType() const override;

/// <summary>
/// {@inheritDoc} </summary>
/// <returns> This method returns {@code false}. </returns>
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/LexerCustomAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ using namespace antlr4;
using namespace antlr4::atn;
using namespace antlr4::misc;

LexerCustomAction::LexerCustomAction(size_t ruleIndex, size_t actionIndex) : _ruleIndex(ruleIndex), _actionIndex(actionIndex) {
LexerCustomAction::LexerCustomAction(size_t ruleIndex, size_t actionIndex) : LexerAction(LexerActionType::CUSTOM), _ruleIndex(ruleIndex), _actionIndex(actionIndex) {
}

size_t LexerCustomAction::getRuleIndex() const {
Expand All @@ -24,10 +24,6 @@ size_t LexerCustomAction::getActionIndex() const {
return _actionIndex;
}

LexerActionType LexerCustomAction::getActionType() const {
return LexerActionType::CUSTOM;
}

bool LexerCustomAction::isPositionDependent() const {
return true;
}
Expand Down
6 changes: 0 additions & 6 deletions runtime/Cpp/runtime/src/atn/LexerCustomAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,12 +48,6 @@ namespace atn {
/// <returns> The action index for the custom action. </returns>
size_t getActionIndex() const;

/// <summary>
/// {@inheritDoc}
/// </summary>
/// <returns> This method returns <seealso cref="LexerActionType#CUSTOM"/>. </returns>
virtual LexerActionType getActionType() const override;

/// <summary>
/// Gets whether the lexer action is position-dependent. Position-dependent
/// actions may have different semantics depending on the <seealso cref="CharStream"/>
Expand Down
6 changes: 1 addition & 5 deletions runtime/Cpp/runtime/src/atn/LexerIndexedCustomAction.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ using namespace antlr4::atn;
using namespace antlr4::misc;

LexerIndexedCustomAction::LexerIndexedCustomAction(int offset, Ref<LexerAction> action)
: _offset(offset), _action(std::move(action)) {
: LexerAction(action->getActionType()), _offset(offset), _action(std::move(action)) {
}

int LexerIndexedCustomAction::getOffset() const {
Expand All @@ -25,10 +25,6 @@ Ref<LexerAction> LexerIndexedCustomAction::getAction() const {
return _action;
}

LexerActionType LexerIndexedCustomAction::getActionType() const {
return _action->getActionType();
}

bool LexerIndexedCustomAction::isPositionDependent() const {
return true;
}
Expand Down
7 changes: 0 additions & 7 deletions runtime/Cpp/runtime/src/atn/LexerIndexedCustomAction.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,13 +55,6 @@ namespace atn {
/// <returns> A <seealso cref="LexerAction"/> object which executes the lexer action. </returns>
Ref<LexerAction> getAction() const;

/// <summary>
/// {@inheritDoc}
/// </summary>
/// <returns> This method returns the result of calling <seealso cref="#getActionType"/>
/// on the <seealso cref="LexerAction"/> returned by <seealso cref="#getAction"/>. </returns>
virtual LexerActionType getActionType() const override;

/// <summary>
/// {@inheritDoc} </summary>
/// <returns> This method returns {@code true}. </returns>
Expand Down
Loading

0 comments on commit 9aaf0c7

Please sign in to comment.