File tree Expand file tree Collapse file tree 1 file changed +10
-3
lines changed
library/src/scala/collection/mutable Expand file tree Collapse file tree 1 file changed +10
-3
lines changed Original file line number Diff line number Diff line change @@ -75,18 +75,25 @@ extends AbstractMap[A, B]
75
75
override def getOrElseUpdate (key : A , defaultValue : => B ): B = {
76
76
val hash = elemHashCode(key)
77
77
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
80
80
else {
81
81
val table0 = table
82
82
val default = defaultValue
83
83
// Avoid recomputing index if the `defaultValue()` hasn't triggered
84
84
// a table resize.
85
85
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
87
93
}
88
94
}
89
95
96
+
90
97
/* inlined HashTable.findEntry0 to preserve its visibility */
91
98
private [this ] def findEntry (key : A , h : Int ): Entry = {
92
99
var e = table(h).asInstanceOf [Entry ]
You can’t perform that action at this time.
0 commit comments