Skip to content

src: use stack allocation in indexOf latin1 path#62268

Open
mertcanaltin wants to merge 1 commit intonodejs:mainfrom
mertcanaltin:mert/buffer-indexof-stack-alloc
Open

src: use stack allocation in indexOf latin1 path#62268
mertcanaltin wants to merge 1 commit intonodejs:mainfrom
mertcanaltin:mert/buffer-indexof-stack-alloc

Conversation

@mertcanaltin
Copy link
Member

@mertcanaltin mertcanaltin commented Mar 15, 2026

Replaced malloc / free with MaybeStackBuffer in the Latin1 path of IndexOfString.
For short needles, the allocation stays on the stack, avoiding heap allocation.
This results in a 12-15% improvement for short string searches.

➜  node git:(mert/buffer-indexof-stack-alloc) ✗ node-benchmark-compare ./result.csv
                                                                                                                                                confidence improvement accuracy (*)    (**)   (***)
buffers/buffer-indexof-number.js n=1000000 value=64                                                                                                      *      1.55 %       ±1.36%  ±1.87%  ±2.57%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='--l'                                                                               0.27 %       ±1.13%  ±1.57%  ±2.17%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='@'                                                                                 1.72 %       ±2.49%  ±3.42%  ±4.68%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='</i> to the Caterpillar'                                                           0.09 %       ±0.31%  ±0.43%  ±0.59%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='aaaaaaaaaaaaaaaaa'                                                                 5.67 %      ±12.05% ±17.30% ±25.40%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='Alice'                                                                            -0.13 %       ±2.35%  ±3.23%  ±4.41%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='found it very'                                                                    -0.01 %       ±0.55%  ±0.76%  ±1.04%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='Gryphon'                                                                          -0.56 %       ±1.41%  ±1.95%  ±2.70%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='neighbouring pool'                                                                10.17 %      ±24.08% ±34.59% ±50.85%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='Ou est ma chatte?'                                                                -0.04 %       ±0.53%  ±0.73%  ±0.99%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='SQ'                                                                         *      1.19 %       ±1.18%  ±1.62%  ±2.24%
buffers/buffer-indexof.js n=50000 type='buffer' encoding='undefined' search='venture to go near the house till she had brought herself down to'                 0.22 %       ±0.72%  ±0.99%  ±1.34%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='--l'                                                                           *      1.76 %       ±1.42%  ±2.01%  ±2.86%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='@'                                                                           ***     12.51 %       ±1.72%  ±2.36%  ±3.23%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='</i> to the Caterpillar'                                                             -0.01 %       ±0.32%  ±0.44%  ±0.60%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='aaaaaaaaaaaaaaaaa'                                                                   -0.65 %       ±2.15%  ±2.98%  ±4.14%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='Alice'                                                                       ***     15.69 %       ±2.20%  ±3.02%  ±4.13%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='found it very'                                                                       -0.02 %       ±0.97%  ±1.39%  ±2.02%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='Gryphon'                                                                       *      1.90 %       ±1.71%  ±2.35%  ±3.21%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='neighbouring pool'                                                                   -0.01 %       ±0.58%  ±0.80%  ±1.11%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='Ou est ma chatte?'                                                                    0.05 %       ±1.34%  ±1.89%  ±2.71%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='SQ'                                                                            *      1.46 %       ±1.37%  ±1.89%  ±2.57%
buffers/buffer-indexof.js n=50000 type='string' encoding='latin1' search='venture to go near the house till she had brought herself down to'                   -0.06 %       ±0.75%  ±1.07%  ±1.52%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='--l'                                                                                   -0.09 %       ±1.19%  ±1.63%  ±2.23%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='@'                                                                                      0.28 %       ±1.60%  ±2.23%  ±3.15%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='</i> to the Caterpillar'                                                                0.25 %       ±0.33%  ±0.46%  ±0.65%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='aaaaaaaaaaaaaaaaa'                                                                      0.57 %       ±2.04%  ±2.83%  ±3.95%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='Alice'                                                                                  1.94 %       ±2.44%  ±3.35%  ±4.57%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='found it very'                                                                         -0.38 %       ±0.92%  ±1.29%  ±1.84%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='Gryphon'                                                                               -0.34 %       ±1.35%  ±1.85%  ±2.53%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='neighbouring pool'                                                                      0.08 %       ±0.36%  ±0.50%  ±0.70%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='Ou est ma chatte?'                                                                      0.58 %       ±0.79%  ±1.11%  ±1.55%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='SQ'                                                                                    -0.16 %       ±1.32%  ±1.83%  ±2.51%
buffers/buffer-indexof.js n=50000 type='string' encoding='ucs2' search='venture to go near the house till she had brought herself down to'                     -0.12 %       ±0.97%  ±1.33%  ±1.83%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='--l'                                                                                   -0.96 %       ±1.28%  ±1.75%  ±2.39%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='@'                                                                                     -0.20 %       ±1.83%  ±2.53%  ±3.47%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='</i> to the Caterpillar'                                                               -0.13 %       ±0.40%  ±0.55%  ±0.76%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='aaaaaaaaaaaaaaaaa'                                                                     -1.16 %       ±1.51%  ±2.07%  ±2.83%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='Alice'                                                                                  0.37 %       ±2.71%  ±3.74%  ±5.18%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='found it very'                                                                         -0.17 %       ±1.09%  ±1.50%  ±2.04%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='Gryphon'                                                                               -0.22 %       ±1.46%  ±2.01%  ±2.77%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='neighbouring pool'                                                                     -0.06 %       ±0.37%  ±0.51%  ±0.69%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='Ou est ma chatte?'                                                                      1.16 %       ±1.86%  ±2.65%  ±3.85%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='SQ'                                                                                     1.60 %       ±2.32%  ±3.25%  ±4.62%
buffers/buffer-indexof.js n=50000 type='string' encoding='utf8' search='venture to go near the house till she had brought herself down to'                     -0.64 %       ±0.78%  ±1.09%  ±1.53%

Be aware that when doing many comparisons the risk of a false-positive result increases.
In this case, there are 45 comparisons, you can thus expect the following amount of false-positive results:
  2.25 false positives, when considering a   5% risk acceptance (*, **, ***),
  0.45 false positives, when considering a   1% risk acceptance (**, ***),
  0.04 false positives, when considering a 0.1% risk acceptance (***)
➜  node git:(mert/buffer-indexof-stack-alloc) ✗ 

@nodejs-github-bot
Copy link
Collaborator

Review requested:

  • @nodejs/performance

@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 Mar 15, 2026
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

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

lgtm

@mcollina mcollina added request-ci Add this label to start a Jenkins CI on a PR. needs-benchmark-ci PR that need a benchmark CI run. labels Mar 15, 2026
@mertcanaltin mertcanaltin marked this pull request as ready for review March 15, 2026 21:47
@github-actions github-actions bot removed the request-ci Add this label to start a Jenkins CI on a PR. label Mar 15, 2026
@nodejs-github-bot
Copy link
Collaborator

@codecov
Copy link

codecov bot commented Mar 15, 2026

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 89.69%. Comparing base (82409af) to head (a95b66f).
⚠️ Report is 6 commits behind head on main.

Additional details and impacted files
@@            Coverage Diff             @@
##             main   #62268      +/-   ##
==========================================
+ Coverage   89.67%   89.69%   +0.01%     
==========================================
  Files         676      676              
  Lines      206555   206552       -3     
  Branches    39552    39559       +7     
==========================================
+ Hits       185225   185258      +33     
+ Misses      13448    13438      -10     
+ Partials     7882     7856      -26     
Files with missing lines Coverage Δ
src/node_buffer.cc 68.11% <100.00%> (+0.10%) ⬆️

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

@nodejs-github-bot
Copy link
Collaborator

@addaleax addaleax added the author ready PRs that have at least one approval, no pending requests for changes, and a CI started. label Mar 15, 2026
@addaleax addaleax added the commit-queue Add this label to land a pull request using GitHub Actions. label Mar 16, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

author ready PRs that have at least one approval, no pending requests for changes, and a CI started. buffer Issues and PRs related to the buffer subsystem. c++ Issues and PRs that require attention from people who are familiar with C++. commit-queue Add this label to land a pull request using GitHub Actions. needs-benchmark-ci PR that need a benchmark CI run. needs-ci PRs that need a full CI run.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

7 participants