Skip to content

Commit 4507572

Browse files
jbachorikclaude
andcommitted
Trivial hot-path performance optimizations
- getLockIndex: % -> & (CONCURRENCY_LEVEL is power-of-2) - flightRecorder: hoist numContextAttributes() out of per-event loop - flightRecorder: remove duplicate flushIfNeeded in recordEvent dispatcher - NativeFrameResolution: carry CodeCache* to avoid re-scanning in walkVM - Libraries::findLibraryByAddress: thread-local last-hit cache (63x hot-case speedup) - wallClock: bind reservoir.sample() result by reference, not copy (150x speedup) - profiler: guard TSC reads with #ifdef COUNTERS - RecordingBuffer: mark final for compiler devirtualization of limit() - benchmarks: add hot_path_benchmark covering fixes 1/5/6 Co-Authored-By: Claude Sonnet 4.6 (1M context) <noreply@anthropic.com>
1 parent 0d2637d commit 4507572

9 files changed

Lines changed: 396 additions & 79 deletions

File tree

ddprof-lib/benchmarks/build.gradle.kts

Lines changed: 63 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,12 @@ plugins {
1515
// multi-config library structure
1616
}
1717

18-
val benchmarkName = "unwind_failures_benchmark"
18+
data class BenchmarkDef(val name: String, val source: String)
19+
20+
val benchmarks = listOf(
21+
BenchmarkDef("unwind_failures_benchmark", "src/unwindFailuresBenchmark.cpp"),
22+
BenchmarkDef("hot_path_benchmark", "src/hotPathBenchmark.cpp"),
23+
)
1924

2025
// Determine if we should build for this platform
2126
val shouldBuild = PlatformUtils.currentPlatform == Platform.MACOS ||
@@ -24,60 +29,74 @@ val shouldBuild = PlatformUtils.currentPlatform == Platform.MACOS ||
2429
if (shouldBuild) {
2530
val compiler = PlatformUtils.findCompiler(project)
2631

27-
// Compile task
28-
val compileTask = tasks.register<NativeCompileTask>("compileBenchmark") {
29-
onlyIf { shouldBuild && !project.hasProperty("skip-native") }
30-
group = "build"
31-
description = "Compile the unwinding failures benchmark"
32-
33-
this.compiler.set(compiler)
34-
compilerArgs.set(listOf("-O2", "-g", "-std=c++17"))
35-
sources.from(file("src/unwindFailuresBenchmark.cpp"))
36-
includes.from(project(":ddprof-lib").file("src/main/cpp"))
37-
objectFileDir.set(file("${layout.buildDirectory.get()}/obj/benchmark"))
38-
}
32+
benchmarks.forEach { bench ->
33+
val safeName = bench.name.split('_').joinToString("") { it.replaceFirstChar { c -> c.uppercase() } }
34+
35+
// Compile task
36+
val compileTask = tasks.register<NativeCompileTask>("compile$safeName") {
37+
onlyIf { shouldBuild && !project.hasProperty("skip-native") }
38+
group = "build"
39+
description = "Compile ${bench.name}"
3940

40-
// Link task
41-
val binary = file("${layout.buildDirectory.get()}/bin/$benchmarkName")
42-
val linkTask = tasks.register<NativeLinkExecutableTask>("linkBenchmark") {
43-
onlyIf { shouldBuild && !project.hasProperty("skip-native") }
44-
dependsOn(compileTask)
45-
group = "build"
46-
description = "Link the unwinding failures benchmark"
47-
48-
linker.set(compiler)
49-
val args = mutableListOf("-ldl", "-lpthread")
50-
if (PlatformUtils.currentPlatform == Platform.LINUX) {
51-
args.add("-lrt")
41+
this.compiler.set(compiler)
42+
compilerArgs.set(listOf("-O2", "-g", "-std=c++17"))
43+
sources.from(file(bench.source))
44+
includes.from(project(":ddprof-lib").file("src/main/cpp"))
45+
objectFileDir.set(file("${layout.buildDirectory.get()}/obj/${bench.name}"))
46+
}
47+
48+
// Link task
49+
val binary = file("${layout.buildDirectory.get()}/bin/${bench.name}")
50+
val linkTask = tasks.register<NativeLinkExecutableTask>("link$safeName") {
51+
onlyIf { shouldBuild && !project.hasProperty("skip-native") }
52+
dependsOn(compileTask)
53+
group = "build"
54+
description = "Link ${bench.name}"
55+
56+
linker.set(compiler)
57+
val args = mutableListOf("-ldl", "-lpthread")
58+
if (PlatformUtils.currentPlatform == Platform.LINUX) {
59+
args.add("-lrt")
60+
}
61+
linkerArgs.set(args)
62+
objectFiles.from(fileTree("${layout.buildDirectory.get()}/obj/${bench.name}") { include("*.o") })
63+
outputFile.set(binary)
64+
}
65+
66+
tasks.named("assemble") {
67+
dependsOn(linkTask)
5268
}
53-
linkerArgs.set(args)
54-
objectFiles.from(fileTree("${layout.buildDirectory.get()}/obj/benchmark") { include("*.o") })
55-
outputFile.set(binary)
56-
}
5769

58-
// Wire linkBenchmark into the standard assemble lifecycle
59-
tasks.named("assemble") {
60-
dependsOn(linkTask)
70+
tasks.register<Exec>("run$safeName") {
71+
dependsOn(linkTask)
72+
group = "verification"
73+
description = "Run ${bench.name}"
74+
75+
executable = binary.absolutePath
76+
77+
doFirst {
78+
if (project.hasProperty("args")) {
79+
args(project.property("args").toString().split(" "))
80+
}
81+
println("Running benchmark: ${binary.absolutePath}")
82+
}
83+
84+
doLast {
85+
println("Benchmark completed.")
86+
}
87+
}
6188
}
6289

63-
// Add a task to run the benchmark
90+
// Convenience alias: runBenchmark → original unwind failures benchmark
6491
tasks.register<Exec>("runBenchmark") {
65-
dependsOn(linkTask)
92+
dependsOn("linkUnwindFailuresBenchmark")
6693
group = "verification"
67-
description = "Run the unwinding failures benchmark"
68-
69-
executable = binary.absolutePath
70-
71-
// Add any additional arguments passed to the Gradle task
94+
description = "Run the unwinding failures benchmark (alias)"
95+
executable = file("${layout.buildDirectory.get()}/bin/unwind_failures_benchmark").absolutePath
7296
doFirst {
7397
if (project.hasProperty("args")) {
7498
args(project.property("args").toString().split(" "))
7599
}
76-
println("Running benchmark: ${binary.absolutePath}")
77-
}
78-
79-
doLast {
80-
println("Benchmark completed.")
81100
}
82101
}
83102
}

0 commit comments

Comments
 (0)