Open
Description
import 'dart:typed_data';
@pragma("wasm:prefer-inline")
int sum(List<int> list) {
int ret = 0;
for (int i = 0; i < list.length; i += 1) {
ret += list[i];
}
return ret;
}
void main() {
Int64List intList = Int64List.fromList([1, 2, 3, 4]);
sum(intList);
sum([1, 2, 3, 4]);
}
Ideally we want all direct calls to Int64List.[]
to be inlined:
sdk/sdk/lib/_internal/wasm/lib/typed_data.dart
Lines 3054 to 3059 in e87c93b
But when a call becomes direct after wasm-opt, because we can't control wasm-opt's inliner with a similar pragma, it's not inlined. Relevant parts of the output of the code above:
(func $main (;297;)
...
loop $label1
local.get $var7
call $_WasmI64ArrayBase.length initializer
local.get $var2
i64.gt_s
if
local.get $var4
local.get $var7
local.get $var2
call $I64List.[]
ref.cast $BoxedInt
struct.get $BoxedInt $field1
i64.add
local.set $var4
local.get $var2
i64.const 1
i64.add
local.set $var2
br $label1
end
end $label1
...
)
Becuase []
is not inlined, each access to the array boxes the integer in the array before returning.