New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid repeated ToNumber calls in %TypedArray%.prototype.fill() #855

Closed
camillobruni opened this Issue Mar 22, 2017 · 1 comment

Comments

Projects
None yet
2 participants
@camillobruni

camillobruni commented Mar 22, 2017

Example:
%TypedArray%.prototype.fill(proxy) causes a ToNumber with side-effects on every fill step.

From the usual behavior of Array.prototype functions, the argument conversion should probably happen only once and in this case would be different for TypedArrays. Obviously this only an issue with non-number fill values.

Array.prototype.fill and %TypedArray%.prototype.fill share the same spec implementation, modulo getting the array length. The TypedArray version should probably introduce an additional step performing a ToNumber(value) before step 3 in 22.1.3.6 to avoid the frequent conversion inside the fill step 7.b.

Steps:
The following steps lead to the ToNumber in question when calling %TypedArray%.prototype.fill.

littledan added a commit to littledan/test262 that referenced this issue Mar 22, 2017

Test for TypedArray.prototype.fill semantics change
The change is proposed in tc39/ecma262#856
as a fix to tc39/ecma262#855

Here, the ToNumber coercion is done only once, rather than on each
iteration. It does not appear that there were previously any
tests against repeated coercion for this parameter previously.

Tested this test against V8, which failed, as V8 implements the
current spec rather than the proposed one.
@littledan

This comment has been minimized.

Show comment
Hide comment
@littledan

littledan Mar 22, 2017

Member

I wrote up a spec patch with new semantics with only a single coercion, and tests to go along with it. I see a couple advantages to a single coercion:

  • It's cleaner semantics. Generally, JavaScript standard library functions tend to coerce things in the beginning of functions once, rather than repeatedly.
  • It simplifies implementing an optimized version of TypedArray.prototype.fill. Ideally, we should be able to use memset here under the hood. However, the interspersed coercions with Set mean that implementations would have to include both a memset path which is invoked when value is already a Number, and a "slow path" which will take things more literally to handle, e.g., the underlying ArrayBuffer being detached halfway through in the coercion.
Member

littledan commented Mar 22, 2017

I wrote up a spec patch with new semantics with only a single coercion, and tests to go along with it. I see a couple advantages to a single coercion:

  • It's cleaner semantics. Generally, JavaScript standard library functions tend to coerce things in the beginning of functions once, rather than repeatedly.
  • It simplifies implementing an optimized version of TypedArray.prototype.fill. Ideally, we should be able to use memset here under the hood. However, the interspersed coercions with Set mean that implementations would have to include both a memset path which is invoked when value is already a Number, and a "slow path" which will take things more literally to handle, e.g., the underlying ArrayBuffer being detached halfway through in the coercion.

leobalter added a commit to tc39/test262 that referenced this issue Mar 27, 2017

Test for TypedArray.prototype.fill semantics change (#927)
The change is proposed in tc39/ecma262#856
as a fix to tc39/ecma262#855

Here, the ToNumber coercion is done only once, rather than on each
iteration. It does not appear that there were previously any
tests against repeated coercion for this parameter previously.

Tested this test against V8, which failed, as V8 implements the
current spec rather than the proposed one.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment