Commit e4bbd39
authored
Optimize typedarray casting (#5037)
# Walkthrough: TypedArray Optimization via Direct Casting
This optimization improves the performance of creating a
[TypedArray](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs#40-44)
from another
[TypedArray](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs#40-44)
of the same `ContentType`. By implementing a direct
[cast](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs#580-732)
method on `TypedArrayElement`, we avoid unnecessary conversions to and
from `JsValue`.
## Changes Made
### [Core Engine]
####
[mod.rs](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs)
- Implemented `TypedArrayElement::cast(self, target: TypedArrayKind) ->
Self`.
- This method handles direct numeric casting and clamping (for
[Uint8Clamped](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs#245-246))
between all non-BigInt and BigInt types respectively.
- It panics if attempting to cast between different content types
(BigInt vs Number), which is consistent with the ECMAScript
specification requirements in
[initialize_from_typed_array](file:///home/mayank/boa/core/engine/src/builtins/typed_array/builtin.rs#2858-3008).
####
[builtin.rs](file:///home/mayank/boa/core/engine/src/builtins/typed_array/builtin.rs)
- Updated
[initialize_from_typed_array](file:///home/mayank/boa/core/engine/src/builtins/typed_array/builtin.rs#2858-3008)
to use `value.cast(element_type)` instead of converting through
`JsValue`.
- Removed the `TODO` comment: "TODO: cast between types instead of
converting to `JsValue`."
## Verification Results
### Automated Tests
Successfully ran the existing
[TypedArray](file:///home/mayank/boa/core/engine/src/builtins/typed_array/mod.rs#40-44)
tests in `boa_engine`:
```bash
cargo test --package boa_engine --lib builtins::typed_array::tests
```
**Results:**
```
running 12 tests
test builtins::typed_array::tests::float32_precision_behavior ... ok
test builtins::typed_array::tests::float32array_nan_behavior ... ok
test builtins::typed_array::tests::float32array_storage ... ok
test builtins::typed_array::tests::typedarray_prototype_set ... ok
test builtins::typed_array::tests::typedarray_prototype_fill ... ok
test builtins::typed_array::tests::typedarray_prototype_subarray_shared_memory ... ok
test builtins::typed_array::tests::uint8array_constructor_from_array ... ok
test builtins::typed_array::tests::uint8array_constructor_from_array_buffer ... ok
test builtins::typed_array::tests::uint8array_constructor_length ... ok
test builtins::typed_array::tests::uint8array_numeric_coercion ... ok
test builtins::typed_array::tests::uint8array_out_of_bounds_behavior ... ok
test builtins::typed_array::tests::uint8array_read_write_semantics ... ok
test result: ok. 12 passed; 0 failed; 0 ignored; 0 measured; 968 filtered out; finished in 0.09s
```
All relevant tests passed, ensuring no regressions in typed array
constructor behavior or standard operations.1 parent 1d0d8cb commit e4bbd39
File tree
3 files changed
+138
-7
lines changed- core/engine/src/builtins/typed_array
3 files changed
+138
-7
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
2963 | 2963 | | |
2964 | 2964 | | |
2965 | 2965 | | |
2966 | | - | |
2967 | | - | |
2968 | | - | |
2969 | | - | |
2970 | | - | |
2971 | | - | |
| 2966 | + | |
2972 | 2967 | | |
2973 | 2968 | | |
2974 | 2969 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
533 | 533 | | |
534 | 534 | | |
535 | 535 | | |
| 536 | + | |
| 537 | + | |
| 538 | + | |
| 539 | + | |
| 540 | + | |
| 541 | + | |
| 542 | + | |
| 543 | + | |
| 544 | + | |
| 545 | + | |
| 546 | + | |
| 547 | + | |
| 548 | + | |
| 549 | + | |
| 550 | + | |
| 551 | + | |
| 552 | + | |
| 553 | + | |
| 554 | + | |
| 555 | + | |
| 556 | + | |
| 557 | + | |
| 558 | + | |
| 559 | + | |
| 560 | + | |
| 561 | + | |
| 562 | + | |
| 563 | + | |
| 564 | + | |
| 565 | + | |
| 566 | + | |
| 567 | + | |
| 568 | + | |
| 569 | + | |
| 570 | + | |
536 | 571 | | |
537 | 572 | | |
538 | 573 | | |
| |||
576 | 611 | | |
577 | 612 | | |
578 | 613 | | |
| 614 | + | |
| 615 | + | |
| 616 | + | |
| 617 | + | |
| 618 | + | |
| 619 | + | |
| 620 | + | |
| 621 | + | |
| 622 | + | |
| 623 | + | |
| 624 | + | |
| 625 | + | |
| 626 | + | |
| 627 | + | |
| 628 | + | |
| 629 | + | |
| 630 | + | |
| 631 | + | |
| 632 | + | |
| 633 | + | |
| 634 | + | |
| 635 | + | |
| 636 | + | |
| 637 | + | |
| 638 | + | |
| 639 | + | |
| 640 | + | |
| 641 | + | |
| 642 | + | |
| 643 | + | |
| 644 | + | |
| 645 | + | |
| 646 | + | |
| 647 | + | |
| 648 | + | |
| 649 | + | |
| 650 | + | |
| 651 | + | |
| 652 | + | |
| 653 | + | |
| 654 | + | |
| 655 | + | |
| 656 | + | |
| 657 | + | |
| 658 | + | |
| 659 | + | |
| 660 | + | |
| 661 | + | |
| 662 | + | |
579 | 663 | | |
580 | 664 | | |
581 | 665 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
1 | | - | |
| 1 | + | |
2 | 2 | | |
3 | 3 | | |
4 | 4 | | |
| |||
109 | 109 | | |
110 | 110 | | |
111 | 111 | | |
| 112 | + | |
| 113 | + | |
| 114 | + | |
| 115 | + | |
| 116 | + | |
| 117 | + | |
| 118 | + | |
| 119 | + | |
| 120 | + | |
| 121 | + | |
| 122 | + | |
| 123 | + | |
| 124 | + | |
| 125 | + | |
| 126 | + | |
| 127 | + | |
| 128 | + | |
| 129 | + | |
| 130 | + | |
| 131 | + | |
| 132 | + | |
| 133 | + | |
| 134 | + | |
| 135 | + | |
| 136 | + | |
| 137 | + | |
| 138 | + | |
| 139 | + | |
| 140 | + | |
| 141 | + | |
| 142 | + | |
| 143 | + | |
| 144 | + | |
| 145 | + | |
| 146 | + | |
| 147 | + | |
| 148 | + | |
| 149 | + | |
| 150 | + | |
| 151 | + | |
| 152 | + | |
| 153 | + | |
| 154 | + | |
| 155 | + | |
| 156 | + | |
| 157 | + | |
| 158 | + | |
| 159 | + | |
| 160 | + | |
| 161 | + | |
| 162 | + | |
| 163 | + | |
0 commit comments