Skip to content

Commit 4a8810b

Browse files
committed
Merge branch 'perf-backport/6967-needless-storage-scope' into perf-backport/omnibus
2 parents af5b824 + b60b3ee commit 4a8810b

File tree

4 files changed

+19
-7
lines changed

4 files changed

+19
-7
lines changed

library/src/scala/Enumeration.scala

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,11 @@ import scala.util.matching.Regex
2525
* `Value` type member of the enumeration (`Value` selected on the stable
2626
* identifier path of the enumeration instance).
2727
*
28+
* Values SHOULD NOT be added to an enumeration after its construction;
29+
* doing so makes the enumeration thread-unsafe. If values are added to an
30+
* enumeration from multiple threads (in a non-synchronized fashion) after
31+
* construction, the behavior of the enumeration is undefined.
32+
*
2833
* @example {{{
2934
* // Define a new enumeration with a type alias and work with the full set of enumerated values
3035
* object WeekDay extends Enumeration {

library/src/scala/collection/immutable/HashMap.scala

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -297,7 +297,7 @@ object HashMap extends ImmutableMapFactory[HashMap] with BitOperations.Int {
297297
protected override def merge0[B1 >: B](that: HashMap[A, B1], level: Int, merger: Merger[A, B1]): HashMap[A, B1] = {
298298
// this can be made more efficient by passing the entire ListMap at once
299299
var m = that
300-
for (p <- kvs) m = m.updated0(p._1, this.hash, level, p._2, p, merger)
300+
for (p <- kvs) m = m.updated0(p._1, this.hash, level, p._2, p, merger.invert)
301301
m
302302
}
303303
}

library/src/scala/collection/mutable/ArrayBuilder.scala

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ object ArrayBuilder {
6060
private var size: Int = 0
6161

6262
private def mkArray(size: Int): Array[T] = {
63-
val newelems = new Array[T](size)
64-
if (this.size > 0) Array.copy(elems, 0, newelems, 0, this.size)
65-
newelems
63+
if (capacity == size && capacity > 0) elems
64+
else if (elems eq null) new Array[T](size)
65+
else java.util.Arrays.copyOf[T](elems, size)
6666
}
6767

6868
private def resize(size: Int) {

library/src/scala/collection/mutable/HashMap.scala

Lines changed: 10 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,18 +75,25 @@ extends AbstractMap[A, B]
7575
override def getOrElseUpdate(key: A, defaultValue: => B): B = {
7676
val hash = elemHashCode(key)
7777
val i = index(hash)
78-
val entry = findEntry(key, i)
79-
if (entry != null) entry.value
78+
val firstEntry = findEntry(key, i)
79+
if (firstEntry != null) firstEntry.value
8080
else {
8181
val table0 = table
8282
val default = defaultValue
8383
// Avoid recomputing index if the `defaultValue()` hasn't triggered
8484
// a table resize.
8585
val newEntryIndex = if (table0 eq table) i else index(hash)
86-
addEntry(createNewEntry(key, default), newEntryIndex)
86+
val e = createNewEntry(key, default)
87+
// Repeat search
88+
// because evaluation of `default` can bring entry with `key`
89+
val secondEntry = findEntry(key, newEntryIndex)
90+
if (secondEntry == null) addEntry0(e, newEntryIndex)
91+
else secondEntry.value = default
92+
default
8793
}
8894
}
8995

96+
9097
/* inlined HashTable.findEntry0 to preserve its visibility */
9198
private[this] def findEntry(key: A, h: Int): Entry = {
9299
var e = table(h).asInstanceOf[Entry]

0 commit comments

Comments
 (0)