[vm/ffi] Performance cliff for nested structs and arrays if used both with Pointer
and TypedData
backing store
#54892
Labels
area-vm
Use area-vm for VM related issues, including code coverage, and the AOT and JIT backends.
library-ffi
triaged
Issue has been triaged by sub team
type-performance
Issue relates to performance or code size
sdk/pkg/vm/lib/transformations/ffi/common.dart
Lines 913 to 928 in ab09f99
This pattern of checking for whether something is backed by a Pointer, and doing pointer arithmetic if it is a pointer and a typed data view if it is not a pointer causes a performance cliff when TFA cannot prove that one of the branches is definitely not happening.
ddfc00e caused benchmarks/FfiStructCopy/dart/FfiStructCopy.dart to slow down 15x. This is because the TypedData path in the if is no longer removed by TFA (due to the TypedData constructors for Array now being shared). This causes the inliner to bail out on inlining all Pointer and TypedData view calculations. Which in turn causes a bunch of calls and allocations.
I presume that any reasonably large FFI program would have both Pointer and TypedDatas backing structs, so this micro-benchmark was probably giving us a too rosy view. (Because if this, I don't believe a revert is in order.)
We should try to remove any branching on something being a
Pointer
orTypedData
.Some ideas:
typedDataBase.offsetBy
and calling that in the struct/array implementation instead of branching on aninstanceOf<Pointer>
. The question is what IL/machine code to generate in this case. We'd still need to branch on whether to allocate aPointer
object or anTypedDataView
object._Compound
and never create new typed data views or pointers at all. Nested struct and array accessors would return the same typed data base object but with a different int offset.cc @mkustermann I believe you have argued for option (c) before.
The text was updated successfully, but these errors were encountered: