From d7a283de2620720120b58d4a71ba636089c8d2cf Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=96mer=20Sinan=20A=C4=9Facan?= Date: Thu, 5 Sep 2024 11:24:10 +0000 Subject: [PATCH] [dart2wasm] Allocate boxed bools once When boxing bool values use cached values for true and false and avoid allocating new boxes for bools. Converts code like the following to allocate a boxed bool object, e.g. in `dartifyRaw`: ;; bool value on stack local.set $var6 ;; save bool value i32.const 3 ;; class id local.get $var6 ;; get bool value struct.new $BoxedBool Into: ;; bool value on stack if global.get $global922 else global.get $global924 end Change-Id: Iec19d6d0d42bfe39804a6487a7ed6b53bf63b5a4 Reviewed-on: https://dart-review.googlesource.com/c/sdk/+/382382 Reviewed-by: Martin Kustermann --- pkg/dart2wasm/lib/translator.dart | 14 +++++++++++++- 1 file changed, 13 insertions(+), 1 deletion(-) diff --git a/pkg/dart2wasm/lib/translator.dart b/pkg/dart2wasm/lib/translator.dart index 4e6c7e208f7..dfcd9baaf49 100644 --- a/pkg/dart2wasm/lib/translator.dart +++ b/pkg/dart2wasm/lib/translator.dart @@ -875,8 +875,20 @@ class Translator with KernelNodes { } } else if (to is w.RefType) { // Boxing - ClassInfo info = classInfo[boxedClasses[from]!]!; + Class cls = boxedClasses[from]!; + ClassInfo info = classInfo[cls]!; assert(info.struct.isSubtypeOf(to.heapType)); + + if (cls == boxedBoolClass) { + final constantType = w.RefType(info.struct, nullable: false); + b.if_([], [constantType]); + constants.instantiateConstant(b, BoolConstant(true), constantType); + b.else_(); + constants.instantiateConstant(b, BoolConstant(false), constantType); + b.end(); + return; + } + w.Local temp = b.addLocal(from); b.local_set(temp); b.i32_const(info.classId);