From e380610ab0ff106c1d15afa329bdeab7138a227c Mon Sep 17 00:00:00 2001 From: Alexander Shabalin Date: Wed, 16 Aug 2023 11:19:37 +0200 Subject: [PATCH] [K/N] Add a note to identityHashCode() --- kotlin-native/runtime/src/main/cpp/Natives.cpp | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/kotlin-native/runtime/src/main/cpp/Natives.cpp b/kotlin-native/runtime/src/main/cpp/Natives.cpp index 140c9cc118f..06b836efe01 100644 --- a/kotlin-native/runtime/src/main/cpp/Natives.cpp +++ b/kotlin-native/runtime/src/main/cpp/Natives.cpp @@ -42,6 +42,11 @@ KBoolean Kotlin_Any_equals(KConstRef thiz, KConstRef other) { } KInt Kotlin_Any_hashCode(KConstRef thiz) { + // NOTE: `Any?.identityHashCode()` is used in Blackhole implementations of both kotlinx-benchmark and + // K/N's own benchmarks. These usages rely on this being an intrinsic property of the object. + // So, calling `obj.identityHashCode()` should be seen by the optimizer as reading the entire + // `obj` memory, and any changes to `obj` beforehand couldn't be optimized away. Additionally, + // it should be very cheap to call in order not to pollute the time measurements. // Here we will use different mechanism for stable hashcode, using meta-objects // if moving collector will be used. return reinterpret_cast(thiz);