Skip to content

Commit 14383ed

Browse files
committed
Add ReusableInstance
1 parent aefc6b0 commit 14383ed

File tree

1 file changed

+37
-0
lines changed

1 file changed

+37
-0
lines changed
Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
package dotty.tools.dotc.util
2+
3+
import scala.collection.mutable.ArrayBuffer
4+
import scala.util.chaining._
5+
6+
/** A wrapper for a list of cached instances of a type `T`.
7+
* The wrapper is recursion-reentrant: several instances are kept, so
8+
* at each depth of reentrance we are reusing the instance for that.
9+
*
10+
* An instance is created upon creating this object, and more instances
11+
* are allocated dynamically, on demand, when reentrance occurs.
12+
*
13+
* Not thread safe.
14+
*
15+
* Ported from scala.reflect.internal.util.ReusableInstance
16+
*/
17+
final class ReusableInstance[T <: AnyRef] private (make: => T, enabled: Boolean) {
18+
private[this] val cache = if (enabled) new ArrayBuffer[T](ReusableInstance.InitialSize).tap(_.addOne(make)) else null
19+
private[this] var taken = 0
20+
21+
@inline def using[R](action: T => R): R =
22+
if (!enabled)
23+
action(make)
24+
else {
25+
if (taken == cache.size)
26+
cache += make
27+
taken += 1
28+
try action(cache(taken-1)) finally taken -= 1
29+
}
30+
}
31+
32+
object ReusableInstance {
33+
private final val InitialSize = 4
34+
35+
def apply[T <: AnyRef](make: => T): ReusableInstance[T] = new ReusableInstance[T](make, enabled = true)
36+
def apply[T <: AnyRef](make: => T, enabled: Boolean): ReusableInstance[T] = new ReusableInstance[T](make, enabled = enabled)
37+
}

0 commit comments

Comments
 (0)