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

Improve PredictionContext performance, fix bugs #4196

Merged
merged 5 commits into from
Mar 28, 2023
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
115 changes: 0 additions & 115 deletions runtime/Go/antlr/v4/array_prediction_context.go

This file was deleted.

28 changes: 14 additions & 14 deletions runtime/Go/antlr/v4/atn_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ type ATNConfig interface {
GetSemanticContext() SemanticContext

// GetContext returns the rule invocation stack associated with this configuration
GetContext() PredictionContext
GetContext() *PredictionContext
// SetContext sets the rule invocation stack associated with this configuration
SetContext(PredictionContext)
SetContext(*PredictionContext)

// GetReachesIntoOuterContext returns the count of references to an outer context from this configuration
GetReachesIntoOuterContext() int
Expand All @@ -52,7 +52,7 @@ type BaseATNConfig struct {
precedenceFilterSuppressed bool
state ATNState
alt int
context PredictionContext
context *PredictionContext
semanticContext SemanticContext
reachesIntoOuterContext int
}
Expand All @@ -69,12 +69,12 @@ func NewBaseATNConfig7(old *BaseATNConfig) ATNConfig { // TODO: Dup - maybe dele
}

// NewBaseATNConfig6 creates a new BaseATNConfig instance given a state, alt and context only
func NewBaseATNConfig6(state ATNState, alt int, context PredictionContext) *BaseATNConfig {
func NewBaseATNConfig6(state ATNState, alt int, context *PredictionContext) *BaseATNConfig {
return NewBaseATNConfig5(state, alt, context, SemanticContextNone)
}

// NewBaseATNConfig5 creates a new BaseATNConfig instance given a state, alt, context and semantic context
func NewBaseATNConfig5(state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
func NewBaseATNConfig5(state ATNState, alt int, context *PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
if semanticContext == nil {
panic("semanticContext cannot be nil") // TODO: Necessary?
}
Expand All @@ -98,13 +98,13 @@ func NewBaseATNConfig2(c ATNConfig, semanticContext SemanticContext) *BaseATNCon
}

// NewBaseATNConfig1 creates a new BaseATNConfig instance given an existing config, a state, and a context only
func NewBaseATNConfig1(c ATNConfig, state ATNState, context PredictionContext) *BaseATNConfig {
func NewBaseATNConfig1(c ATNConfig, state ATNState, context *PredictionContext) *BaseATNConfig {
return NewBaseATNConfig(c, state, context, c.GetSemanticContext())
}

// NewBaseATNConfig creates a new BaseATNConfig instance given an existing config, a state, a context and a semantic context, other 'constructors'
// are just wrappers around this one.
func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
func NewBaseATNConfig(c ATNConfig, state ATNState, context *PredictionContext, semanticContext SemanticContext) *BaseATNConfig {
if semanticContext == nil {
panic("semanticContext cannot be nil") // TODO: Remove this - probably put here for some bug that is now fixed
}
Expand All @@ -115,7 +115,7 @@ func NewBaseATNConfig(c ATNConfig, state ATNState, context PredictionContext, se
return b
}

func (b *BaseATNConfig) InitBaseATNConfig(c ATNConfig, state ATNState, alt int, context PredictionContext, semanticContext SemanticContext) {
func (b *BaseATNConfig) InitBaseATNConfig(c ATNConfig, state ATNState, alt int, context *PredictionContext, semanticContext SemanticContext) {

b.state = state
b.alt = alt
Expand Down Expand Up @@ -144,12 +144,12 @@ func (b *BaseATNConfig) GetAlt() int {
}

// SetContext sets the rule invocation stack associated with this configuration
func (b *BaseATNConfig) SetContext(v PredictionContext) {
func (b *BaseATNConfig) SetContext(v *PredictionContext) {
b.context = v
}

// GetContext returns the rule invocation stack associated with this configuration
func (b *BaseATNConfig) GetContext() PredictionContext {
func (b *BaseATNConfig) GetContext() *PredictionContext {
return b.context
}

Expand Down Expand Up @@ -248,7 +248,7 @@ type LexerATNConfig struct {
passedThroughNonGreedyDecision bool
}

func NewLexerATNConfig6(state ATNState, alt int, context PredictionContext) *LexerATNConfig {
func NewLexerATNConfig6(state ATNState, alt int, context *PredictionContext) *LexerATNConfig {

return &LexerATNConfig{
BaseATNConfig: BaseATNConfig{
Expand All @@ -260,7 +260,7 @@ func NewLexerATNConfig6(state ATNState, alt int, context PredictionContext) *Lex
}
}

func NewLexerATNConfig5(state ATNState, alt int, context PredictionContext, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig {
func NewLexerATNConfig5(state ATNState, alt int, context *PredictionContext, lexerActionExecutor *LexerActionExecutor) *LexerATNConfig {
return &LexerATNConfig{
BaseATNConfig: BaseATNConfig{
state: state,
Expand Down Expand Up @@ -291,7 +291,7 @@ func NewLexerATNConfig3(c *LexerATNConfig, state ATNState, lexerActionExecutor *
return lac
}

func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context PredictionContext) *LexerATNConfig {
func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context *PredictionContext) *LexerATNConfig {
lac := &LexerATNConfig{
lexerActionExecutor: c.lexerActionExecutor,
passedThroughNonGreedyDecision: checkNonGreedyDecision(c, state),
Expand All @@ -301,7 +301,7 @@ func NewLexerATNConfig2(c *LexerATNConfig, state ATNState, context PredictionCon
}

//goland:noinspection GoUnusedExportedFunction
func NewLexerATNConfig1(state ATNState, alt int, context PredictionContext) *LexerATNConfig {
func NewLexerATNConfig1(state ATNState, alt int, context *PredictionContext) *LexerATNConfig {
lac := &LexerATNConfig{
BaseATNConfig: BaseATNConfig{
state: state,
Expand Down
5 changes: 3 additions & 2 deletions runtime/Go/antlr/v4/atn_simulator.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,12 +18,13 @@ type BaseATNSimulator struct {
decisionToDFA []*DFA
}

func (b *BaseATNSimulator) getCachedContext(context PredictionContext) PredictionContext {
func (b *BaseATNSimulator) getCachedContext(context *PredictionContext) *PredictionContext {
if b.sharedContextCache == nil {
return context
}

visited := NewJStore[PredictionContext, Comparator[PredictionContext]](pContextEqInst)
// TODO: Should this be guarded by a mutex?
visited := NewJStore[*PredictionContext, Comparator[*PredictionContext]](pContextEqInst)

return getCachedBasePredictionContext(context, b.sharedContextCache, visited)
}
Expand Down
6 changes: 3 additions & 3 deletions runtime/Go/antlr/v4/atn_state.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,9 +78,9 @@ type BaseATNState struct {
transitions []Transition
}

//func NewBaseATNState() *BaseATNState {
// return &BaseATNState{stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateInvalidType}
//}
func NewBaseATNState() *BaseATNState {
return &BaseATNState{stateNumber: ATNStateInvalidStateNumber, stateType: ATNStateInvalidType}
}

func (as *BaseATNState) GetRuleIndex() int {
return as.ruleIndex
Expand Down
45 changes: 0 additions & 45 deletions runtime/Go/antlr/v4/base_prediction_context.go

This file was deleted.

2 changes: 1 addition & 1 deletion runtime/Go/antlr/v4/char_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,5 +8,5 @@ type CharStream interface {
IntStream
GetText(int, int) string
GetTextFromTokens(start, end Token) string
GetTextFromInterval(*Interval) string
GetTextFromInterval(Interval) string
}
21 changes: 8 additions & 13 deletions runtime/Go/antlr/v4/common_token_stream.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type CommonTokenStream struct {
// fetch: The check to prevent adding multiple EOF symbols into tokens is
// trivial with bt field.
fetchedEOF bool

// index into [tokens] of the current token (next token to consume).
// tokens[p] should be LT(1). It is set to -1 when the stream is first
// constructed or when SetTokenSource is called, indicating that the first token
// has not yet been fetched from the token source. For additional information,
// see the documentation of [IntStream] for a description of initializing methods.
index int

// tokenSource is the [TokenSource] from which tokens for the bt stream are
// fetched.
tokenSource TokenSource
Expand Down Expand Up @@ -246,8 +246,8 @@ func (c *CommonTokenStream) GetHiddenTokensToRight(tokenIndex, channel int) []To

nextOnChannel := c.NextTokenOnChannel(tokenIndex+1, LexerDefaultTokenChannel)
from := tokenIndex + 1

// If no onChannel to the right, then nextOnChannel == -1, so set 'to' to the last token
// If no onChannel to the right, then nextOnChannel == -1, so set 'to' to the last token
var to int

if nextOnChannel == -1 {
Expand Down Expand Up @@ -317,7 +317,8 @@ func (c *CommonTokenStream) Index() int {
}

func (c *CommonTokenStream) GetAllText() string {
return c.GetTextFromInterval(nil)
c.Fill()
return c.GetTextFromInterval(NewInterval(0, len(c.tokens)-1))
}

func (c *CommonTokenStream) GetTextFromTokens(start, end Token) string {
Expand All @@ -332,15 +333,9 @@ func (c *CommonTokenStream) GetTextFromRuleContext(interval RuleContext) string
return c.GetTextFromInterval(interval.GetSourceInterval())
}

func (c *CommonTokenStream) GetTextFromInterval(interval *Interval) string {
func (c *CommonTokenStream) GetTextFromInterval(interval Interval) string {
c.lazyInit()

if interval == nil {
c.Fill()
interval = NewInterval(0, len(c.tokens)-1)
} else {
c.Sync(interval.Stop)
}
c.Sync(interval.Stop)

start := interval.Start
stop := interval.Stop
Expand Down
2 changes: 1 addition & 1 deletion runtime/Go/antlr/v4/comparators.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ var (
dfaStateEqInst = &ObjEqComparator[*DFAState]{}
semctxEqInst = &ObjEqComparator[SemanticContext]{}
atnAltCfgEqInst = &ATNAltConfigComparator[ATNConfig]{}
pContextEqInst = &ObjEqComparator[PredictionContext]{}
pContextEqInst = &ObjEqComparator[*PredictionContext]{}
)

// Equals2 delegates to the Equals() method of type T
Expand Down
Loading