rrva
02/27/2023, 9:56 PMEmil Kantis
02/27/2023, 10:02 PMJson.encodeToJsonElement(person).jsonObject["name"]
No clue if it's faster than reflection though.. I suppose it would encode the entire object..ephemient
02/27/2023, 10:03 PMrrva
02/27/2023, 10:40 PMdata class Pojo(val name: String, val age: Int) {
private val nameGetter = this::class.declaredMemberProperties.first { it.name == "name" }.getter
fun fetchDirectly(): String {
return this.name
}
fun fetchViaGetter(): String {
return nameGetter.call(this) as String
}
}
Benchmark Mode Cnt Score Error Units
GetterAccessBenchmark.measureDirectAccess thrpt 15 467950724,747 ± 11791341,287 ops/s
GetterAccessBenchmark.measureReflectionAccess thrpt 15 25674364,892 ± 650107,316 ops/s
import foo.Pojo;
import org.openjdk.jmh.annotations.Benchmark;
import org.openjdk.jmh.annotations.Measurement;
import org.openjdk.jmh.annotations.Warmup;
import org.openjdk.jmh.infra.Blackhole;
@Warmup(iterations = 2, time = 2, batchSize = 3)
@Measurement(iterations = 3, time = 2, batchSize = 4)
public class GetterAccessBenchmark {
static Pojo pojo = new Pojo("Joe", 42);
@Benchmark
public void measureDirectAccess(Blackhole bh) {
Object name = pojo.fetchDirectly();
bh.consume(name);
}
@Benchmark
public void measureReflectionAccess(Blackhole bh) {
Object name = pojo.fetchViaGetter();
bh.consume(name);
}
}
rrva
02/27/2023, 10:44 PMephemient
02/27/2023, 11:01 PMBenchmark Mode Cnt Score Error Units
GetterAccessBenchmark.measureDirectAccess thrpt 3 56398569.746 ± 51533607.791 ops/s
GetterAccessBenchmark.measureReflectionAccess thrpt 3 17567775.066 ± 26923892.904 ops/s
GetterAccessBenchmark.measureReflectionAccessCached thrpt 3 18804598.799 ± 23139937.490 ops/s
where the cached version fetches the getter once, statically (for a slight improvement)rrva
02/28/2023, 12:30 AMrrva
02/28/2023, 5:40 AMBenchmark Mode Cnt Score Error Units
GetterAccessBenchmark.measureDirectAccess thrpt 15 446993080,524 ± 13086976,816 ops/s
GetterAccessBenchmark.measureHandleAccess thrpt 15 333375173,469 ± 1198007,040 ops/s
GetterAccessBenchmark.measureReflectionAccess thrpt 15 31950420,516 ± 349907,622 ops/s
and this
package foo
import fetching.LambdaFetchingSupport
import kotlinx.serialization.Serializable
import kotlin.reflect.full.declaredMemberProperties
private val nameGetter = Pojo::class.declaredMemberProperties.first { it.name == "name" }.getter
private val nameHandle = LambdaFetchingSupport.createGetter(Pojo::class.java, "name")
@Serializable
data class Pojo(val name: String, val age: Int) {
fun fetchDirectly(): String {
return this.name
}
fun fetchViaGetter(): String {
return nameGetter.call(this) as String
}
fun fetchViaHandle(): String {
return nameHandle.get().apply(this) as String
}
}
ephemient
02/28/2023, 5:58 AMMethodHandles.lookup().unreflect(kproperty.getter.javaMethod)
instead of going through thatrrva
02/28/2023, 6:26 AMrrva
02/28/2023, 6:28 AMephemient
02/28/2023, 6:28 AMrrva
02/28/2023, 6:28 AMrrva
02/28/2023, 6:29 AMephemient
02/28/2023, 6:33 AMrrva
02/28/2023, 6:43 AMrrva
02/28/2023, 6:44 AMephemient
02/28/2023, 6:45 AMephemient
02/28/2023, 6:48 AMfun pojoGetter(name: String) = when (name) {
"name" -> Pojo::name
"age" -> Pojo::age
else -> null
}
or whatever fits your use caserrva
02/28/2023, 7:04 AMephemient
02/28/2023, 7:06 AM