bc831f8ba3
This CL adds one byte string specialization support for fast API call arguments. It introduces a kOneByteString variant to CTypeInfo. We see a ~6x improvement in Deno's TextEncoder#encode microbenchmark. Rendered results: https://divy-v8-patches.deno.dev/ Bug: chromium:1052746 Change-Id: I47c3a9e101cd18ddc6ad58f627db3a34231b60f7 Reviewed-on: https://chromium-review.googlesource.com/c/v8/v8/+/4036884 Reviewed-by: Toon Verwaest <verwaest@chromium.org> Reviewed-by: Maya Lekova <mslekova@chromium.org> Commit-Queue: Maya Lekova <mslekova@chromium.org> Cr-Commit-Position: refs/heads/main@{#84552}
85 lines
2.6 KiB
JavaScript
85 lines
2.6 KiB
JavaScript
// Copyright 2022 the V8 project authors. All rights reserved.
|
|
// Use of this source code is governed by a BSD-style license that can be
|
|
// found in the LICENSE file.
|
|
|
|
// This file excercises one byte string support for fast API calls.
|
|
|
|
// Flags: --turbo-fast-api-calls --expose-fast-api --allow-natives-syntax --turbofan
|
|
// --always-turbofan is disabled because we rely on particular feedback for
|
|
// optimizing to the fastest path.
|
|
// Flags: --no-always-turbofan
|
|
// The test relies on optimizing/deoptimizing at predictable moments, so
|
|
// it's not suitable for deoptimization fuzzing.
|
|
// Flags: --deopt-every-n-times=0
|
|
|
|
assertThrows(() => d8.test.FastCAPI());
|
|
const fast_c_api = new d8.test.FastCAPI();
|
|
|
|
function assertSlowCall(input) {
|
|
assertEquals(new Uint8Array(input.length), copy_string(false, input));
|
|
}
|
|
|
|
function assertFastCall(input) {
|
|
const bytes = Uint8Array.from(input, c => c.charCodeAt(0));
|
|
assertEquals(bytes, copy_string(false, input));
|
|
}
|
|
|
|
function copy_string(should_fallback = false, input) {
|
|
const buffer = new Uint8Array(input.length);
|
|
fast_c_api.copy_string(should_fallback, input, buffer);
|
|
return buffer;
|
|
}
|
|
|
|
%PrepareFunctionForOptimization(copy_string);
|
|
assertSlowCall('Hello');
|
|
%OptimizeFunctionOnNextCall(copy_string);
|
|
|
|
fast_c_api.reset_counts();
|
|
assertFastCall('Hello');
|
|
assertFastCall('');
|
|
assertFastCall(['Hello', 'World'].join(''));
|
|
assertOptimized(copy_string);
|
|
assertEquals(3, fast_c_api.fast_call_count());
|
|
assertEquals(0, fast_c_api.slow_call_count());
|
|
|
|
// Fall back for twobyte strings.
|
|
fast_c_api.reset_counts();
|
|
assertSlowCall('Hello\u{10000}');
|
|
assertSlowCall('नमस्ते');
|
|
assertSlowCall(['नमस्ते', 'World'].join(''));
|
|
assertOptimized(copy_string);
|
|
assertEquals(0, fast_c_api.fast_call_count());
|
|
assertEquals(3, fast_c_api.slow_call_count());
|
|
|
|
// Fall back for cons strings.
|
|
function getTwoByteString() {
|
|
return '\u1234t';
|
|
}
|
|
function getCons() {
|
|
return 'hello' + getTwoByteString()
|
|
}
|
|
|
|
fast_c_api.reset_counts();
|
|
assertSlowCall(getCons());
|
|
assertOptimized(copy_string);
|
|
assertEquals(0, fast_c_api.fast_call_count());
|
|
assertEquals(1, fast_c_api.slow_call_count());
|
|
|
|
// Fall back for sliced strings.
|
|
fast_c_api.reset_counts();
|
|
function getSliced() {
|
|
return getCons().slice(1);
|
|
}
|
|
assertSlowCall(getSliced());
|
|
assertOptimized(copy_string);
|
|
assertEquals(0, fast_c_api.fast_call_count());
|
|
assertEquals(1, fast_c_api.slow_call_count());
|
|
|
|
// Fall back for SMI and non-string inputs.
|
|
fast_c_api.reset_counts();
|
|
assertSlowCall(1);
|
|
assertSlowCall({});
|
|
assertSlowCall(new Uint8Array(1));
|
|
assertEquals(0, fast_c_api.fast_call_count());
|
|
assertEquals(3, fast_c_api.slow_call_count());
|