File tree Expand file tree Collapse file tree 1 file changed +37
-0
lines changed
compiler/src/dotty/tools/dotc/util Expand file tree Collapse file tree 1 file changed +37
-0
lines changed Original file line number Diff line number Diff line change
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
+ }
You can’t perform that action at this time.
0 commit comments