Skip to content

Commit b6ee1f6

Browse files
committed
Use a thread local WeakHashMap instead of synchronized
This should scale much better.
1 parent b8b0f87 commit b6ee1f6

File tree

1 file changed

+10
-3
lines changed

1 file changed

+10
-3
lines changed

src/main/scala/scala/util/parsing/input/OffsetPosition.scala

+10-3
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@ package scala
1010
package util.parsing.input
1111

1212
import scala.collection.mutable.ArrayBuffer
13+
import java.lang.{CharSequence, ThreadLocal}
14+
import java.util.WeakHashMap
1315

1416
/** `OffsetPosition` is a standard class for positions
1517
* represented as offsets into a source ``document''.
@@ -19,7 +21,7 @@ import scala.collection.mutable.ArrayBuffer
1921
*
2022
* @author Martin Odersky
2123
*/
22-
case class OffsetPosition(source: java.lang.CharSequence, offset: Int) extends Position {
24+
case class OffsetPosition(source: CharSequence, offset: Int) extends Position {
2325

2426
/** An index that contains all line starts, including first line, and eof. */
2527
private lazy val index: Array[Int] = {
@@ -87,6 +89,11 @@ case class OffsetPosition(source: java.lang.CharSequence, offset: Int) extends P
8789
* @author Tomáš Janoušek
8890
*/
8991
object OffsetPosition {
90-
private lazy val indexCache = java.util.Collections.synchronizedMap(
91-
new java.util.WeakHashMap[java.lang.CharSequence, Array[Int]])
92+
private lazy val indexCacheTL =
93+
// not DynamicVariable as that would share the map from parent to child :-(
94+
new ThreadLocal[java.util.Map[CharSequence, Array[Int]]] {
95+
override def initialValue = new WeakHashMap[CharSequence, Array[Int]]
96+
}
97+
98+
private def indexCache = indexCacheTL.get
9299
}

0 commit comments

Comments
 (0)