[Wasm] Introducing private identityHashCode shared between Any's hashCode and toString

It splits the dependency of toString from hashCode that prevents keeping hashCode when toString is used.
This commit is contained in:
Zalim Bashorov
2023-03-28 01:30:03 +02:00
parent 6e3eef7a88
commit 2ef47901d0
5 changed files with 16 additions and 9 deletions
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM // TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_294 // WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 139_730
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_247 // WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_629
// FILE: test.kt // FILE: test.kt
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM // TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_626 // WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 140_062
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_188 // WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_570
fun box(): String { fun box(): String {
println("Hello, World!") println("Hello, World!")
+2 -2
View File
@@ -1,7 +1,7 @@
// TARGET_BACKEND: WASM // TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 149_186 // WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 142_622
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_686 // WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 7_068
// FILE: test.kt // FILE: test.kt
+2 -2
View File
@@ -1,6 +1,6 @@
// TARGET_BACKEND: WASM // TARGET_BACKEND: WASM
// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_345 // WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 139_781
// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_119 // WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_501
fun box() = "OK" fun box() = "OK"
+8 -1
View File
@@ -41,6 +41,13 @@ public open class Any @WasmPrimitiveConstructor constructor() {
*/ */
internal var _hashCode: Int = 0 internal var _hashCode: Int = 0
public open fun hashCode(): Int { public open fun hashCode(): Int {
return identityHashCode()
}
// Don't use outside, otherwise it could break classes reusing `_hashCode` field, like String.
// Don't inline it into usages, specifically to `hashCode`.
// It was extracted to remove `toString`'s dependency on `hashCode`, which improves output size when DCE is involved.
private fun identityHashCode(): Int {
if (_hashCode == 0) if (_hashCode == 0)
_hashCode = Random.nextInt(1, Int.MAX_VALUE) _hashCode = Random.nextInt(1, Int.MAX_VALUE)
return _hashCode return _hashCode
@@ -52,6 +59,6 @@ public open class Any @WasmPrimitiveConstructor constructor() {
public open fun toString(): String { public open fun toString(): String {
val typeData = getTypeInfoTypeDataByPtr(typeInfo) val typeData = getTypeInfoTypeDataByPtr(typeInfo)
val qualifiedName = if (typeData.packageName.isEmpty()) typeData.typeName else "${typeData.packageName}.${typeData.typeName}" val qualifiedName = if (typeData.packageName.isEmpty()) typeData.typeName else "${typeData.packageName}.${typeData.typeName}"
return "$qualifiedName@${hashCode()}" return "$qualifiedName@${identityHashCode()}"
} }
} }