Skip to content

Commit 748d43b

Browse files
committed
Address review: rename using to withInstance
1 parent 42a5dd4 commit 748d43b

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

compiler/src/dotty/tools/dotc/core/classfile/ClassfileParser.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -81,7 +81,7 @@ class ClassfileParser(
8181
private def mismatchError(className: SimpleName) =
8282
throw new IOException(s"class file '${classfile.canonicalPath}' has location not matching its contents: contains class $className")
8383

84-
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.using { reader =>
84+
def run()(using Context): Option[Embedded] = try ctx.base.reusableDataReader.withInstance { reader =>
8585
implicit val reader2 = reader.reset(classfile)
8686
report.debuglog("[class] >> " + classRoot.fullName)
8787
parseHeader()

compiler/src/dotty/tools/dotc/util/ReusableInstance.scala

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -14,24 +14,20 @@ import scala.util.chaining._
1414
*
1515
* Ported from scala.reflect.internal.util.ReusableInstance
1616
*/
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
17+
final class ReusableInstance[T <: AnyRef] private (make: => T) {
18+
private[this] val cache = new ArrayBuffer[T](ReusableInstance.InitialSize).tap(_.addOne(make))
1919
private[this] var taken = 0
2020

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-
}
21+
inline def withInstance[R](action: T => R): R ={
22+
if (taken == cache.size)
23+
cache += make
24+
taken += 1
25+
try action(cache(taken-1)) finally taken -= 1
26+
}
3027
}
3128

3229
object ReusableInstance {
3330
private final val InitialSize = 4
3431

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)
32+
def apply[T <: AnyRef](make: => T): ReusableInstance[T] = new ReusableInstance[T](make)
3733
}

0 commit comments

Comments
 (0)