Skip to content

Commit 65d2790

Browse files
authored
Speed-up and cache conversion from offset to Kiama's position (#1021)
This makes the parser about 25-50% [!!!] quicker: (before vs after the first commit) <img width="1105" alt="Screenshot 2025-05-24 at 23 12 19" src="https://github.com/user-attachments/assets/57cb34e5-647d-46ad-826b-3d8c0dc990c9" /> Although we'll replace Kiama positions in the future, I think this still makes sense as it's a tiny fix that noticeably improves the performance... Related to #744. Everything works in the editor as of the second commit :)
1 parent b1570df commit 65d2790

File tree

2 files changed

+9
-3
lines changed

2 files changed

+9
-3
lines changed

effekt/shared/src/main/scala/effekt/RecursiveDescent.scala

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1521,6 +1521,12 @@ class RecursiveDescent(positions: Positions, tokens: Seq[Token], source: Source)
15211521
Span(source, start, end)
15221522
}
15231523

1524+
/* Hack: Resolving Kiama positions takes a lot of time, let's cache `offset` ~> `kiama.Position`.
1525+
* This makes the parser about 30% faster. */
1526+
private val positionCache = scala.collection.mutable.HashMap[Int, Position]()
1527+
private def getPosition(offset: Int): Position =
1528+
positionCache.getOrElseUpdate(offset, source.offsetToPosition(offset))
1529+
15241530
// the handler for the "span" effect.
15251531
private val _start: scala.util.DynamicVariable[Int] = scala.util.DynamicVariable(0)
15261532
inline def nonterminal[T](inline p: => T): T = _start.withValue(peek.start) {
@@ -1544,8 +1550,8 @@ class RecursiveDescent(positions: Positions, tokens: Seq[Token], source: Source)
15441550
// case _ => ()
15451551
// }
15461552

1547-
val startPos = source.offsetToPosition(start)
1548-
val endPos = source.offsetToPosition(end)
1553+
val startPos = getPosition(start)
1554+
val endPos = getPosition(end)
15491555

15501556
// recursively add positions to subtrees that are not yet annotated
15511557
// this is better than nothing and means we have positions for desugared stuff

kiama

0 commit comments

Comments
 (0)