Closed jensjoha closed 1 week ago
Summary: The Dart VM produces inefficient code when iterating over an Iterable<String>
compared to a List<String>
. This results in unnecessary null checks and moves, even though the code is non-nullable. Switching to List<String>
resolves the issue.
The null check and assert looks like an inlined Iterator.moveNext
and Iterator.current
, where something being null
it's a sign the iterator has reached its end, and current is get current => _current as T;
At least as of c7f99144977aa10767eed8564808d402ce4540d1 the parser has the following code:
which - at least when compiling
pkg/front_end/tool/_fasta/compile.dart
- produces the following code:which - on top of in general have a lot of moves I that can't figure out where comes from - contains what seems to be null-checks (
StrictCompare:12(===, v132 T{X0??}, v0 T{Null?})
,AssertAssignable:18(v132 T{X0??}, v23 T{_TypeParameter}, ' in type cast', instantiator_type_args(v57 T{TypeArguments}), function_type_args(v0 T{Null?})) T{X0?}
) despite it being non-nullable code.Changing the type from
Iterable
toList
remedies the problem:and indeed doing a small benchmark:
says it's quite a bit faster having it typed as a list:
What's going on?