Skip to content

Improve parse perf using bitwise flags - #76

Open
blakeembrey wants to merge 1 commit into
masterfrom
be/parse-perf
Open

Improve parse perf using bitwise flags#76
blakeembrey wants to merge 1 commit into
masterfrom
be/parse-perf

Conversation

@blakeembrey

Copy link
Copy Markdown
Member

Before:

 ✓ src/index.bench.ts > parse 17104ms
     name                                                       hz     min     max    mean     p75     p99    p995    p999     rme  samples
   · basic                                           16,113,888.97  0.0000  1.0510  0.0001  0.0001  0.0001  0.0002  0.0002  ±0.70%  8056945
   · simple parameters                                2,838,935.69  0.0002  0.3220  0.0004  0.0004  0.0004  0.0005  0.0009  ±0.40%  1419468
   · single parameter                                 5,456,431.11  0.0001  0.0475  0.0002  0.0002  0.0003  0.0003  0.0004  ±0.11%  2728216
   · uppercase single parameter                       5,453,110.96  0.0001  0.3120  0.0002  0.0002  0.0002  0.0003  0.0005  ±0.16%  2726556
   · accept header (comma = true)                     6,396,519.98  0.0000  0.0564  0.0002  0.0002  0.0002  0.0002  0.0003  ±0.09%  3198261
   · accept header (comma = false)                    3,673,513.63  0.0002  0.3119  0.0003  0.0003  0.0003  0.0004  0.0008  ±0.36%  1836757
   · simple quoted parameter                          5,080,187.10  0.0001  0.2890  0.0002  0.0002  0.0002  0.0003  0.0006  ±0.29%  2540094
   · simple parameters (options.parameters = false)  11,690,130.60  0.0000  0.3273  0.0001  0.0001  0.0001  0.0001  0.0002  ±0.18%  5845066
   · quoted and escaped parameters                    2,313,647.64  0.0003  0.2310  0.0004  0.0004  0.0005  0.0006  0.0035  ±0.31%  1156824
   · OWS-heavy parameters                             2,609,728.36  0.0002  0.2555  0.0004  0.0004  0.0005  0.0005  0.0009  ±0.29%  1304865
   · many parameters                                  1,805,848.65  0.0004  0.2545  0.0006  0.0005  0.0007  0.0007  0.0011  ±0.27%   902925
   · escape-heavy quoted parameters                   1,458,234.16  0.0005  0.2559  0.0007  0.0007  0.0008  0.0011  0.0014  ±0.36%   729118

After:

 ✓ src/index.bench.ts > parse 19407ms
     name                                                       hz     min      max    mean     p75     p99    p995    p999      rme   samples
   · basic                                           20,056,451.24  0.0000   0.0883  0.0000  0.0000  0.0001  0.0001  0.0002   ±0.10%  10028226
   · simple parameters                                3,340,430.35  0.0002   4.5476  0.0003  0.0003  0.0004  0.0004  0.0009   ±1.85%   1670216
   · single parameter                                 5,666,319.18  0.0000  12.1667  0.0002  0.0002  0.0003  0.0004  0.0005   ±6.64%   2833160
   · uppercase single parameter                       5,323,281.24  0.0001   0.4540  0.0002  0.0002  0.0003  0.0003  0.0009   ±0.35%   2661641
   · accept header (comma = true)                     7,068,431.77  0.0000   0.0619  0.0001  0.0002  0.0002  0.0002  0.0003   ±0.14%   3534216
   · accept header (comma = false)                    4,006,771.94  0.0001   0.4999  0.0002  0.0003  0.0003  0.0003  0.0007   ±0.59%   2003386
   · simple quoted parameter                          7,277,198.79  0.0000   0.0597  0.0001  0.0002  0.0002  0.0002  0.0003   ±0.13%   3638600
   · simple parameters (options.parameters = false)  16,085,461.42  0.0000  34.7025  0.0001  0.0001  0.0001  0.0001  0.0002  ±13.60%   8042731
   · quoted and escaped parameters                    2,676,170.14  0.0002   0.3357  0.0004  0.0004  0.0004  0.0005  0.0009   ±0.38%   1338086
   · OWS-heavy parameters                             3,052,145.49  0.0002   0.3206  0.0003  0.0003  0.0004  0.0004  0.0008   ±0.19%   1526073
   · many parameters                                  2,167,994.21  0.0003   0.3490  0.0005  0.0005  0.0006  0.0006  0.0017   ±0.20%   1083998
   · escape-heavy quoted parameters                   1,489,250.93  0.0005   0.4189  0.0007  0.0007  0.0008  0.0010  0.0017   ±0.46%    744626

I think I might be able to combine these flags with the validation logic to also improve that performance.

@codecov

codecov Bot commented Aug 24, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 100.00%. Comparing base (04b9688) to head (d7850d3).

Additional details and impacted files
@@            Coverage Diff            @@
##            master       #76   +/-   ##
=========================================
  Coverage   100.00%   100.00%           
=========================================
  Files            1         1           
  Lines           95       155   +60     
  Branches        20        40   +20     
=========================================
+ Hits            95       155   +60     

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 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.

Comment thread src/index.ts
* Skip over characters until a semicolon or other exit character.
* Remove backslashes from quoted pairs in a known-terminated quoted string body.
*/
function skipValue(

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Inlined skipValue, skipOWS and removing trailingOWS in favor of tracking the last whitespace showed marginal improvements to performance (a few % each).

Comment thread src/index.ts
continue;
}

value += String.fromCharCode(code);

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Unquoting seemed like a large perf improvement with the right conditions (most won't have escapes, turns into a single slice).

Comment thread src/index.ts
}
const valueEnd = whitespace === -1 ? index : whitespace;
const value = header.slice(valueStart, valueEnd);
const type = (typeFlags & CASE_FLAGS) === 0 ? value : value.toLowerCase();

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Skipping toLowerCase was the largest perf win when the value is already lower cased.

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant