All Downloads are FREE. Search and download functionalities are using the official Maven repository.

dotty.tools.dotc.util.ReusableInstance.scala Maven / Gradle / Ivy

There is a newer version: 3.6.4-RC1-bin-20241220-0bfa1af-NIGHTLY
Show newest version
package dotty.tools.dotc.util

import scala.collection.mutable.ArrayBuffer
import scala.util.chaining.*

/** A wrapper for a list of cached instances of a type `T`.
  * The wrapper is recursion-reentrant: several instances are kept, so
  * at each depth of reentrance we are reusing the instance for that.
  *
  * An instance is created upon creating this object, and more instances
  * are allocated dynamically, on demand, when reentrance occurs.
  *
  * Not thread safe.
  *
  * Ported from scala.reflect.internal.util.ReusableInstance
  */
final class ReusableInstance[T <: AnyRef] private (make: => T) {
  private val cache = new ArrayBuffer[T](ReusableInstance.InitialSize).tap(_.addOne(make))
  private var taken = 0

  inline def withInstance[R](action: T => R): R ={
    if (taken == cache.size)
      cache += make
    taken += 1
    try action(cache(taken-1)) finally taken -= 1
  }
}

object ReusableInstance {
  private inline val InitialSize = 4

  def apply[T <: AnyRef](make: => T): ReusableInstance[T] = new ReusableInstance[T](make)
}




© 2015 - 2025 Weber Informatics LLC | Privacy Policy