8000 Merge pull request #10981 from bbrehm/bbrehm/arraybufferInlinePerf · scala/scala@c6ce350 · GitHub
[go: up one dir, main page]

8000
Skip to content

Commit c6ce350

Browse files
authored
Merge pull request #10981 from bbrehm/bbrehm/arraybufferInlinePerf
improve ArrayBuffer.addOne performance
2 parents 0d570fb + fe85718 commit c6ce350

File tree

2 files changed

+16
-2
lines changed

2 files changed

+16
-2
lines changed

src/library/scala/collection/mutable/ArrayBuffer.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -140,9 +140,9 @@ class ArrayBuffer[A] private (initialElements: Array[AnyRef], initialSize: Int)
140140
def addOne(elem: A): this.type = {
141141
mutationCount += 1
142142
val newSize = size0 + 1
143-
ensureSize(newSize)
143+
if(array.length <= newSize - 1) ensureSize(newSize)
144144
size0 = newSize
145-
this(size0 - 1) = elem
145+
array(newSize - 1) = elem.asInstanceOf[AnyRef]
146146
this
147147
}
148148

test/benchmarks/src/main/scala/scala/collection/mutable/ArrayBufferBenchmark.scala

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,20 @@ class ArrayBufferBenchmark {
7474
bh.consume(b1)
7575
}
7676

77+
//addOne
78+
@Benchmark def addOneArrayBuffer(bh:Blackhole):Unit = {
79+
val res = ArrayBuffer[Object]()
80+
ref.asInstanceOf[ArrayBuffer[Object]].foreach(res.addOne)
81+
bh.consume(res)
82+
}
83+
84+
//addOne comparison
85+
@Benchmark def addOneArrayList(bh:Blackhole):Unit = {
86+
val res = new java.util.ArrayList[Object]()
87+
ref.asInstanceOf[ArrayBuffer[Object]].foreach(res.add)
88+
bh.consume(res)
89+
}
90+
7791
// append `Iterable` with known size
7892
@Benchmark def addAll2(bh: Blackhole): Unit = {
7993
val b = ref.clone()

0 commit comments

Comments
 (0)
0