Skip to content

Editorial: qualify claim about TA length changing during sort - #4043

Open
bakkot wants to merge 2 commits into
tc39:mainfrom
bakkot:ta-sort-length-note
Open

bakkot wants to merge 2 commits into
tc39:mainfrom
bakkot:ta-sort-length-note

Conversation

@bakkot

@bakkot bakkot commented Oct 29, 2026

Copy link
Copy Markdown
Member

This note became incorrect with the introduction of resizable ArrayBuffers, though arguably it was already incorrect given ArrayBuffer detaching. As reported in #4013 by @JinDX.

I think the "no user code" note is useful; the UC annotation doesn't suffice because it will tell you that SortIndexedProperties can call user code - which it can, but only by calling the user-provided comparator. That really can change the length of the TA during execution although the algorithm mostly doesn't care (it does all the reads before calling the comparator, and the trailing writes just don't happen if the TA ends up too small for them).

I'm open to dropping the "and the TypedArray's length cannot decrease" part since it's a little confusing in light of the up-front read.

@bakkot

bakkot commented Oct 29, 2026

Copy link
Copy Markdown
Member Author

(Why do we even have this argument? What other way are you sorting the contents of a TypedArray?)

@github-actions

Copy link
Copy Markdown

The rendered spec preview for this PR is available as a single page at https://tc39.es/ecma262/pr/4043 and as multiple pages at https://tc39.es/ecma262/pr/4043/multipage .

Comment thread spec.html Outdated
<emu-clause id="sec-%typedarray%.prototype.sort" oldids="sec-typedarraysortcompare" type="built-in function">
<h1>%TypedArray%.prototype.sort ( _comparator_ )</h1>
<p>This is a distinct method that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in <emu-xref href="#sec-array.prototype.sort"></emu-xref>. The implementation of this method may be optimized with the knowledge that the *this* value is an object that has a fixed length and whose integer-indexed properties are not sparse.</p>
<p>This is a distinct method that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in <emu-xref href="#sec-array.prototype.sort"></emu-xref>. The implementation of this method may be optimized with the knowledge that the *this* value is an object whose integer-indexed properties are not sparse. If _comparator_ is *undefined*, no user code is executed during this algorithm, and the TypedArray's length cannot decrease.</p>

@nicolo-ribaudo nicolo-ribaudo Oct 30, 2026 •

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Suggested change
<p>This is a distinct method that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in <emu-xref href="#sec-array.prototype.sort"></emu-xref>. The implementation of this method may be optimized with the knowledge that the *this* value is an object whose integer-indexed properties are not sparse. If _comparator_ is *undefined*, no user code is executed during this algorithm, and the TypedArray's length cannot decrease.</p>
<p>This is a distinct method that, except as described below, implements the same requirements as those of `Array.prototype.sort` as defined in <emu-xref href="#sec-array.prototype.sort"></emu-xref>. The implementation of this method may be optimized with the knowledge that the *this* value is an object whose integer-indexed properties are not sparse. If _comparator_ is *undefined*, no user code is executed by this algorithm, and the TypedArray's length cannot decrease.</p>

Since user code from other threads could still mutate the array values if it's baked by a SharedArrayBuffer, right? (which is why you added that the length cannot decrease).

(Why do we even have this argument? What other way are you sorting the contents of a TypedArray?)

Clearly for people that prefer lexicographic order!

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.

Done.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants