Skip to content

[JSC] The URI functions return their argument when nothing changes, and build the result in one string otherwise - #839

Open
robobun wants to merge 1 commit into
mainfrom
robobun/c7a6e1be/uri-globals-identity-return
Open

robobun wants to merge 1 commit into
mainfrom
robobun/c7a6e1be/uri-globals-identity-return

Conversation

@robobun

@robobun robobun commented Oct 18, 2026

Copy link
Copy Markdown
Collaborator

Related to oven-sh/bun#45700.

Problem

  • encodeURIComponent("photos") costs 40 ns in bun against 17 ns in node. A 26 char value with 7 escapes costs 93 ns against 50. escape and unescape of a plain string cost 4 to 5x node.
  • The cause is in runtime/JSGlobalObjectFunctions.cpp. All six URI functions go through toStringView, append to a StringBuilder one code unit at a time, and make a new StringImpl and JSString from it. They do that also when the result has the argument's code units, which is the common case for a path piece or a query value.

Fix

  • Each function reads its argument through withStringArgument, which hands the callback the JSString. A scan runs first. When no code unit changes (encode: every unit is in the unescaped set, decode: no %), the function returns the argument. unchanged narrows a 16-bit argument whose code units fit in 8 bits, as the builder did, so a caller still reads an 8-bit result.
  • encode and escape count the output length in one pass, with the URIError checks of the spec at the same steps, and fill one StringImpl of that size. A length over StringImpl::MaxLength is the out-of-memory RangeError as before.
  • decode and unescape keep their parse. It starts at the first %. The code units before it are appended in one piece (8-bit input only: a bulk append of 16-bit units would make the builder 16-bit).
  • Verified: JSTests/stress/uri-globals-return-the-argument.js (new, 8-bit and 16-bit strings against V8's results, and the 8-bit representation of the result), the five existing URI stress tests, the updated out-of-memory-in-globalFuncUnescape.js, and the 208 test262 files under built-ins/{encodeURI,encodeURIComponent,decodeURI,decodeURIComponent} and annexB/built-ins/{escape,unescape} (208/208 before and after, with a one-off runner).

Background

  • toStringView (runtime/ParseInt.h) converts the argument and passes only a StringView to its callback. The callback cannot return the argument. parseInt and parseFloat keep using it.
  • A JSString that is a rope resolves in place in view(). The returned cell is the same cell. A substring rope is returned unresolved, which String.prototype.slice also does.
  • JSTests/stress/out-of-memory-in-globalFuncUnescape.js expected RangeError from unescape of 2^30 \u0100 with no %. That came from the builder's doubling on upconvert. The input now returns itself, and the file keeps an overflow case through encodeURIComponent (six code units per input unit). JSTests/BUN-TEST-DIFFERENCES.md row A10 records it.
  • Considered a count-then-fill rewrite of decode and unescape too: their count pass is the whole %XX parse, so the dense case would run it twice. Considered a bun-side override of the three encode globals in bun's global object table: a second encoder in the process, and node:vm contexts keep the slow one.

Downsides

  • A 16-bit argument that is returned as is still pays one StringImpl and one JSString for the narrowed copy, as before. A decodeURIComponent of 60 KB with an escape every 25 units is 6% slower in the bun build (88 to 94 us, node 57 us), and 5% faster in the jsc shell (80 to 75 us). The per unit parse is unchanged, so this is code layout.
  • encode and escape read an input with escapes twice. On 60 KB with 7 escapes per 26 units that is still 200 to 135 us. Text grows by 8.9 KB in bun's release binary (size, 103,175,513 to 103,184,409).
Measurements

Release builds of bun, linked against this commit (B) and against 87ee10da (A), node 26.3.0, linux x64 in a shared container, so the absolute numbers are high. /tmp/one2.mjs: 200k calls warm-up, median of 11 runs of 200k calls (2k for 60 KB), three interleaved processes per runtime. ns per call.

case A B node
encodeURIComponent "photos" 73 to 133 8.5 to 12 27 to 75
encodeURIComponent 28 char path piece 78 to 104 18 to 39 44 to 68
encodeURIComponent 26 chars, 7 escapes 159 to 187 64 to 84 74 to 75
encodeURIComponent "photos", 16-bit 57 to 78 7 to 12 26 to 27
encodeURIComponent "" 23 7.5 to 10.5 15
encodeURI "photos" 82 to 132 10 to 23 26 to 42
escape "photos" 81 to 83 10 to 12 13
escape 26 chars, 7 escapes 171 to 186 66 to 82 63 to 64
decodeURIComponent "photos" 76 to 81 5.9 to 7.6 50 to 62
decodeURIComponent 26 chars, 7 escapes 131 to 137 143 to 178 137 to 153
unescape "photos" 68 to 78 8.4 to 12 12 to 18
unescape 26 chars, 7 escapes 90 to 102 109 to 117 135 to 139
encodeURIComponent 60 KB plain (us) 122 to 127 22.6 to 24.1 49
encodeURIComponent 60 KB, escapes (us) 199 to 207 133 to 141 105 to 109
decodeURIComponent 60 KB plain (us) 115 to 118 0.45 to 0.49 41 to 43
decodeURIComponent 60 KB, escapes (us) 86 to 90 93 to 97 57 to 62

JSString cells per call of encodeURIComponent("photos"), 100k calls kept alive, bun:jsc heapStats().objectTypeCounts.string after a full GC: A 1.00, B 0.00.

jsc shell, JSTests/microbenchmarks: encode-uri-component-query-string.js 110 to 126 ms to 68 to 71 ms, encode-uri-component-unicode.js 95 to 100 ms to 50 to 54 ms, uri-globals-short-strings.js (new) 127 to 130 ms to 66 to 76 ms.

decodeURIComponent("a".repeat(2**29 + 1) + "%E2%82%AC"): RangeError: Out of memory on A, the decoded string on B (the prefix buffer is exact, not a doubled capacity). 2**30 fails on both: the builder's upconvert cap, #631.

Instruction and allocation counts are not measured: the container has no perf, valgrind or bloaty. The cell count stands in for the allocation count.

@github-actions

github-actions Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Preview build of 5ed1127: autobuild-preview-pr-839-5ed11277

@claude claude Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Nothing blocking. The comments below are optional suggestions. There is no need to push a fix for them before merging.

Beyond the inline findings, I also checked the count-then-fill rewrite of encode and escape: the per-code-unit output lengths (3/6/9/12 for UTF-8 octet counts, 3/6 for %XY/%uWXYZ) match what the fill pass writes, and the unchecked *++cursor after a lead surrogate in the fill pass is safe because the count pass already threw on a trailing or unpaired lead. The restructured unescape loop advances k by the same 3 and 6 as the old 8-bit and 16-bit branches, so the parse is unchanged.

Extended reasoning...

The change rewrites the six URI globals in Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp to return the argument JSString when nothing changes and to size the encode/escape result in one pass, plus a new stress test, a microbenchmark, an updated out-of-memory test and a BUN-TEST-DIFFERENCES.md row; it touches no auth, injection or data-exposure surface. Two confirmed inline findings (substring-rope retention from returning the argument, and the unescape/decode 16-bit overflow asymmetry) already signal a human look, so this note only records what else was examined and ruled out.

static JSValue unchanged(JSGlobalObject* globalObject, JSString* string, std::span<const CharacterType> characters)
{
if constexpr (sizeof(CharacterType) == 1)
return string;

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟡 (optional) Callers that store a URI function's result computed from a slice of a large string now keep that whole large string alive, which the base does not. unchanged() at JSGlobalObjectFunctions.cpp:83 returns the argument JSString itself, and for a substring rope view() leaves it unresolved, so the result still points at the base cell. Fix: when string->isSubstring(), return a fresh flat string built from characters (one copy, as the old builder made) and keep the identity return for non-substring strings only; the 16-bit return at line 86 needs the same. The author's note compares this to slice, but slice results are usually transient while decoded values are what gets stored.

Why this was flagged

Input: any string of 3 or more code units produced by String.prototype.split or slice, e.g. a form-body or query-string parser doing decodeURIComponent(part) for each part of body.split('&'). withStringArgument calls string->view() at JSGlobalObjectFunctions.cpp:69; for a substring rope JSRopeString::view at JSString.h:1281-1284 returns the base's view without resolving. When the part has no '%' (decode, line 237-239) or every unit is unescaped (encode line 121-122, escape line 729-730), unchanged() at line 83 (8-bit) or line 86 (16-bit non-Latin1) returns that same substring rope cell. A caller that keeps the decoded value now retains the entire source string for the lifetime of that value. On the base branch every result came from builder.toString() (old lines 138, 237, 668) or StringImpl creation, an independent flat string with no reference to the source, so the source was collectable once the parts were dropped. No safeguard exists: nothing in unchanged() checks isSubstring() or the base length.

Verification: unchanged() at JSGlobalObjectFunctions.cpp:83 does return string; for 8-bit input (and for 16-bit non-Latin1 input), so the returned value is the substring rope whose fiber1 is the base JSString (substringBase(), JSString.h:768). On the base commit, every path went through a StringBuilder and jsString(vm, builder.toString()), producing a fresh StringImpl independent of the argument's storage.

int length = static_cast<int>(characters.size());

StringBuilder builder(OverflowPolicy::RecordOverflow);
builder.reserveCapacity(length);

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

🟣 pre-existing, not blocking: Users calling unescape on a large 16-bit string get RangeError when it contains one '%', while the same string without '%' now succeeds. JSGlobalObjectFunctions.cpp:791 still does reserveCapacity(length) as 8-bit, and the first non-Latin1 unit appended at :813 upconverts to a doubled 16-bit buffer, which overflows for inputs near 2^30. The PR and A10 in JSTests/BUN-TEST-DIFFERENCES.md present the %-free case as fixed, leaving the sibling path with the old cap. Fix: for a 16-bit argument reserve the capacity in 16-bit (or skip reserveCapacity) so the builder does not double on upconvert, and cover the '%'-containing case in the updated test.
A small fix can ride a push you are already making; otherwise a short reply is enough.

Why this was flagged

The input is a 16-bit string of about 2^30 code units that contains at least one '%', passed to unescape, reaching the builder path at JSGlobalObjectFunctions.cpp:790-798. reserveCapacity(length) at :791 allocates an 8-bit buffer of length; the first append of a unit >= 0x100 at :813 calls extendBufferForAppendingWithUpconvert, which allocates a char16_t buffer of min(2*length, maxCapacity), so a 2^30 input asks for a ~4 GiB buffer and records overflow, giving RangeError: Out of memory at :818. The base branch behaved the same for this input, but it also threw for the %-free input; the change removes the throw only for the %-free case (:779-780), so after merging the outcome depends on whether the string happens to contain a '%'. The new test at JSTests/stress/out-of-memory-in-globalFuncUnescape.js:6-8 asserts the %-free case returns the argument and does not exercise the sibling. Remedy: avoid the 8-bit reserve for 16-bit input so the upconvert does not double, and add the '%' case to the test.

Verification: Pre-existing; acknowledged in diff. A 16-bit argument of about 2^30 code units that contains a '%' is passed to unescape. Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp:791 allocates an 8-bit buffer; the first unit >= 0x100 at :813 upconverts to a doubled char16_t buffer, and :817-818 throws the out-of-memory RangeError. The base branch fails this input by the same route; merging makes nothing worse.

@coderabbitai

coderabbitai Bot commented Oct 18, 2026 •

Copy link
Copy Markdown

Review in Change Stack →

Important

Review skipped

We couldn't safely recover the incremental review. No full review was started, and the last reviewed checkpoint was preserved. Retry later, or explicitly request a full review by commenting @coderabbitai full review.

You can disable this status message by setting the reviews.review_status to false in the CodeRabbit configuration file.

Use the checkbox below for a quick retry:

  • 🔍 Trigger review

No actionable comments were generated in the recent review. 🎉

ℹ️ Recent review info
⚙️ Run configuration
  • Configuration used: Organization UI
  • Review profile: ASSERTIVE
  • Plan: Advanced
  • Run ID: 8ed4c29d-76d3-43f2-b48f-14567c5c9fb4
📥 Commits

Reviewing files that changed from the base of the PR and between 87ee10d and 94dbd7b.

📒 Files selected for processing: 5
  • JSTests/BUN-TEST-DIFFERENCES.md
  • JSTests/microbenchmarks/uri-globals-short-strings.js
  • JSTests/stress/out-of-memory-in-globalFuncUnescape.js
  • JSTests/stress/uri-globals-return-the-argument.js
  • Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp

Included review availability: This review used your included allowance. Your plan provides up to 2 included reviews per hour; 0 remain after this review.

Walkthrough

URI encoding, decoding, escape, and unescape now process string spans and return unchanged inputs when applicable. The changes add direct output allocation for encoding paths, expanded stress tests, a microbenchmark, and documentation updates.

Changes

URI Global Functions

Layer / File(s) Summary
URI encoding and decoding
Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp, JSTests/stress/uri-globals-return-the-argument.js, JSTests/microbenchmarks/uri-globals-short-strings.js, JSTests/BUN-TEST-DIFFERENCES.md
Encoding and decoding use string spans and return unchanged inputs when applicable. Tests cover both string widths, coercion, errors, and large inputs. The benchmark exercises six URI functions; the documentation adds A10.
Legacy escape and unescape
Source/JavaScriptCore/runtime/JSGlobalObjectFunctions.cpp, JSTests/stress/out-of-memory-in-globalFuncUnescape.js
escape precomputes output length and writes into an exact-size allocation. unescape processes both string widths through one span-based loop. The stress test checks unchanged large input and an encodeURIComponent out-of-memory error.

Priority: ➖ Normal

Merge Risk: ⚪ Minimal · up to 94dbd

No concrete issue is established that would block merging. Complete normal build and test checks before merging.

Pre-merge checks: Passed 4
Check name Status Explanation
Title check Passed The title clearly summarizes the primary change: URI functions return the original argument when unchanged and build changed results efficiently.
Description check Passed The description is detailed, relevant, and covers the problem, implementation, validation, performance results, and trade-offs. It does not include the repository template's Bugzilla URL, Reviewed by …
Linked Issues check Passed Check skipped because no linked issues were found for this pull request.
Out of Scope Changes check Passed Check skipped because no linked issues were found for this pull request.
Full details: Description check

Explanation

The description is detailed, relevant, and covers the problem, implementation, validation, performance results, and trade-offs. It does not include the repository template's Bugzilla URL, Reviewed by line, or explicit path/function list.

  • Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts

Warning

Git: CodeRabbit could not clone the repository, so clone-backed analysis was skipped and this review may be incomplete. Verify repository clone access, such as SSH credentials, before requesting another full review. If clone access is intentionally unavailable, use path_filters to narrow the review scope.

Comment @coderabbitai help to get the list of available commands.

…nd build the result in one string otherwise

encodeURI, encodeURIComponent, escape, decodeURI, decodeURIComponent and unescape built every result in a StringBuilder, one append per code unit, and made a new StringImpl and JSString for it, also when the result had the argument's code units. encodeURIComponent of a short plain string cost about twice what V8 charges.

Each function now scans first. When no code unit changes, it returns the argument JSString (a 16-bit argument whose code units fit in 8 bits is narrowed, as the builder did). encode and escape then count the output length in one pass, with the URIError checks of the spec in the same places, and fill one StringImpl of that size. decode and unescape keep their parse. It starts at the first '%', with the code units before it appended in one piece.

JSTests/stress/out-of-memory-in-globalFuncUnescape.js: unescape of a '%'-free string is that string, so the overflow case moves to encodeURIComponent. JSTests/stress/uri-globals-return-the-argument.js pins the results for 8-bit and 16-bit strings against V8's. JSTests/microbenchmarks/uri-globals-short-strings.js times the six functions on short strings.
@robobun
robobun force-pushed the robobun/c7a6e1be/uri-globals-identity-return branch from 187ae6b to 5ed1127 Compare October 22, 2026 13:16
robobun added a commit to oven-sh/bun that referenced this pull request Oct 23, 2026
…eturn their argument when nothing changes

Adds test/js/bun/jsc/uri-globals.test.ts and bench/snippets/encode-uri.mjs.
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