Skip to content

Conversation

@anonrig
Copy link
Member

@anonrig anonrig commented Feb 11, 2026

In an attempt to improve performance, and reduce the code bloat in internal/util.js... Pending benchmark CI.

Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1798/

@nodejs-github-bot nodejs-github-bot added c++ Issues and PRs that require attention from people who are familiar with C++. encoding Issues and PRs related to the TextEncoder and TextDecoder APIs. needs-ci PRs that need a full CI run. labels Feb 11, 2026
@codecov
Copy link

codecov bot commented Feb 11, 2026

Codecov Report

❌ Patch coverage is 74.31907% with 66 lines in your changes missing coverage. Please review.
✅ Project coverage is 89.74%. Comparing base (3819c7f) to head (57c45d0).
⚠️ Report is 18 commits behind head on main.

Files with missing lines Patch % Lines
src/encoding_binding.cc 74.21% 51 Missing and 15 partials ⚠️
Additional details and impacted files
@@           Coverage Diff            @@
##             main   #61778    +/-   ##
========================================
  Coverage   89.74%   89.74%            
========================================
  Files         675      675            
  Lines      204642   204761   +119     
  Branches    39322    39328     +6     
========================================
+ Hits       183657   183767   +110     
- Misses      13257    13264     +7     
- Partials     7728     7730     +2     
Files with missing lines Coverage Δ
lib/internal/encoding.js 100.00% <100.00%> (ø)
src/encoding_binding.cc 63.01% <74.21%> (+10.09%) ⬆️

... and 42 files with indirect coverage changes

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
  • 📦 JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.

@anonrig anonrig force-pushed the yagiz/move-text-encoder branch from b45c314 to 010d87f Compare February 11, 2026 21:22
@anonrig anonrig force-pushed the yagiz/move-text-encoder branch from 010d87f to eb0a347 Compare February 12, 2026 00:29
@anonrig anonrig force-pushed the yagiz/move-text-encoder branch from eb0a347 to 57c45d0 Compare February 12, 2026 01:04
@anonrig
Copy link
Member Author

anonrig commented Feb 12, 2026

Benchmarks are not good... Closing.

@anonrig anonrig closed this Feb 12, 2026
@anonrig anonrig deleted the yagiz/move-text-encoder branch February 12, 2026 01:49
Local<String> source = args[0].As<String>();
source->WriteUtf8V2(isolate,
static_cast<char*>(bs->Data()),
bs->MaxByteLength(),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
bs->MaxByteLength(),
bs->ByteLength(),

This is not a resizable array buffer, so no need to use this.

Comment on lines +246 to +248
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, output_length, BackingStoreInitializationMode::kUninitialized);
CHECK(bs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think we should also handle the error here.

Suggested change
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, output_length, BackingStoreInitializationMode::kUninitialized);
CHECK(bs);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, output_length, BackingStoreInitializationMode::kUninitialized,
BackingStoreOnFailureMode::kReturnNull);
if (!bs) [[unlikely]] {
THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
return MaybeLocal<Uint8Array>();
}

Comment on lines +286 to +288
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, utf8_length, BackingStoreInitializationMode::kUninitialized);
CHECK(bs);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, utf8_length, BackingStoreInitializationMode::kUninitialized);
CHECK(bs);
std::unique_ptr<BackingStore> bs = ArrayBuffer::NewBackingStore(
isolate, utf8_length, BackingStoreInitializationMode::kUninitialized,
BackingStoreOnFailureMode::kReturnNull);
if (!bs) [[unlikely]] {
THROW_ERR_MEMORY_ALLOCATION_FAILED(isolate);
return MaybeLocal<Uint8Array>();
}

} else {
conversion_buffer.AllocateSufficientStorage(length);
conversion_buffer.SetLength(length);
simdutf::to_well_formed_utf16(data, length, conversion_buffer.out());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this case is there really much to be gained from this juggling compared to just using WriteUtf8V2?

// Use a cached ObjectTemplate so every result shares the same hidden class
// (map). This gives V8 fast-properties objects with inline storage, unlike
// DictionaryTemplate which creates slow dictionary-mode objects.
Local<Context> context = env->context();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think this will make it even slower than dictionary objects? Dictionary is only slow in comparison to fast properties whose reads get compiled into a memory offset read. If it's in comparison to jumping through the hoops to invoke a C++ accessor it's still faster.

text_encoder->SetClassName(FIXED_ONE_BYTE_STRING(isolate, "TextEncoder"));
Local<v8::Signature> signature = v8::Signature::New(isolate, text_encoder);

Local<FunctionTemplate> encode =
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You can just use the SetProtoMethodNoSideEffect helpers for these methods? If you need to set length you can just add a new parameter to that.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

c++ Issues and PRs that require attention from people who are familiar with C++. encoding Issues and PRs related to the TextEncoder and TextDecoder APIs. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants