Skip to content

Commit

Permalink
chore: Update benchmarks to use new query syntax
Browse files Browse the repository at this point in the history
perf: Optimize query iteration performance slightly
  • Loading branch information
0ffz committed Nov 1, 2024
1 parent 9952033 commit 0d5e7b3
Show file tree
Hide file tree
Showing 17 changed files with 1,739 additions and 168 deletions.
8 changes: 4 additions & 4 deletions geary-benchmarks/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -43,10 +43,10 @@ benchmark {
}

create("specific") {
include("EventCalls")
warmups = 1
iterations = 1
iterationTime = 3
include("Unpack6")
warmups = 3
iterations = 3
iterationTime = 5
iterationTimeUnit = "sec"
}
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mineinabyss.geary.benchmarks

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.oneMil
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
Expand Down
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
package com.mineinabyss.geary.benchmarks.helpers

import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.modules.TestEngineModule
import com.mineinabyss.geary.modules.geary
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
package com.mineinabyss.geary.benchmarks.misc

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.GearyBenchmark
import co.touchlab.kermit.Logger
import co.touchlab.kermit.Severity
import com.mineinabyss.geary.benchmarks.helpers.*
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,51 +3,9 @@ package com.mineinabyss.geary.benchmarks.unpacking
import com.mineinabyss.geary.benchmarks.helpers.*
import com.mineinabyss.geary.modules.Geary
import com.mineinabyss.geary.systems.query.GearyQuery
import com.mineinabyss.geary.systems.query.query

class Query1(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
}

class Query1Defaulting(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>().orDefault { Comp1(0) }
override fun ensure() = this { has<Comp1>() }
}

class Query2(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
val comp2 by get<Comp2>()
}

class Query6(world: Geary) : GearyQuery(world) {
val comp1 by get<Comp1>()
val comp2 by get<Comp2>()
val comp3 by get<Comp3>()
val comp4 by get<Comp4>()
val comp5 by get<Comp5>()
val comp6 by get<Comp6>()
}


class Query6WithoutDelegate(world: Geary) : GearyQuery(world) {
val comp1 = get<Comp1>()
val comp2 = get<Comp2>()
val comp3 = get<Comp3>()
val comp4 = get<Comp4>()
val comp5 = get<Comp5>()
val comp6 = get<Comp6>()

override fun ensure() = this {
hasSet<Comp1>()
hasSet<Comp2>()
hasSet<Comp3>()
hasSet<Comp4>()
hasSet<Comp5>()
hasSet<Comp6>()
}
}

fun Geary.systemOf1() = cache(::Query1)
fun Geary.systemOf1Defaulting() = cache(::Query1Defaulting)
fun Geary.systemOf2() = cache(::Query2)
fun Geary.systemOf6() = cache(::Query6)
fun Geary.systemOf6WithoutDelegate() = cache(::Query6WithoutDelegate)
fun Geary.systemOf1() = cache(query<Comp1>())
fun Geary.systemOf1OrNull() = cache(query<Comp1?>())
fun Geary.systemOf2() = cache(query<Comp1, Comp2>())
fun Geary.systemOf6() = cache(query<Comp1, Comp2, Comp3, Comp4, Comp5, Comp6>())
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.Comp1
import com.mineinabyss.geary.benchmarks.helpers.GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
import org.openjdk.jmh.annotations.Benchmark
Expand All @@ -11,7 +11,6 @@ import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class Unpack1Benchmark : GearyBenchmark() {

@Setup
fun setUp() {
repeat(tenMil) {
Expand All @@ -23,8 +22,12 @@ class Unpack1Benchmark : GearyBenchmark() {

@Benchmark
fun unpack1of1Comp() {
systemOf1().forEach {
comp1
systemOf1().forEach { (a) -> }
}

@Benchmark
fun unpack1Nullable() {
systemOf1OrNull().forEach { (a) ->
}
}
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.Comp1
import com.mineinabyss.geary.benchmarks.helpers.Comp2
import com.mineinabyss.geary.benchmarks.helpers.GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.tenMil
import com.mineinabyss.geary.helpers.entity
import org.openjdk.jmh.annotations.Benchmark
Expand All @@ -24,16 +24,11 @@ class Unpack2Benchmark : GearyBenchmark() {

@Benchmark
fun unpack1of2Comp() {
systemOf1().forEach {
it.comp1
}
systemOf2().forEach { (a) -> }
}

@Benchmark
fun unpack2of2Comp() {
systemOf2().forEach {
it.comp1
it.comp2
}
systemOf2().forEach { (a, b) -> }
}
}
Original file line number Diff line number Diff line change
@@ -1,16 +1,15 @@
package com.mineinabyss.geary.benchmarks.unpacking

import GearyBenchmark
import com.mineinabyss.geary.benchmarks.helpers.*
import com.mineinabyss.geary.helpers.entity
import com.mineinabyss.geary.systems.query.query
import org.openjdk.jmh.annotations.Benchmark
import org.openjdk.jmh.annotations.Scope
import org.openjdk.jmh.annotations.Setup
import org.openjdk.jmh.annotations.State

@State(Scope.Benchmark)
class Unpack6Benchmark : GearyBenchmark() {

@Setup
fun setUp() {
repeat(tenMil) {
Expand All @@ -27,57 +26,24 @@ class Unpack6Benchmark : GearyBenchmark() {

@Benchmark
fun unpack1of6Comp() {
systemOf1().forEach {
it.comp1
}
}

@Benchmark
fun unpack1of6CompWithUnoptimizedAccessor() {
systemOf1Defaulting().forEach {
it.comp1
systemOf6().forEach { (a) ->
}
}

@Benchmark
fun unpack6of6Comp() {
systemOf6().forEach {
it.comp1
it.comp2
it.comp3
it.comp4
it.comp5
it.comp6
}
}

@Benchmark
fun unpack1of6CompNoDelegate() {
systemOf6WithoutDelegate().forEach {
it.comp1.get(it)
}
}

// This test gives ridiculous numbers, I think kotlin might just be optimizing some calls away that it can't with a delegate?
@Benchmark
fun unpack6of6CompNoDelegate() {
systemOf6WithoutDelegate().forEach {
it.comp1.get(it)
it.comp2.get(it)
it.comp3.get(it)
it.comp4.get(it)
it.comp5.get(it)
it.comp6.get(it)
systemOf6().forEach { (a, b, c, d, e, f) ->
}
}
}

fun main() {
Unpack6Benchmark().apply {
setUp()
val query = cache(query<Comp1, Comp2, Comp3, Comp4, Comp5, Comp6>())
repeat(10000) {
// unpack1of6CompNoDelegate()
unpack6of6CompNoDelegate()
query.forEach { (a, b, c, d, e, f) ->
}
}
}
}
Loading

0 comments on commit 0d5e7b3

Please sign in to comment.