1 of 8

V8 Fast API Calls

BlinkOn 14

Austin Eng / Google

enga@chromium.org

2 of 8

Today’s V8 → Blink Bindings (Simplified)

function foo() {� …� obj.draw(3, ...);� ...�}

void DrawOperationCallback(v8::FunctionCallbackInfo<v8::Value>& info) {� …� v8::Local<v8::Object> v8_receiver = info.This();� WebGPURenderPassEncoder* blink_receiver =� V8WebGPURenderPassEncoder::ToWrappableUnsafe(v8_receiver);� …� auto&& arg1 = NativeValueTraits<IDLUnsignedLong>::ArgumentValue(� isolate, 0, info[0], exception_state);� …� blink_receiver->draw(arg1, ...)�}

void GPURenderPassEncoder::draw(uint32_t count, ...) {� …�}

3 of 8

V8 Fast API Bindings (Simplified)

function foo() {� …� obj.draw(3, ...);� ...�}

void DrawOperationNoAllocDirectCallback(� v8::Value* arg0_receiver, uint32_t arg1_count, ...) {� GPURenderPassEncoder* blink_receiver =� ToScriptWrappable(static_cast<v8::Object*>(arg0_receiver))� ->ToImpl<GPURenderPassEncoder>();�� blink_receiver->draw(arg1_count, ...)�}

void GPURenderPassEncoder::draw(uint32_t count, ...) {� …�}

4 of 8

V8 Fast API Bindings - Why is it faster?

Code path kicks in after Turbofan optimizes the callsite

  1. Argument conversions from V8's internal representation to primitive C types are inlined into the callsite
  2. Turbofan generates a very cheap call to the fast callback in Blink instead of constructing a more heavyweight HandleScope
  3. Overload resolution may occur at Turbofan optimization time if the overload can be determined solely from the number of arguments.

5 of 8

20% Reduction in Main Thread CPU Time

6 of 8

V8 Fast API Call Restrictions

  • Must not trigger a GC event ⇒
    • No allocation on the managed heap
    • No calling back into Javascript
    • Guarded by CHECKs
  • Email v8-fast-api@ if you plan to use this

7 of 8

DeferredActions Queue

  • No allocation means Blink cannot throw exceptions, log to the console, etc. These operations can be supported by recording the intent to execute them in a deferred manner.
  • V8 will call into Blink a second time in a GC-safe manner so these actions can be executed.

8 of 8

What’s Next?

  • In-progress:
    • Interface type arguments
    • Sequence arguments
    • TypedArray arguments
    • Basic overloads