diff --git a/compiler/testData/codegen/box/size/add.kt b/compiler/testData/codegen/box/size/add.kt index 5afbf0266f4..c7d5b5f8b9e 100644 --- a/compiler/testData/codegen/box/size/add.kt +++ b/compiler/testData/codegen/box/size/add.kt @@ -1,7 +1,7 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_294 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_247 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 139_730 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_629 // FILE: test.kt diff --git a/compiler/testData/codegen/box/size/helloWorld.kt b/compiler/testData/codegen/box/size/helloWorld.kt index 641f7d9df7b..dbfd1b677a5 100644 --- a/compiler/testData/codegen/box/size/helloWorld.kt +++ b/compiler/testData/codegen/box/size/helloWorld.kt @@ -1,7 +1,7 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_626 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_188 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 140_062 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_570 fun box(): String { println("Hello, World!") diff --git a/compiler/testData/codegen/box/size/helloWorldDOM.kt b/compiler/testData/codegen/box/size/helloWorldDOM.kt index 33e1bfa2c20..651e5652c07 100644 --- a/compiler/testData/codegen/box/size/helloWorldDOM.kt +++ b/compiler/testData/codegen/box/size/helloWorldDOM.kt @@ -1,7 +1,7 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 149_186 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_686 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 142_622 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 7_068 // FILE: test.kt diff --git a/compiler/testData/codegen/box/size/ok.kt b/compiler/testData/codegen/box/size/ok.kt index 63a1e7e8a5b..9779ce29a61 100644 --- a/compiler/testData/codegen/box/size/ok.kt +++ b/compiler/testData/codegen/box/size/ok.kt @@ -1,6 +1,6 @@ // TARGET_BACKEND: WASM -// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 146_345 -// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 8_119 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: wasm 139_781 +// WASM_DCE_EXPECTED_OUTPUT_SIZE: mjs 6_501 fun box() = "OK" \ No newline at end of file diff --git a/libraries/stdlib/wasm/builtins/kotlin/Any.kt b/libraries/stdlib/wasm/builtins/kotlin/Any.kt index 07d4606dd1f..cc5576bd926 100644 --- a/libraries/stdlib/wasm/builtins/kotlin/Any.kt +++ b/libraries/stdlib/wasm/builtins/kotlin/Any.kt @@ -41,6 +41,13 @@ public open class Any @WasmPrimitiveConstructor constructor() { */ internal var _hashCode: Int = 0 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) _hashCode = Random.nextInt(1, Int.MAX_VALUE) return _hashCode @@ -52,6 +59,6 @@ public open class Any @WasmPrimitiveConstructor constructor() { public open fun toString(): String { val typeData = getTypeInfoTypeDataByPtr(typeInfo) val qualifiedName = if (typeData.packageName.isEmpty()) typeData.typeName else "${typeData.packageName}.${typeData.typeName}" - return "$qualifiedName@${hashCode()}" + return "$qualifiedName@${identityHashCode()}" } }