Skip to content

Improve performance of atPos under -Yrangepos#8858

Merged
retronym merged 1 commit intoscala:2.12.xfrom
retronym:topic/atPos
Apr 7, 2020
Merged

Improve performance of atPos under -Yrangepos#8858
retronym merged 1 commit intoscala:2.12.xfrom
retronym:topic/atPos

Conversation

@retronym
Copy link
Member

@retronym retronym commented Apr 6, 2020

Don't materialize lists of child nodes, and instead use
a new method, foreachChild to iterate through them.

Existing tests of range positions such as

test/files/run/dynamic-applyDynamic.scala

... give good coverage of the logic.

@scala-jenkins scala-jenkins added this to the 2.12.12 milestone Apr 6, 2020
@retronym retronym added the performance the need for speed. usually compiler performance, sometimes runtime performance. label Apr 6, 2020
@retronym
Copy link
Member Author

retronym commented Apr 6, 2020

In retronym/compiler-benchmark#2, I measured a 15% reduction in the allocations in repeatedly parsing a file full of case classes. This exercises the atPos code to position the subtrees of synthetic methods like unapply.

}
def apply(tree: Tree): Unit = {
wrappingPosAccumulator.reset()
if (!tree.isEmpty && tree.canHaveAttrs && tree.pos == NoPosition) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe test for UndefinedPosition instead of NoPosition?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I tried to keep the logic as it was before. UndefinedPosition also includes FakePos, that that appears only to be used to report errors through APIs that expect a position in contexts when we don't have a SourceFile. I wouldn't expect it to be attached to a tree.


/** Equivalent to `this.children.headOption.getOrElse(EmptyTree)`, but more efficient. */
final def onlyChild: Tree = {
onlyChildAccumulator.using(accum => { foreachChild(accum); accum.result()})
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be worth the effort to break out after seeing the second child?

Copy link
Member Author

@retronym retronym Apr 6, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, the callback to foreachChild can how return false to stop traversal.

@retronym retronym force-pushed the topic/atPos branch 3 times, most recently from 8db250c to 3ba534f Compare April 6, 2020 22:11
@retronym retronym merged commit 2bbb927 into scala:2.12.x Apr 7, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

performance the need for speed. usually compiler performance, sometimes runtime performance.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants