Skip to content

[vm/ffi] Optimize ffi calls with multiple Dart_Handle arguments #42341

Closed
@dcharkes

Description

@dcharkes

FfiCall.Handlex01(RunTime): 347.8671304347826 us.
FfiCall.Handlex02(RunTime): 384.86376755820663 us.
FfiCall.Handlex04(RunTime): 492.896007885658 us.
FfiCall.Handlex10(RunTime): 846.1214043993232 us.
FfiCall.Handlex20(RunTime): 1193.412291169451 us.

We currently call into C for every handle with

Fragment FlowGraphBuilder::AllocateHandle(LocalVariable* api_local_scope) {
Fragment code;
if (api_local_scope != nullptr) {
// Use the reference the scope we created in the trampoline.
code += LoadLocal(api_local_scope);
} else {
// Or get a reference to the top handle scope.
code += GetTopHandleScope();
}
Value* api_local_scope_value = Pop();
auto* instr = new (Z) AllocateHandleInstr(api_local_scope_value);
Push(instr);
code <<= instr;
return code;
}

extern "C" LocalHandle* DLRT_AllocateHandle(ApiLocalScope* scope) {
CHECK_STACK_ALIGNMENT;
TRACE_RUNTIME_CALL("AllocateHandle %p", scope);
LocalHandle* return_value = scope->local_handles()->AllocateHandle();
TRACE_RUNTIME_CALL("AllocateHandle returning %p", return_value);
return return_value;
}
DEFINE_RAW_LEAF_RUNTIME_ENTRY(
AllocateHandle,
1,
false /* is_float */,
reinterpret_cast<RuntimeFunction>(&DLRT_AllocateHandle));

Instead, we can extend the EnterHandleScope with an int how many handles we want, and do some integer arithmetic in IL to compute the addresses of those handles. (Up to the number of handles that fit into one single block.)

Fragment FlowGraphBuilder::EnterHandleScope() {
auto* instr = new (Z)
EnterHandleScopeInstr(EnterHandleScopeInstr::Kind::kEnterHandleScope);
Push(instr);
return Fragment(instr);
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    area-vmUse area-vm for VM related issues, including code coverage, and the AOT and JIT backends.library-ffitype-enhancementA request for a change that isn't a bugtype-performanceIssue relates to performance or code size

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions