8000 Merge pull request #4974 from gzm0/chm · scala-js/scala-js@95c38be · GitHub
[go: up one dir, main page]

Skip to content

Commit 95c38be

Browse files
authored
Merge pull request #4974 from gzm0/chm
Replace (Par)TrieMap with ConcurrentHashMap in IncOptimizer
2 parents b5158b2 + d3c42e1 commit 95c38be

File tree

6 files changed

+196
-171
lines changed

6 files changed

+196
-171
lines changed

javalib/src/main/scala/java/util/concurrent/ConcurrentHashMap.scala

Lines changed: 18 additions & 0 deletions
< 8000 td data-grid-cell-id="diff-7230f078865e6300d11c1eb84d62ea99502294e84267007b8b7e1dc17d9760e8-74-87-1" data-selected="false" role="gridcell" style="background-color:var(--diffBlob-additionNum-bgColor, var(--diffBlob-addition-bgColor-num));text-align:center" tabindex="-1" valign="top" class="focusable-grid-cell diff-line-number position-relative left-side">87
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212

1313
package java.util.concurrent
1414

15+
import java.util.function.{BiConsumer, Consumer}
16+
1517
import java.io.Serializable
1618
import java.util._
1719

@@ -72,6 +74,22 @@ class ConcurrentHashMap[K, V] private (initialCapacity: Int, loadFactor: Float)
7274
new ConcurrentHashMap.KeySetView[K, V](this.inner, mappedValue)
7375
}
7476

77+
def forEach(parallelismThreshold: Long, action: BiConsumer[_ >: K, _ >: V]): Unit = {
78+
// Note: It is tempting to simply call inner.forEach here:
79+
// However, this will not have the correct snapshotting behavior.
80+
val i = inner.nodeIterator()
81+
while (i.hasNext()) {
82+
val n = i.next()
83+
action.accept(n.key, n.value)
84+
}
85+
}
86+
+
def forEachKey(parallelismThreshold: Long, action: Consumer[_ >: K]): Unit =
88+
inner.keyIterator().forEachRemaining(action)
89+
90+
def forEachValue(parallelismThreshold: Long, action: Consumer[_ >: V]): Unit =
91+
inner.valueIterator().forEachRemaining(action)
92+
7593
override def values(): Collection[V] =
7694
inner.values()
7795

linker/jvm/src/main/scala/org/scalajs/linker/frontend/optimizer/ParCollOps.scala

Lines changed: 3 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -14,52 +14,20 @@ package org.scalajs.linker.frontend.optimizer
1414

1515
import scala.annotation.tailrec
1616

17-
import scala.collection.concurrent.TrieMap
18-
import scala.collection.parallel.mutable.{ParTrieMap, ParArray}
17+
import scala.collection.parallel.mutable.ParArray
1918
import scala.collection.parallel._
2019

2120
import java.util.concurrent.atomic._
2221

2322
private[optimizer] object ParCollOps extends AbsCollOps {
24-
type Map[K, V] = TrieMap[K, V]
25-
type ParMap[K, V] = ParTrieMap[K, V]
26-
type AccMap[K, V] = TrieMap[K, Addable[V]]
2723
type ParIterable[V] = ParArray[V]
2824
type Addable[V] = AtomicReference[List[V]]
2925

30-
def emptyAccMap[K, V]: AccMap[K, V] = TrieMap.empty
31-
def emptyMap[K, V]: Map[K, V] = TrieMap.empty
32-
def emptyParMap[K, V]: ParMap[K, V] = ParTrieMap.empty
26+
def parThreshold: Long = 1 // max parallelism
27+
3328
def emptyParIterable[V]: ParIterable[V] = ParArray.empty
3429
def emptyAddable[V]: Addable[V] = new AtomicReference[List[V]](Nil)
3530

36-
// Operations on ParMap
37-
def isEmpty[K, V](map: ParMap[K, V]): Boolean = map.isEmpty
38-
def forceGet[K, V](map: ParMap[K, V], k: K): V = map(k)
39-
def get[K, V](map: ParMap[K, V], k: K): Option[V] = map.get(k)
40-
def put[K, V](map: ParMap[K, V], k: K, v: V): Unit = map.put(k, v)
41-
def remove[K, V](map: ParMap[K, V], k: K): Option[V] = map.remove(k)
42-
43-
def retain[K, V](map: ParMap[K, V])(p: (K, V) => Boolean): Unit = {
44-
map.foreach { case (k, v) =>
45-
if (!p(k, v))
46-
map.remove(k)
47-
}
48-
}
49-
50-
def valuesForeach[K, V, U](map: ParMap[K, V])(f: V => U): Unit =
51-
map.values.foreach(f)
52-
53-
// Operations on AccMap
54-
def acc[K, V](map: AccMap[K, V], k: K, v: V): Unit =
55-
add(map.getOrElseUpdate(k, emptyAddable), v)
56-
57-
def getAcc[K, V](map: AccMap[K, V], k: K): ParIterable[V] =
58-
map.get(k).fold(emptyParIterable[V])(finishAdd(_))
59-
60-
def parFlatMapKeys[A, B](map< 6D40 /span>: AccMap[A, _])(f: A => Option[B]): ParIterable[B] =
61-
map.keys.flatMap(f(_)).toParArray
62-
6331
// Operations on ParIterable
6432
def prepAdd[V](it: ParIterable[V]): Addable[V] =
6533
new AtomicReference(it.toList)

linker/shared/src/main/scala/org/scalajs/linker/frontend/optimizer/AbsCollOps.scala

Lines changed: 2 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,15 @@ package org.scalajs.linker.frontend.optimizer
1414

1515
import language.higherKinds
1616

17-
import scala.collection.mutable
18-
1917
private[optimizer] trait AbsCollOps {
20-
type Map[K, V] <: mutable.Map[K, V]
21-
type ParMap[K, V] <: AnyRef
9E88
22-
type AccMap[K, V] <: AnyRef
2318
type ParIterable[V] <: AnyRef
2419
type Addable[V] <: AnyRef
2520

26-
def emptyAccMap[K, V]: AccMap[K, V]
27-
def emptyMap[K, V]: Map[K, V]
28-
def emptyParMap[K, V]: ParMap[K, V]
21+
def parThreshold: Long
22+
2923
def emptyParIterable[V]: ParIterable[V]
3024
def emptyAddable[V]: Addable[V]
3125

32-
// Operations on ParMap
33-
def isEmpty[K, V](map: ParMap[K, V]): Boolean
34-
def forceGet[K, V](map: ParMap[K, V], k: K): V
35-
def get[K, V](map: ParMap[K, V], k: K): Option[V]
36-
def put[K, V](map: ParMap[K, V], k: K, v: V): Unit
37-
def remove[K, V](map: ParMap[K, V], k: K): Option[V]
38-
def retain[K, V](map: ParMap[K, V])(p: (K, V) => Boolean): Unit
39-
def valuesForeach[K, V, U](map: ParMap[K, V])(f: V => U): Unit
40-
41-
// Operations on AccMap
42-
def acc[K, V](map: AccMap[K, V], k: K, v: V): Unit
43-
def getAcc[K, V](map: AccMap[K, V], k: K): ParIterable[V]
44-
def parFlatMapKeys[A, B](map: AccMap[A, _])(f: A => Option[B]): ParIterable[B]
45-
4626
// Operations on ParIterable
4727
def prepAdd[V](it: ParIterable[V]): Addable[V]
4828
def add[V](addable: Addable[V], v: V): Unit

0 commit comments

Comments
 (0)
0