buffer: add fast api for isUtf8 and isAscii

Signed-off-by: Gürgün Dayıoğlu <hey@gurgun.day>
PR-URL: https://github.com/nodejs/node/pull/64169
Reviewed-By: Daniel Lemire <daniel@lemire.me>
Reviewed-By: Matteo Collina <matteo.collina@gmail.com>
Reviewed-By: Anna Henningsen <anna@addaleax.net>
This commit is contained in:
Gürgün Dayıoğlu authored and Antoine du Hamel committed 2026-07-30 13:51:47 +02:00
1 parent d3073a7ba6
commit 081c41eb86
2 files changed
+102 -9

No files matched your search

+68 -9
View File
@@ -1261,19 +1261,58 @@ void FastSwap64(Local<Value> receiver,
static CFunction fast_swap64(CFunction::Make(FastSwap64));
struct ValidationResult {
bool is_valid;
bool was_detached;
};
static ValidationResult ValidateUtf8(Local<Value> value) {
ArrayBufferViewContents<char> abv(value);
bool was_detached = abv.WasDetached();
return {!was_detached && simdutf::validate_utf8(abv.data(), abv.length()),
was_detached};
}
static void IsUtf8(const FunctionCallbackInfo<Value>& args) {
Environment* env = Environment::GetCurrent(args);
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());
ArrayBufferViewContents<char> abv(args[0]);
if (abv.WasDetached()) {
const ValidationResult result = ValidateUtf8(args[0]);
if (result.was_detached) {
return node::THROW_ERR_INVALID_STATE(
env, "Cannot validate on a detached buffer");
}
args.GetReturnValue().Set(simdutf::validate_utf8(abv.data(), abv.length()));
args.GetReturnValue().Set(result.is_valid);
}
static bool FastIsUtf8(Local<Value> receiver,
Local<Value> value,
// NOLINTNEXTLINE(runtime/references)
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("buffer.isUtf8");
HandleScope scope(options.isolate);
const ValidationResult result = ValidateUtf8(value);
if (result.was_detached) {
node::THROW_ERR_INVALID_STATE(options.isolate,
"Cannot validate on a detached buffer");
return false;
}
return result.is_valid;
}
static CFunction fast_is_utf8(CFunction::Make(FastIsUtf8));
static ValidationResult ValidateAscii(Local<Value> value) {
ArrayBufferViewContents<char> abv(value);
bool was_detached = abv.WasDetached();
return {
!was_detached &&
!simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error,
was_detached};
}
static void IsAscii(const FunctionCallbackInfo<Value>& args) {
@@ -1281,17 +1320,34 @@ static void IsAscii(const FunctionCallbackInfo<Value>& args) {
CHECK_EQ(args.Length(), 1);
CHECK(args[0]->IsTypedArray() || args[0]->IsArrayBuffer() ||
args[0]->IsSharedArrayBuffer());
ArrayBufferViewContents<char> abv(args[0]);
if (abv.WasDetached()) {
const ValidationResult result = ValidateAscii(args[0]);
if (result.was_detached) {
return node::THROW_ERR_INVALID_STATE(
env, "Cannot validate on a detached buffer");
}
args.GetReturnValue().Set(
!simdutf::validate_ascii_with_errors(abv.data(), abv.length()).error);
args.GetReturnValue().Set(result.is_valid);
}
static bool FastIsAscii(Local<Value> receiver,
Local<Value> value,
// NOLINTNEXTLINE(runtime/references)
FastApiCallbackOptions& options) {
TRACK_V8_FAST_API_CALL("buffer.isAscii");
HandleScope scope(options.isolate);
const ValidationResult result = ValidateAscii(value);
if (result.was_detached) {
node::THROW_ERR_INVALID_STATE(options.isolate,
"Cannot validate on a detached buffer");
return false;
}
return result.is_valid;
}
static CFunction fast_is_ascii(CFunction::Make(FastIsAscii));
void SetBufferPrototype(const FunctionCallbackInfo<Value>& args) {
Realm* realm = Realm::GetCurrent(args);
@@ -1663,8 +1719,9 @@ void Initialize(Local<Object> target,
SetFastMethod(context, target, "swap32", Swap32, &fast_swap32);
SetFastMethod(context, target, "swap64", Swap64, &fast_swap64);
SetMethodNoSideEffect(context, target, "isUtf8", IsUtf8);
SetMethodNoSideEffect(context, target, "isAscii", IsAscii);
SetFastMethodNoSideEffect(context, target, "isUtf8", IsUtf8, &fast_is_utf8);
SetFastMethodNoSideEffect(
context, target, "isAscii", IsAscii, &fast_is_ascii);
target
->Set(context,
@@ -1737,7 +1794,9 @@ void RegisterExternalReferences(ExternalReferenceRegistry* registry) {
registry->Register(fast_swap64);
registry->Register(IsUtf8);
registry->Register(fast_is_utf8);
registry->Register(IsAscii);
registry->Register(fast_is_ascii);
registry->Register(StringSlice<ASCII>);
registry->Register(StringSlice<BASE64>);
@@ -0,0 +1,34 @@
// Flags: --expose-internals --no-warnings --allow-natives-syntax
'use strict';
const common = require('../common');
const assert = require('assert');
const { Buffer, isAscii, isUtf8 } = require('buffer');
const ascii = Buffer.from('hello');
const utf8 = Buffer.from('hello \xc4\x9f');
function testFastIsAscii() {
assert.strictEqual(isAscii(ascii), true);
}
function testFastIsUtf8() {
assert.strictEqual(isUtf8(utf8), true);
}
eval('%PrepareFunctionForOptimization(isAscii)');
testFastIsAscii();
eval('%OptimizeFunctionOnNextCall(isAscii)');
testFastIsAscii();
eval('%PrepareFunctionForOptimization(isUtf8)');
testFastIsUtf8();
eval('%OptimizeFunctionOnNextCall(isUtf8)');
testFastIsUtf8();
if (common.isDebug) {
const { internalBinding } = require('internal/test/binding');
const { getV8FastApiCallCount } = internalBinding('debug');
assert.strictEqual(getV8FastApiCallCount('buffer.isAscii'), 1);
assert.strictEqual(getV8FastApiCallCount('buffer.isUtf8'), 1);
}