Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
62 changes: 62 additions & 0 deletions benchmark/fs/readfile-utf8-fastpath.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
'use strict';

const common = require('../common.js');
const fs = require('fs');
const path = require('path');
const tmpdir = require('../../test/common/tmpdir');

const bench = common.createBenchmark(main, {
size: [64, 1024, 16384, 262144, 4194304],
content: ['ascii', 'latin1', 'utf8_mixed'],
source: ['path', 'fd'],
n: [3e3],
});

function buildContent(kind, size) {
if (kind === 'ascii') {
return Buffer.alloc(size, 0x61); // 'a'
}
if (kind === 'latin1') {
// 'é' in UTF-8 is 0xC3 0xA9 (2 bytes per char)
const pair = Buffer.from([0xC3, 0xA9]);
const buf = Buffer.alloc(size);
for (let i = 0; i + 2 <= size; i += 2) pair.copy(buf, i);
return buf;
}
if (kind === 'utf8_mixed') {
// mixed ASCII + 3-byte CJK (U+4E2D 中 = E4 B8 AD)
const cjk = Buffer.from([0xE4, 0xB8, 0xAD]);
const buf = Buffer.alloc(size);
let i = 0;
while (i + 4 <= size) {
buf[i++] = 0x61;
cjk.copy(buf, i);
i += 3;
}
return buf;
}
throw new Error('unknown content: ' + kind);
}

function main({ n, size, content, source }) {
tmpdir.refresh();
const file = path.join(tmpdir.path, `bench-${content}-${size}.bin`);
fs.writeFileSync(file, buildContent(content, size));

let arg;
let shouldClose = false;
if (source === 'fd') {
arg = fs.openSync(file, 'r');
shouldClose = true;
} else {
arg = file;
}

bench.start();
for (let i = 0; i < n; i++) {
fs.readFileSync(arg, 'utf8');
}
bench.end(n);

if (shouldClose) fs.closeSync(arg);
}
26 changes: 21 additions & 5 deletions benchmark/util/text-decoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -6,26 +6,42 @@ const bench = common.createBenchmark(main, {
encoding: ['utf-8', 'windows-1252', 'iso-8859-3'],
ignoreBOM: [0, 1],
fatal: [0, 1],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
content: ['ascii', 'one-byte-string', 'two-byte-string'],
len: [256, 1024 * 16, 1024 * 128],
n: [1e3],
type: ['SharedArrayBuffer', 'ArrayBuffer', 'Buffer'],
});

function main({ encoding, len, n, ignoreBOM, type, fatal }) {
function buildContent(content, len) {
let base;
switch (content) {
case 'ascii': base = 'a'; break;
case 'one-byte-string': base = '\xff'; break;
case 'two-byte-string': base = 'ğ'; break;
}
const unitBytes = Buffer.byteLength(base, 'utf8');
const copies = Math.max(1, Math.floor(len / unitBytes));
return Buffer.from(base.repeat(copies));
}

function main({ encoding, len, n, ignoreBOM, type, fatal, content }) {
const decoder = new TextDecoder(encoding, { ignoreBOM, fatal });
const seed = buildContent(content, len);
let buf;

switch (type) {
case 'SharedArrayBuffer': {
buf = new SharedArrayBuffer(len);
buf = new SharedArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'ArrayBuffer': {
buf = new ArrayBuffer(len);
buf = new ArrayBuffer(seed.length);
new Uint8Array(buf).set(seed);
break;
}
case 'Buffer': {
buf = Buffer.allocUnsafe(len);
buf = seed;
break;
}
}
Expand Down
7 changes: 4 additions & 3 deletions src/encoding_binding.cc
Original file line number Diff line number Diff line change
Expand Up @@ -459,14 +459,15 @@ void BindingData::DecodeUTF8(const FunctionCallbackInfo<Value>& args) {
return node::THROW_ERR_ENCODING_INVALID_ENCODED_DATA(
env->isolate(), "The encoded data was not valid for encoding utf-8");
}

// TODO(chalker): save on utf8 validity recheck in StringBytes::Encode()
}

if (length == 0) return args.GetReturnValue().SetEmptyString();

Local<Value> ret;
if (StringBytes::Encode(env->isolate(), data, length, UTF8).ToLocal(&ret)) {
v8::MaybeLocal<Value> encoded =
has_fatal ? StringBytes::EncodeValidUtf8(env->isolate(), data, length)
: StringBytes::Encode(env->isolate(), data, length, UTF8);
if (encoded.ToLocal(&ret)) {
args.GetReturnValue().Set(ret);
}
}
Expand Down
34 changes: 34 additions & 0 deletions src/string_bytes.cc
Original file line number Diff line number Diff line change
Expand Up @@ -671,6 +671,40 @@ MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
}
}

MaybeLocal<Value> StringBytes::EncodeValidUtf8(Isolate* isolate,
const char* buf,
size_t buflen) {
CHECK_BUFLEN_IN_RANGE(buflen);
if (!buflen) return String::Empty(isolate);
buflen = keep_buflen_in_range(buflen);

// ASCII fast path
if (!simdutf::validate_ascii_with_errors(buf, buflen).error) {
return ExternOneByteString::NewFromCopy(isolate, buf, buflen);
}

if (buflen >= 32) {
size_t u16size = simdutf::utf16_length_from_utf8(buf, buflen);
if (u16size > static_cast<size_t>(v8::String::kMaxLength)) {
isolate->ThrowException(ERR_STRING_TOO_LONG(isolate));
return MaybeLocal<Value>();
}
return EncodeTwoByteString(
isolate, u16size, [buf, buflen, u16size](uint16_t* dst) {
size_t written = simdutf::convert_valid_utf8_to_utf16(
buf, buflen, reinterpret_cast<char16_t*>(dst));
CHECK_EQ(written, u16size);
});
}

Local<String> str;
if (!String::NewFromUtf8(isolate, buf, v8::NewStringType::kNormal, buflen)
.ToLocal(&str)) {
isolate->ThrowException(node::ERR_STRING_TOO_LONG(isolate));
}
return str;
}

MaybeLocal<Value> StringBytes::Encode(Isolate* isolate,
const uint16_t* buf,
size_t buflen) {
Expand Down
5 changes: 5 additions & 0 deletions src/string_bytes.h
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,11 @@ class StringBytes {
size_t buflen,
enum encoding encoding);

// Like Encode(..., UTF8) but does not re-validate. Input must be valid UTF-8.
static v8::MaybeLocal<v8::Value> EncodeValidUtf8(v8::Isolate* isolate,
const char* buf,
size_t buflen);

// Warning: This reverses endianness on BE platforms, even though the
// signature using uint16_t implies that it should not.
// However, the brokenness is already public API and can't therefore
Expand Down
16 changes: 0 additions & 16 deletions src/util-inl.h
Original file line number Diff line number Diff line change
Expand Up @@ -341,22 +341,6 @@ v8::Maybe<void> FromV8Array(v8::Local<v8::Context> context,
return js_array->Iterate(context, PushItemToVector, &data);
}

v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,
v8::Isolate* isolate) {
if (isolate == nullptr) isolate = v8::Isolate::GetCurrent();
if (str.size() >= static_cast<size_t>(v8::String::kMaxLength)) [[unlikely]] {
// V8 only has a TODO comment about adding an exception when the maximum
// string size is exceeded.
ThrowErrStringTooLong(isolate);
return v8::MaybeLocal<v8::Value>();
}

return v8::String::NewFromUtf8(
isolate, str.data(), v8::NewStringType::kNormal, str.size())
.FromMaybe(v8::Local<v8::String>());
}

v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::u16string_view str,
v8::Isolate* isolate) {
Expand Down
11 changes: 11 additions & 0 deletions src/util.cc
Original file line number Diff line number Diff line change
Expand Up @@ -812,4 +812,15 @@ v8::Maybe<int> GetValidFileMode(Environment* env,
return v8::Just(mode);
}

v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,
v8::Isolate* isolate) {
if (isolate == nullptr) isolate = v8::Isolate::GetCurrent();
if (str.size() >= static_cast<size_t>(v8::String::kMaxLength)) [[unlikely]] {
ThrowErrStringTooLong(isolate);
return v8::MaybeLocal<v8::Value>();
}
return StringBytes::Encode(isolate, str.data(), str.size(), UTF8);
}

} // namespace node
6 changes: 3 additions & 3 deletions src/util.h
Original file line number Diff line number Diff line change
Expand Up @@ -701,9 +701,9 @@ inline v8::Maybe<void> FromV8Array(v8::Local<v8::Context> context,
v8::Local<v8::Array> js_array,
std::vector<v8::Global<v8::Value>>* out);

inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,
v8::Isolate* isolate = nullptr);
v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::string_view str,
v8::Isolate* isolate = nullptr);
inline v8::MaybeLocal<v8::Value> ToV8Value(v8::Local<v8::Context> context,
std::u16string_view str,
v8::Isolate* isolate = nullptr);
Expand Down
103 changes: 103 additions & 0 deletions test/parallel/test-fs-readfile-utf8-fast-path.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
'use strict';

require('../common');
const fs = require('node:fs');
const path = require('node:path');
const assert = require('node:assert');
const { describe, it } = require('node:test');
const tmpdir = require('../common/tmpdir');

tmpdir.refresh();

function writeFile(name, buf) {
const p = path.join(tmpdir.path, name);
fs.writeFileSync(p, buf);
return p;
}

function expectMatches(filePath, rawBuf) {
assert.strictEqual(
fs.readFileSync(filePath, 'utf8'),
rawBuf.toString('utf8'),
);
}

describe('fs.readFileSync utf8 simdutf dispatch', () => {
it('empty file', () => {
const p = writeFile('empty.txt', Buffer.alloc(0));
assert.strictEqual(fs.readFileSync(p, 'utf8'), '');
});

it('ascii small', () => {
const buf = Buffer.from('hello');
expectMatches(writeFile('tiny-ascii.txt', buf), buf);
});

it('ascii 20KB', () => {
const buf = Buffer.alloc(20 * 1024, 0x41);
expectMatches(writeFile('medium-ascii.txt', buf), buf);
});

it('ascii 1MB', () => {
const buf = Buffer.alloc(1024 * 1024, 0x61);
expectMatches(writeFile('large-ascii.txt', buf), buf);
});

it('fd input', () => {
const buf = Buffer.alloc(50 * 1024, 0x62);
const p = writeFile('fd-ascii.txt', buf);
const fd = fs.openSync(p, 'r');
try {
assert.strictEqual(fs.readFileSync(fd, 'utf8'), buf.toString('utf8'));
} finally {
fs.closeSync(fd);
}
});

it('multibyte UTF-8', () => {
const buf = Buffer.from('中文测试 — café — 🚀'.repeat(500), 'utf8');
expectMatches(writeFile('multibyte.txt', buf), buf);
});

it('latin1-fits utf8', () => {
const buf = Buffer.from('naïve café résumé — niño Köln '.repeat(500), 'utf8');
expectMatches(writeFile('latin1-fits.txt', buf), buf);
});

it('invalid: lone continuation byte', () => {
const buf = Buffer.from([0x68, 0x69, 0x80, 0x21]);
expectMatches(writeFile('invalid-cont.txt', buf), buf);
});

it('invalid: overlong', () => {
const buf = Buffer.from([0x41, 0xC0, 0xAF, 0x42]);
expectMatches(writeFile('invalid-overlong.txt', buf), buf);
});

it('invalid: surrogate', () => {
const buf = Buffer.from([0x41, 0xED, 0xA0, 0x80, 0x42]);
expectMatches(writeFile('invalid-surrogate.txt', buf), buf);
});

it('latin1 boundary U+00FF', () => {
const buf = Buffer.from('ÿ'.repeat(2048), 'utf8');
expectMatches(writeFile('latin1-boundary.txt', buf), buf);
});

it('above latin1 U+0100', () => {
const buf = Buffer.from('ĀāĂ'.repeat(1024), 'utf8');
expectMatches(writeFile('above-latin1.txt', buf), buf);
});

it('single codepoint each UTF-8 length', () => {
for (const cp of [0x41, 0x00E9, 0x4E2D, 0x1F600]) {
const buf = Buffer.from(String.fromCodePoint(cp), 'utf8');
expectMatches(writeFile(`single-cp-${cp.toString(16)}.txt`, buf), buf);
}
});

it('truncated multibyte at EOF', () => {
const buf = Buffer.from([0x41, 0xE4, 0xB8]);
expectMatches(writeFile('truncated-multibyte.txt', buf), buf);
});
});
Loading