[K/N] Benchmarks on weak references
Merge-request: KT-MR-7575 Merged-by: Alexander Shabalin <Alexander.Shabalin@jetbrains.com>
This commit is contained in:
committed by
Space Team
parent
ab590e1279
commit
a31c3f979b
@@ -229,6 +229,9 @@ class RingLauncher : Launcher() {
|
||||
"Calls.interfaceMethodHexamorphic" to BenchmarkEntryWithInit.create(::CallsBenchmark, { interfaceMethodCall_HexamorphicCallsite() }),
|
||||
"LocalObjects.localArray" to BenchmarkEntryWithInit.create(::LocalObjectsBenchmark, { localArray() }),
|
||||
"ComplexArrays.outerProduct" to BenchmarkEntryWithInit.create(::ComplexArraysBenchmark, { outerProduct() }),
|
||||
"WeakRefBenchmark.aliveReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { aliveReference() }),
|
||||
"WeakRefBenchmark.deadReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { deadReference() }),
|
||||
"WeakRefBenchmark.dyingReference" to BenchmarkEntryWithInit.create(::WeakRefBenchmark, { dyingReference() }),
|
||||
)
|
||||
|
||||
init {
|
||||
|
||||
@@ -0,0 +1,75 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.ring
|
||||
|
||||
import kotlin.native.internal.GC
|
||||
import kotlin.native.ref.WeakReference
|
||||
import kotlin.test.assertEquals
|
||||
import kotlin.test.assertNotEquals
|
||||
import kotlinx.cinterop.StableRef
|
||||
import org.jetbrains.benchmarksLauncher.Blackhole
|
||||
import org.jetbrains.benchmarksLauncher.Random
|
||||
|
||||
private const val REPEAT_COUNT = BENCHMARK_SIZE
|
||||
private const val REFERENCES_COUNT = 3
|
||||
|
||||
private class Data(var x: Int = Random.nextInt(1000) + 1)
|
||||
|
||||
private class ReferenceWrapper private constructor(
|
||||
data: Data
|
||||
) {
|
||||
private val weak = WeakReference(data)
|
||||
private val strong = StableRef.create(data)
|
||||
|
||||
val value: Int
|
||||
get() {
|
||||
val ref: Data? = weak.value
|
||||
if (ref == null) {
|
||||
return 0
|
||||
}
|
||||
return ref.x
|
||||
}
|
||||
|
||||
fun dispose() {
|
||||
strong.dispose()
|
||||
}
|
||||
|
||||
companion object {
|
||||
fun create() = ReferenceWrapper(Data())
|
||||
}
|
||||
}
|
||||
|
||||
private fun ReferenceWrapper.stress() = (1..REPEAT_COUNT).sumOf {
|
||||
this.value
|
||||
}
|
||||
|
||||
open class WeakRefBenchmark {
|
||||
private val aliveRef = ReferenceWrapper.create()
|
||||
private val deadRef = ReferenceWrapper.create().apply {
|
||||
dispose()
|
||||
GC.collect()
|
||||
}
|
||||
|
||||
// Access alive reference.
|
||||
fun aliveReference() {
|
||||
assertNotEquals(0, aliveRef.stress())
|
||||
}
|
||||
|
||||
// Access dead reference.
|
||||
fun deadReference() {
|
||||
assertEquals(0, deadRef.stress())
|
||||
}
|
||||
|
||||
// Access reference that is nulled out in the middle.
|
||||
fun dyingReference() {
|
||||
val ref = ReferenceWrapper.create()
|
||||
|
||||
ref.dispose()
|
||||
GC.schedule()
|
||||
|
||||
Blackhole.consume(ref.stress())
|
||||
}
|
||||
}
|
||||
@@ -17,7 +17,11 @@ swiftBenchmark {
|
||||
applicationName = "swiftInterop"
|
||||
commonSrcDirs = listOf("$toolsPath/benchmarks/shared/src/main/kotlin/report", "src", "../shared/src/main/kotlin")
|
||||
nativeSrcDirs = listOf("../shared/src/main/kotlin-native/common", "../shared/src/main/kotlin-native/posix")
|
||||
swiftSources = listOf("$projectDir/swiftSrc/benchmarks.swift", "$projectDir/swiftSrc/main.swift")
|
||||
swiftSources = listOf(
|
||||
"$projectDir/swiftSrc/benchmarks.swift",
|
||||
"$projectDir/swiftSrc/main.swift",
|
||||
"$projectDir/swiftSrc/weakRefBenchmarks.swift",
|
||||
)
|
||||
compileTasks = listOf("compileKotlinNative", "linkBenchmark${buildType.name.toLowerCase().capitalize()}FrameworkNative")
|
||||
cleanBeforeRunTask = "compileKotlinNative"
|
||||
}
|
||||
}
|
||||
|
||||
@@ -0,0 +1,11 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.gc
|
||||
|
||||
import kotlin.native.internal.GC
|
||||
|
||||
fun collect() = GC.collect()
|
||||
fun schedule() = GC.schedule()
|
||||
@@ -0,0 +1,10 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
package org.jetbrains.model
|
||||
|
||||
import org.jetbrains.benchmarksLauncher.Random
|
||||
|
||||
class KotlinData(var x: Int = Random.nextInt())
|
||||
@@ -36,6 +36,28 @@ swiftLauncher.addBase(name: "createMultigraphOfInt", benchmark: companion.create
|
||||
lambda: { ($0 as! SwiftInteropBenchmarks).stringInterop() }))
|
||||
swiftLauncher.addBase(name: "simpleFunction", benchmark: companion.create(ctor: { return SwiftInteropBenchmarks() },
|
||||
lambda: { ($0 as! SwiftInteropBenchmarks).simpleFunction() }))
|
||||
swiftLauncher.addBase(
|
||||
name: "WeakRefBenchmark.aliveReference",
|
||||
benchmark: BenchmarkEntryWithInit.companion.create(
|
||||
ctor: { return WeakRefBenchmark() },
|
||||
lambda: { ($0 as! WeakRefBenchmark).aliveReference() }
|
||||
)
|
||||
)
|
||||
swiftLauncher.addBase(
|
||||
name: "WeakRefBenchmark.deadReference",
|
||||
benchmark: BenchmarkEntryWithInit.companion.create(
|
||||
ctor: { return WeakRefBenchmark() },
|
||||
lambda: { ($0 as! WeakRefBenchmark).deadReference() }
|
||||
)
|
||||
)
|
||||
swiftLauncher.addBase(
|
||||
name: "WeakRefBenchmark.dyingReference",
|
||||
benchmark: BenchmarkEntryWithInit.companion.create(
|
||||
ctor: { return WeakRefBenchmark() },
|
||||
lambda: { ($0 as! WeakRefBenchmark).dyingReference() }
|
||||
)
|
||||
)
|
||||
|
||||
runner.runBenchmarks(args: args, run: { (arguments: BenchmarkArguments) -> [BenchmarkResult] in
|
||||
if arguments is BaseBenchmarkArguments {
|
||||
let argumentsList: BaseBenchmarkArguments = arguments as! BaseBenchmarkArguments
|
||||
@@ -50,4 +72,4 @@ runner.runBenchmarks(args: args, run: { (arguments: BenchmarkArguments) -> [Benc
|
||||
return runner.parse(args: args, benchmarksListAction: { (baseOnly: KotlinBoolean) in swiftLauncher.benchmarksListAction(baseOnly: baseOnly.boolValue) }) },
|
||||
collect: { (benchmarks: [BenchmarkResult], arguments: BenchmarkArguments) -> Void in
|
||||
runner.collect(results: benchmarks, arguments: arguments)
|
||||
}, benchmarksListAction: { (baseOnly: KotlinBoolean) in swiftLauncher.benchmarksListAction(baseOnly: baseOnly.boolValue) })
|
||||
}, benchmarksListAction: { (baseOnly: KotlinBoolean) in swiftLauncher.benchmarksListAction(baseOnly: baseOnly.boolValue) })
|
||||
|
||||
@@ -0,0 +1,81 @@
|
||||
/*
|
||||
* Copyright 2010-2022 JetBrains s.r.o. Use of this source code is governed by the Apache 2.0 license
|
||||
* that can be found in the LICENSE file.
|
||||
*/
|
||||
|
||||
import Foundation
|
||||
import benchmark
|
||||
|
||||
private let REPEAT_COUNT = 10000
|
||||
private let REFERENCES_COUNT = 3
|
||||
|
||||
private class ReferenceWrapper {
|
||||
private weak var weakRef: KotlinData?
|
||||
private var strongRef: KotlinData?
|
||||
|
||||
init() {
|
||||
let data = KotlinData(x: Int32.random(in: 1 ... 1000))
|
||||
self.strongRef = data
|
||||
self.weakRef = data
|
||||
}
|
||||
|
||||
// This function must not be inlined. We must ensure `weakRef` is accessed at each loop iteration.
|
||||
@inline(never) func value() -> Int32 {
|
||||
let ref: KotlinData? = self.weakRef
|
||||
if ref == nil {
|
||||
return 0
|
||||
}
|
||||
return ref!.x
|
||||
}
|
||||
|
||||
func dispose() {
|
||||
self.strongRef = nil
|
||||
}
|
||||
|
||||
func stress() -> Int32 {
|
||||
var counter: Int32 = 0
|
||||
for _ in 1...REPEAT_COUNT {
|
||||
counter += self.value()
|
||||
}
|
||||
return counter
|
||||
}
|
||||
}
|
||||
|
||||
private func deadReferenceWrapper() -> ReferenceWrapper {
|
||||
let ref = ReferenceWrapper()
|
||||
ref.dispose()
|
||||
GCKt.collect()
|
||||
return ref
|
||||
}
|
||||
|
||||
class WeakRefBenchmark {
|
||||
private let aliveRef = ReferenceWrapper()
|
||||
private let deadRef = deadReferenceWrapper()
|
||||
|
||||
// Access alive reference.
|
||||
func aliveReference() {
|
||||
let counter = aliveRef.stress()
|
||||
if counter == 0 {
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
// Access dead reference.
|
||||
func deadReference() {
|
||||
let counter = deadRef.stress()
|
||||
if counter != 0 {
|
||||
fatalError()
|
||||
}
|
||||
}
|
||||
|
||||
// Access reference that is nulled out in the middle.
|
||||
func dyingReference() {
|
||||
let ref = ReferenceWrapper()
|
||||
|
||||
ref.dispose()
|
||||
GCKt.schedule()
|
||||
|
||||
let counter = ref.stress()
|
||||
Blackhole.companion.consume(value: counter)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user