Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #10736 from lrytz/t12978
Use lock-free fifo cache for `parsedClasses` in backend
- Loading branch information
Showing
5 changed files
with
62 additions
and
37 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
56 changes: 56 additions & 0 deletions
56
src/compiler/scala/tools/nsc/backend/jvm/opt/FifoCache.scala
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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)) | ||
} | ||
} |
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,2 @@ | ||
// scalac: -Werror | ||
//> using options -Werror -Ybackend-parallelism 1 | ||
class HotDog |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters