Skip to content

buffer: speed up hex decoding - #66174

Open
anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:cursor/buffer-hotpath-ead2
Open

anonrig wants to merge 2 commits into
nodejs:mainfrom
anonrig:cursor/buffer-hotpath-ead2

Conversation

@anonrig

@anonrig anonrig commented Sep 21, 2026

Copy link
Copy Markdown
Member

Buffer.from(hex, 'hex') and buf.write(hex, 'hex') both go through StringBytes::Write and the scalar nbytes::HexDecode loop. Valid hex is the common case, so decode four output bytes per iteration and keep the existing “stop at the first invalid nibble” behavior (odd trailing nibble still ignored).

This change was implemented with assistance from a closed-source coding agent. I verified the source, official benchmark/buffers/buffer-hex-encode.js results, and the buffer hex tests listed below.

Official benches

benchmark/buffers/buffer-hex-encode.js (Buffer.from(hex, 'hex')), three runs each, median rate:

len main this PR delta
64 9.34M 10.08M +8%
1024 1.23M 1.54M +25%

Tests

test-buffer-badhex, test-buffer-from, test-buffer-write, test-buffer-write-fast, test-buffer-tostring, test-buffer-tostring-range.

Unroll StringBytes hex decode so the common valid-input case
processes four output bytes per iteration, while still stopping at
the first invalid nibble.

Official benchmark/buffers/buffer-hex-encode.js (Buffer.from hex):
len=1024 is about 25% faster and len=64 is about 8% faster.

Assisted-by: a closed-source coding agent
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>
Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
@nodejs-github-bot nodejs-github-bot added buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. needs-ci PRs that need a full CI run. labels Sep 21, 2026
Keep the invalid-nibble early exits on one line so format-cpp
matches the project's clang-format.

Assisted-by: a closed-source coding agent
Signed-off-by: Yagiz Nizipli <yagiz@nizipli.com>

Co-authored-by: Yagiz Nizipli <anonrig@users.noreply.github.com>
@codecov

codecov Bot commented Sep 21, 2026

Copy link
Copy Markdown

Codecov Report

❌ Patch coverage is 90.47619% with 4 lines in your changes missing coverage. Please review.
✅ Project coverage is 90.29%. Comparing base (dd5dfb5) to head (6004dda).
⚠️ Report is 38 commits behind head on main.

Files with missing lines Patch % Lines
src/string_bytes.cc 90.47% 0 Missing and 4 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main   #66174      +/-   ##
==========================================
+ Coverage   90.27%   90.29%   +0.02%     
==========================================
  Files         790      790              
  Lines      271651   272080     +429     
  Branches    51842    51941      +99     
==========================================
+ Hits       245228   245684     +456     
+ Misses      16928    16899      -29     
- Partials     9495     9497       +2     
Files with missing lines Coverage Δ
src/string_bytes.cc 76.80% <90.47%> (+1.05%) ⬆️

... and 70 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.

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

Labels

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants