Skip to content

Commit

Permalink
Merge pull request #10736 from lrytz/t12978
Browse files Browse the repository at this point in the history
Use lock-free fifo cache for `parsedClasses` in backend
  • Loading branch information
SethTisue committed Apr 8, 2024
2 parents 0bc6ad5 + 25b5b88 commit 5a6caca
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 37 deletions.
Expand Up @@ -56,7 +56,8 @@ abstract class ByteCodeRepository extends PerRunInit {
* Note - although this is typed a mutable.Map, individual simple get and put operations are threadsafe as the
* underlying data structure is synchronized.
*/
val parsedClasses: mutable.Map[InternalName, Either[ClassNotFound, ClassNode]] = recordPerRunCache(LruMap[InternalName, Either[ClassNotFound, ClassNode]](maxCacheSize, threadsafe = true))
val parsedClasses: mutable.Map[InternalName, Either[ClassNotFound, ClassNode]] =
recordPerRunCache(FifoCache[InternalName, Either[ClassNotFound, ClassNode]](maxCacheSize, threadsafe = true))

/**
* Contains the internal names of all classes that are defined in Java source files of the current
Expand Down
56 changes: 56 additions & 0 deletions src/compiler/scala/tools/nsc/backend/jvm/opt/FifoCache.scala
@@ -0,0 +1,56 @@
/*
* Scala (https://www.scala-lang.org)
*
* Copyright EPFL and Lightbend, Inc.
*
* Licensed under Apache License 2.0
* (http://www.apache.org/licenses/LICENSE-2.0).
*
* See the NOTICE file distributed with this work for
* additional information regarding copyright ownership.
*/

package scala.tools.nsc.backend.jvm.opt

import java.util.concurrent.{ConcurrentHashMap, ConcurrentLinkedQueue}
import java.util.{LinkedHashMap, Map => JMap}
import scala.collection.mutable
import scala.jdk.CollectionConverters._

object FifoCache {
def apply[K,V](maxSize: Int, threadsafe: Boolean): mutable.Map[K,V] = {
require(maxSize > 0)
if (threadsafe) new ConcFifoCache(maxSize) else new FifoCache[K, V](maxSize).asScala
}

private class FifoCache[K, V](maxSize: Int) extends LinkedHashMap[K,V] {
override def removeEldestEntry(eldest: JMap.Entry[K, V]): Boolean = {
size() > maxSize
}
}

private class ConcFifoCache[K, V](maxSize: Int) extends mutable.Map[K,V] {
private val cache: ConcurrentHashMap[K, V] = new ConcurrentHashMap()
private val queue: ConcurrentLinkedQueue[K] = new ConcurrentLinkedQueue()

def get(key: K): Option[V] = Option(cache.get(key))

def subtractOne(key: K): this.type = {
cache.remove(key)
queue.remove(key)
this
}

def addOne(elem: (K, V)): this.type = {
while (cache.size() >= maxSize) {
val oldest = queue.poll()
if (oldest != null) cache.remove(oldest)
}
queue.add(elem._1)
cache.put(elem._1, elem._2)
this
}

def iterator: Iterator[(K, V)] = cache.entrySet.iterator.asScala.map(e => (e.getKey, e.getValue))
}
}
33 changes: 0 additions & 33 deletions src/compiler/scala/tools/nsc/backend/jvm/opt/LruMap.scala

This file was deleted.

2 changes: 1 addition & 1 deletion test/files/neg/case-collision-multifile/one.scala
@@ -1,2 +1,2 @@
// scalac: -Werror
//> using options -Werror -Ybackend-parallelism 1
class HotDog
Expand Up @@ -12,6 +12,7 @@ import scala.reflect.internal.util.JavaClearable
import scala.tools.asm.tree._
import scala.tools.nsc.backend.jvm.BackendReporting._
import scala.tools.nsc.reporters.StoreReporter
import scala.tools.testkit.ASMConverters.convertMethod
import scala.tools.testkit.BytecodeTesting
import scala.tools.testkit.BytecodeTesting._

Expand Down Expand Up @@ -42,9 +43,9 @@ class CallGraphTest extends BytecodeTesting {
val callsite = callGraph.callsites(callsiteMethod)(call)
try {
assert(callsite.callsiteInstruction == call)
assert(callsite.callsiteMethod == callsiteMethod)
assert(convertMethod(callsite.callsiteMethod) == convertMethod(callsiteMethod))
val callee = callsite.callee.get
assert(callee.callee == target)
assert(convertMethod(callee.callee) == convertMethod(target))
assert(callee.calleeDeclarationClass == calleeDeclClass)
assertEquals("safeToInline", safeToInline, callee.safeToInline)
assert(callee.annotatedInline == atInline)
Expand Down

0 comments on commit 5a6caca

Please sign in to comment.