Skip to content

Commit

Permalink
Add ObjStack base class
Browse files Browse the repository at this point in the history
  • Loading branch information
durban committed Apr 28, 2024
1 parent 995cfc7 commit 6e2f185
Show file tree
Hide file tree
Showing 6 changed files with 256 additions and 46 deletions.
161 changes: 161 additions & 0 deletions bench/results_internalstackbench.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,161 @@
[
{
"jmhVersion" : "1.37",
"benchmark" : "dev.tauri.choam.core.bench.InternalStackBench.byteStack",
"mode" : "thrpt",
"threads" : 1,
"forks" : 1,
"jvm" : "/usr/lib/jvm/java-17-openjdk-amd64/bin/java",
"jvmArgs" : [
"-Ddev.tauri.choam.stats=true"
],
"jdkVersion" : "17.0.10",
"vmName" : "OpenJDK 64-Bit Server VM",
"vmVersion" : "17.0.10",
"warmupIterations" : 5,
"warmupTime" : "10 s",
"warmupBatchSize" : 1,
"measurementIterations" : 5,
"measurementTime" : "10 s",
"measurementBatchSize" : 1,
"primaryMetric" : {
"score" : 455448.14199102187,
"scoreError" : 44383.85916467767,
"scoreConfidence" : [
411064.2828263442,
499832.00115569955
],
"scorePercentiles" : {
"0.0" : 435227.7027486247,
"50.0" : 459416.3277988159,
"90.0" : 464246.32292815176,
"95.0" : 464246.32292815176,
"99.0" : 464246.32292815176,
"99.9" : 464246.32292815176,
"99.99" : 464246.32292815176,
"99.999" : 464246.32292815176,
"99.9999" : 464246.32292815176,
"100.0" : 464246.32292815176
},
"scoreUnit" : "ops/s",
"rawData" : [
[
458273.9351155497,
460076.4213639668,
435227.7027486247,
464246.32292815176,
459416.3277988159
]
]
},
"secondaryMetrics" : {

}
},
{
"jmhVersion" : "1.37",
"benchmark" : "dev.tauri.choam.core.bench.InternalStackBench.listObjStack",
"mode" : "thrpt",
"threads" : 1,
"forks" : 1,
"jvm" : "/usr/lib/jvm/java-17-openjdk-amd64/bin/java",
"jvmArgs" : [
"-Ddev.tauri.choam.stats=true"
],
"jdkVersion" : "17.0.10",
"vmName" : "OpenJDK 64-Bit Server VM",
"vmVersion" : "17.0.10",
"warmupIterations" : 5,
"warmupTime" : "10 s",
"warmupBatchSize" : 1,
"measurementIterations" : 5,
"measurementTime" : "10 s",
"measurementBatchSize" : 1,
"primaryMetric" : {
"score" : 288301.25161354896,
"scoreError" : 5750.103795063591,
"scoreConfidence" : [
282551.1478184854,
294051.3554086125
],
"scorePercentiles" : {
"0.0" : 286272.07668261183,
"50.0" : 288573.9876125965,
"90.0" : 290220.8409964899,
"95.0" : 290220.8409964899,
"99.0" : 290220.8409964899,
"99.9" : 290220.8409964899,
"99.99" : 290220.8409964899,
"99.999" : 290220.8409964899,
"99.9999" : 290220.8409964899,
"100.0" : 290220.8409964899
},
"scoreUnit" : "ops/s",
"rawData" : [
[
288573.9876125965,
290220.8409964899,
287502.2098314421,
288937.14294460445,
286272.07668261183
]
]
},
"secondaryMetrics" : {

}
},
{
"jmhVersion" : "1.37",
"benchmark" : "dev.tauri.choam.core.bench.InternalStackBench.listObjStackAbstract",
"mode" : "thrpt",
"threads" : 1,
"forks" : 1,
"jvm" : "/usr/lib/jvm/java-17-openjdk-amd64/bin/java",
"jvmArgs" : [
"-Ddev.tauri.choam.stats=true"
],
"jdkVersion" : "17.0.10",
"vmName" : "OpenJDK 64-Bit Server VM",
"vmVersion" : "17.0.10",
"warmupIterations" : 5,
"warmupTime" : "10 s",
"warmupBatchSize" : 1,
"measurementIterations" : 5,
"measurementTime" : "10 s",
"measurementBatchSize" : 1,
"primaryMetric" : {
"score" : 286154.4597080738,
"scoreError" : 5778.150016780637,
"scoreConfidence" : [
280376.30969129317,
291932.60972485447
],
"scorePercentiles" : {
"0.0" : 283702.6515427146,
"50.0" : 286237.253674842,
"90.0" : 287504.8283045882,
"95.0" : 287504.8283045882,
"99.0" : 287504.8283045882,
"99.9" : 287504.8283045882,
"99.99" : 287504.8283045882,
"99.999" : 287504.8283045882,
"99.9999" : 287504.8283045882,
"100.0" : 287504.8283045882
},
"scoreUnit" : "ops/s",
"rawData" : [
[
286095.3307041361,
287504.8283045882,
286237.253674842,
287232.2343140882,
283702.6515427146
]
]
},
"secondaryMetrics" : {

}
}
]
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ import org.openjdk.jmh.infra.Blackhole

import internal.mcas.Mcas
import stats.RxnProfiler
import bench.util.McasImplState
import dev.tauri.choam.bench.util.McasImplState

@Fork(3)
@Threads(2) // 4, 6, 8, ...
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,63 +17,74 @@

package dev.tauri.choam
package core
package bench

import org.openjdk.jmh.annotations._
import org.openjdk.jmh.infra.Blackhole

@Fork(2)
@Threads(1)
class InternalStackBench {

import InternalStackBench._

final val N = 512

@Benchmark
def byteStack(s: St, bh: Blackhole): Unit = {
s.byteStack = new ByteStack(initSize = 8)
def byteStack(bh: Blackhole): Unit = {
val byteStack = new ByteStack(initSize = 8)
var i = 0
while (i < N) {
s.byteStack.push(42)
byteStack.push(42)
i += 1
}
i = 0
while (i < N) {
bh.consume(s.byteStack.pop())
bh.consume(byteStack.pop())
i += 1
}
i = 0
while (i < N) {
s.byteStack.push(42)
byteStack.push(42)
i += 1
}
}

@Benchmark
def listObjStack(s: St, bh: Blackhole): Unit = {
s.listObjStack = new ListObjStack[String]
def listObjStack(bh: Blackhole): Unit = {
val listObjStack: ListObjStack[String] = new ListObjStack[String]
var i = 0
while (i < N) {
s.listObjStack.push("test")
listObjStack.push("test")
i += 1
}
i = 0
while (i < N) {
bh.consume(s.listObjStack.pop())
bh.consume(listObjStack.pop())
i += 1
}
i = 0
while (i < N) {
s.listObjStack.push("test")
listObjStack.push("test")
i += 1
}
}
}

private object InternalStackBench {

@State(Scope.Benchmark)
class St {
final var byteStack: ByteStack = null
final var listObjStack: ListObjStack[String] = null
@Benchmark
def listObjStackAbstract(bh: Blackhole): Unit = {
val listObjStackAbstract: ObjStack[String] = new ListObjStack[String]
var i = 0
while (i < N) {
listObjStackAbstract.push("test")
i += 1
}
i = 0
while (i < N) {
bh.consume(listObjStackAbstract.pop())
i += 1
}
i = 0
while (i < N) {
listObjStackAbstract.push("test")
i += 1
}
}
}
29 changes: 11 additions & 18 deletions core/shared/src/main/scala/dev/tauri/choam/core/ListObjStack.scala
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
package dev.tauri.choam
package core

private final class ListObjStack[A]() {
private final class ListObjStack[A]() extends ObjStack[A] {

private[this] var lst: ListObjStack.Lst[A] =
null
Expand All @@ -28,36 +28,29 @@ private final class ListObjStack[A]() {
else "ListObjStack()"
}

final def push(a: A): Unit = {
final override def push(a: A): Unit = {
this.lst = new ListObjStack.Lst(a, this.lst)
}

final def pushAll(as: Iterable[A]): Unit = {
val it = as.iterator
while (it.hasNext) {
this.push(it.next())
}
}

private[this] final def assertNonEmpty(): Unit = {
if (this.isEmpty) {
throw new NoSuchElementException
}
}

final def pop(): A = {
final override def pop(): A = {
assertNonEmpty()
val r = this.lst.head
this.lst = this.lst.tail
r
}

final def peek(): A = {
final override def peek(): A = {
assertNonEmpty()
this.lst.head
}

final def peekSecond(): A = {
final override def peekSecond(): A = {
assertNonEmpty()
this.lst.tail match {
case null =>
Expand All @@ -67,27 +60,27 @@ private final class ListObjStack[A]() {
}
}

final def clear(): Unit = {
final override def clear(): Unit = {
this.lst = null
}

final def isEmpty: Boolean = {
final override def isEmpty: Boolean = {
this.lst eq null
}

final def nonEmpty: Boolean = {
final override def nonEmpty: Boolean = {
this.lst ne null
}

final def takeSnapshot(): ListObjStack.Lst[A] = {
final override def takeSnapshot(): ListObjStack.Lst[A] = {
this.lst
}

final def loadSnapshot(snapshot: ListObjStack.Lst[A]): Unit = {
final override def loadSnapshot(snapshot: ListObjStack.Lst[A]): Unit = {
this.lst = snapshot
}

final def loadSnapshotUnsafe(snapshot: ListObjStack.Lst[Any]): Unit = {
final override def loadSnapshotUnsafe(snapshot: ListObjStack.Lst[Any]): Unit = {
this.lst = snapshot.asInstanceOf[ListObjStack.Lst[A]]
}
}
Expand Down
49 changes: 49 additions & 0 deletions core/shared/src/main/scala/dev/tauri/choam/core/ObjStack.scala
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
/*
* SPDX-License-Identifier: Apache-2.0
* Copyright 2016-2024 Daniel Urban and contributors listed in NOTICE.txt
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package dev.tauri.choam
package core

private abstract class ObjStack[A]() {

def push(a: A): Unit

def pop(): A

def peek(): A

def peekSecond(): A

def clear(): Unit

def isEmpty: Boolean

def nonEmpty: Boolean

def takeSnapshot(): ListObjStack.Lst[A]

def loadSnapshot(snapshot: ListObjStack.Lst[A]): Unit

def loadSnapshotUnsafe(snapshot: ListObjStack.Lst[Any]): Unit

final def pushAll(as: Iterable[A]): Unit = {
val it = as.iterator
while (it.hasNext) {
this.push(it.next())
}
}
}
Loading

0 comments on commit 6e2f185

Please sign in to comment.