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

Use of reflect.TypeOf slows down Go execution by 33% #2278

Closed
davesisson opened this issue Apr 20, 2018 · 7 comments · Fixed by #3353
Closed

Use of reflect.TypeOf slows down Go execution by 33% #2278

davesisson opened this issue Apr 20, 2018 · 7 comments · Fixed by #3353

Comments

@davesisson
Copy link
Contributor

A coworker of mine found that replacing if reflect.TypeOf with ctx.(IParameterListContext) in the generated grammar improved the performance noticeably. I suggest we remove the use of reflect.TypeOf as much as possible in the Go implementation.

@davesisson
Copy link
Contributor Author

Sample Before:

  var t = s.GetTypedRuleContext(
    reflect.TypeOf((*IMyTypeContext)(nil)).Elem(), i)

Sample After:

  var t antlr.RuleContext; j := 0
  for _, ctx := range s.GetChildren() {
    if _, ok := ctx.(IMyTypeContext); ok {
      if j==i { t = ctx.(antlr.RuleContext); break } else { j++ } 
    }
  }

@wdscxsj
Copy link

wdscxsj commented Apr 24, 2018

@millergarym @pboyer Care to take a look? Thanks.

@pboyer
Copy link
Contributor

pboyer commented Apr 24, 2018 via email

@millergarym
Copy link
Contributor

Cool.

@pboyer I setup up a regression test environment when last doing performance work. It might be of interest. see https://github.com/wxio/antlr4-jenkins/

@davesisson any chance of getting an example grammar and input. Benchmarking can be quite context dependant. Also, imo its nicer to have a really problem that someone cares about than a substituent.

@davesisson
Copy link
Contributor Author

davesisson commented Apr 25, 2018 via email

@wdscxsj
Copy link

wdscxsj commented Jun 19, 2018

Any news, please? In my case, removing reflection from the parser does make it faster, but not by much (25.60s -> 24.59s, internal project). Still noticeably slower than the Java version.

I modified antlr.jar/org/antlr/v4/tool/templates/codegen/Go/Go.stg according to the code samples by @davesisson. Should I make a PR or wait for something good to happen? Of course, all credits to @davesisson and his coworker.

@KvanTTT
Copy link
Member

KvanTTT commented Nov 6, 2021

but not by much (25.60s -> 24.59s, internal project)

It's quite a good improvement (4%).

Should I make a PR or wait for something good to happen?

Yes, please do it if you can. In upcoming 4.9.3 we also have other Go runtime performance improvements: #3243

KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Nov 6, 2021
KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Nov 6, 2021
KvanTTT added a commit to KvanTTT/antlr4 that referenced this issue Nov 14, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

5 participants