Skip to content
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

Replace all u64/i64 for length, index or offsets into usize/isize #4083

Draft
wants to merge 6 commits into
base: main
Choose a base branch
from

Conversation

hansl
Copy link
Contributor

@hansl hansl commented Dec 11, 2024

On platforms that are 32-bits (or 128?) this should make array and iterations faster in general. On platforms that are 64-bits, this will have no effect.

On platforms that are 32-bits (or 128?) this should make array and
iterations faster in general. On platforms that are 64-bits, this
will have no effect.
Copy link

codecov bot commented Dec 11, 2024

Codecov Report

Attention: Patch coverage is 43.57143% with 79 lines in your changes missing coverage. Please review.

Project coverage is 53.34%. Comparing base (6ddc2b4) to head (442bc60).
Report is 319 commits behind head on main.

Files with missing lines Patch % Lines
core/engine/src/builtins/typed_array/builtin.rs 12.90% 27 Missing ⚠️
core/engine/src/builtins/regexp/mod.rs 41.17% 10 Missing ⚠️
core/engine/src/builtins/array/mod.rs 73.33% 8 Missing ⚠️
core/engine/src/builtins/dataview/mod.rs 0.00% 7 Missing ⚠️
core/engine/src/builtins/array_buffer/mod.rs 0.00% 6 Missing ⚠️
core/engine/src/object/builtins/jsdataview.rs 0.00% 5 Missing ⚠️
core/engine/src/builtins/array_buffer/shared.rs 0.00% 4 Missing ⚠️
core/engine/src/builtins/string/mod.rs 66.66% 3 Missing ⚠️
core/engine/src/builtins/atomics/mod.rs 0.00% 2 Missing ⚠️
core/engine/src/object/builtins/jsarray.rs 0.00% 2 Missing ⚠️
... and 5 more
Additional details and impacted files
@@            Coverage Diff             @@
##             main    #4083      +/-   ##
==========================================
+ Coverage   47.24%   53.34%   +6.09%     
==========================================
  Files         476      484       +8     
  Lines       46892    48196    +1304     
==========================================
+ Hits        22154    25709    +3555     
+ Misses      24738    22487    -2251     

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

@jedel1043
Copy link
Member

Test262 conformance changes

Test result main count PR count difference
Total 48,625 48,625 0
Passed 43,616 43,607 -9
Ignored 1,471 1,471 0
Failed 3,538 3,547 +9
Panics 0 0 0
Conformance 89.70% 89.68% -0.02%
Broken tests (9):
test/built-ins/TypedArray/prototype/lastIndexOf/search-not-found-returns-minus-one.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/tointeger-fromindex.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/search-found-returns-index.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/fromIndex-minus-zero.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/resizable-buffer.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/BigInt/search-not-found-returns-minus-one.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/BigInt/tointeger-fromindex.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/BigInt/search-found-returns-index.js (previously Passed)
test/built-ins/TypedArray/prototype/lastIndexOf/BigInt/fromIndex-minus-zero.js (previously Passed)

@jedel1043
Copy link
Member

Hmm, there were some test regressions. Can you investigate them? Maybe it was just a typo somewhere.

@hansl hansl requested a review from jedel1043 December 13, 2024 01:14
@hansl
Copy link
Contributor Author

hansl commented Dec 13, 2024

@jedel1043 I don't know how to manually re-run the test262 on this project, so I'll leave that to you as a reviewer. Thanks!

@jedel1043
Copy link
Member

jedel1043 commented Dec 13, 2024

@hansl tests already ran, it's just on the output of the "Update comment / Write a new comment" steps for the Test262 action. We really need to setup something so that this thing works on external repos 😅

@jedel1043
Copy link
Member

Test262 conformance changes

Test result main count PR count difference
Total 48,625 48,625 0
Passed 43,616 43,616 0
Ignored 1,471 1,471 0
Failed 3,538 3,538 0
Panics 0 0 0
Conformance 89.70% 89.70% 0.00%

Copy link
Member

@jedel1043 jedel1043 left a comment

Choose a reason for hiding this comment

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

Looks great!

Approving this, but can you open an issue to add CI tests for a 32-bit architecture? I'd really like to potentially test that just to avoid introducing platform-dependent regressions.

@hansl
Copy link
Contributor Author

hansl commented Dec 14, 2024

Sounds good. Let me test the test262 on my embedded platform before merging. Will have to wait to Monday.

Scratch that. I won't be able to easily run test262, but I'll run the integration tests and the unit tests tonight and report back. Will also create an issue.

@hansl
Copy link
Contributor Author

hansl commented Dec 14, 2024

At least 3 tests are failing in boa_engine, will investigate Monday.

CleanShot 2024-12-13 at 19 12 58@2x

To be fair, they're likely wrong test expectations based on overflow/underflow, but I still want to make sure.

Comment on lines 899 to 902
pub fn to_length(&self, context: &mut Context) -> JsResult<usize> {
// 1. Let len be ? ToInteger(argument).
// 2. If len ≤ +0, return +0.
// 3. Return min(len, 2^53 - 1).
Copy link
Member

Choose a reason for hiding this comment

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

We used to have it be usize, but we changed it because on 32-bit systems it does not capture the full range that a JavaScript length can be. (for arrays it's fine to put it as usize since it's between 0..2^32-1, but for strings and typed arrays, the upper bound is 2^53-1 (max safe representable integer in a f64).

Setting it to usize truncates the values on 32-bit platforms giving wrong results.

Copy link
Member

Choose a reason for hiding this comment

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

For example, one place that to_length is used is String.padEnd() on the maxLength parameter, putting 2^32-1 would work on 32-bit platform anything greater will be truncated (i.e. 2^32 will be truncated to 0), on 64-bit platform this would be fine.

Copy link
Contributor Author

@hansl hansl Dec 14, 2024

Choose a reason for hiding this comment

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

MAX_STRING_LENGTH is 2^32-1.

From the MDN:

The language specification requires strings to have a maximum length of 253 - 1 elements, which is the upper limit for precise integers. However, a string with this length needs 16384TiB of storage, which cannot fit in any reasonable device's memory, so implementations tend to lower the threshold, which allows the string's length to be conveniently stored in a 32-bit integer.

In V8 (used by Chrome and Node), the maximum length is 229 - 24 (~1GiB). On 32-bit systems, the maximum length is 228 - 16 (~512MiB).
In Firefox, the maximum length is 230 - 2 (~2GiB). Before Firefox 65, the maximum length was 228 - 1 (~512MiB).
In Safari, the maximum length is 231 - 1 (~4GiB).

Copy link
Member

@HalidOdat HalidOdat Dec 15, 2024

Choose a reason for hiding this comment

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

The issue isn't with allocation of the strings, rather that it breaks the math and checks in many places.

EDIT: What I mean is that even if the allocation is limited, the spec still assumes at the length is the full range, and all the math and checks before the final allocation should be able to hold that range.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That's all fine. If there's a bug with math being broken, then we need to fix it.

I would assume test262 and/or our tests would find any discrepancies, and I'd consider any issue a blocker for this PR. I'm currently running the full test262 suite on a Rasp Pi 2 equivalent. Everything works locally on my aarch64 Mac.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

If you can provide me with a valid JavaScript snippet that works on other implementations but doesn't work on this PR, I'll be more than happy to add it to tests and fix this PR.

@hansl
Copy link
Contributor Author

hansl commented Dec 19, 2024

Okay so after running test262 a bunch, and testing in browsers and Deno, I would still like to build this PR the right way but will need to move to a different approach:

  • isize/usize will be used internally for sizes, indices and offsets of strings.
  • i64/u64 will be used for array.

This should still result in performance improvements on 32-bits platforms (e.g. in wasm32).

I may type the new sizes StringUSize / StringISize and ArrayUSize / ArrayISize so it becomes more clear in the code which we're dealing with.

For now this PR will be back to draft.

@hansl hansl marked this pull request as draft December 19, 2024 01:49
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.

3 participants