Skip to content

Commit 3ff1003

Browse files
authored
Add fast paths for array's fill method (#2307)
1 parent 9c0a8c7 commit 3ff1003

12 files changed

+8921
-7977
lines changed

Diff for: std/assembly/array.ts

+28
Original file line numberDiff line numberDiff line change
@@ -173,6 +173,34 @@ export class Array<T> {
173173
);
174174
}
175175
} else {
176+
if (ASC_SHRINK_LEVEL <= 1) {
177+
if (isInteger<T>()) {
178+
// @ts-ignore
179+
if (value == <T>0 | value == <T>-1) {
180+
if (start < end) {
181+
memory.fill(
182+
ptr + (<usize>start << alignof<T>()),
183+
u8(value),
184+
<usize>(end - start) << alignof<T>()
185+
);
186+
}
187+
return this;
188+
}
189+
} else if (isFloat<T>()) {
190+
// for floating non-negative zeros we can use fast memory.fill
191+
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
192+
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
193+
if (start < end) {
194+
memory.fill(
195+
ptr + (<usize>start << alignof<T>()),
196+
0,
197+
<usize>(end - start) << alignof<T>()
198+
);
199+
}
200+
return this;
201+
}
202+
}
203+
}
176204
for (; start < end; ++start) {
177205
store<T>(ptr + (<usize>start << alignof<T>()), value);
178206
}

Diff for: std/assembly/staticarray.ts

+28
Original file line numberDiff line numberDiff line change
@@ -160,6 +160,34 @@ export class StaticArray<T> {
160160
);
161161
}
162162
} else {
163+
if (ASC_SHRINK_LEVEL <= 1) {
164+
if (isInteger<T>()) {
165+
// @ts-ignore
166+
if (value == <T>0 | value == <T>-1) {
167+
if (start < end) {
168+
memory.fill(
169+
ptr + (<usize>start << alignof<T>()),
170+
u8(value),
171+
<usize>(end - start) << alignof<T>()
172+
);
173+
}
174+
return this;
175+
}
176+
} else if (isFloat<T>()) {
177+
// for floating non-negative zeros we can use fast memory.fill
178+
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
179+
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
180+
if (start < end) {
181+
memory.fill(
182+
ptr + (<usize>start << alignof<T>()),
183+
0,
184+
<usize>(end - start) << alignof<T>()
185+
);
186+
}
187+
return this;
188+
}
189+
}
190+
}
163191
for (; start < end; ++start) {
164192
store<T>(ptr + (<usize>start << alignof<T>()), value);
165193
}

Diff for: std/assembly/typedarray.ts

+28
Original file line numberDiff line numberDiff line change
@@ -1571,6 +1571,34 @@ function FILL<TArray extends ArrayBufferView, T extends number>(
15711571
if (sizeof<T>() == 1) {
15721572
if (start < end) memory.fill(ptr + <usize>start, <u8>value, <usize>(end - start));
15731573
} else {
1574+
if (ASC_SHRINK_LEVEL <= 1) {
1575+
if (isInteger<T>()) {
1576+
// @ts-ignore
1577+
if (value == <T>0 | value == <T>-1) {
1578+
if (start < end) {
1579+
memory.fill(
1580+
ptr + (<usize>start << alignof<T>()),
1581+
u8(value),
1582+
<usize>(end - start) << alignof<T>()
1583+
);
1584+
}
1585+
return array;
1586+
}
1587+
} else if (isFloat<T>()) {
1588+
// for floating non-negative zeros we can use fast memory.fill
1589+
if ((sizeof<T>() == 4 && reinterpret<u32>(f32(value)) == 0) ||
1590+
(sizeof<T>() == 8 && reinterpret<u64>(f64(value)) == 0)) {
1591+
if (start < end) {
1592+
memory.fill(
1593+
ptr + (<usize>start << alignof<T>()),
1594+
0,
1595+
<usize>(end - start) << alignof<T>()
1596+
);
1597+
}
1598+
return array;
1599+
}
1600+
}
1601+
}
15741602
for (; start < end; ++start) {
15751603
store<T>(ptr + (<usize>start << alignof<T>()), value);
15761604
}

Diff for: tests/compiler/std-wasi/crypto.debug.wat

+4-4
Original file line numberDiff line numberDiff line change
@@ -4920,7 +4920,7 @@
49204920
if
49214921
i32.const 448
49224922
i32.const 656
4923-
i32.const 1870
4923+
i32.const 1898
49244924
i32.const 5
49254925
call $~lib/wasi/index/abort
49264926
unreachable
@@ -4939,7 +4939,7 @@
49394939
if
49404940
i32.const 144
49414941
i32.const 656
4942-
i32.const 1875
4942+
i32.const 1903
49434943
i32.const 9
49444944
call $~lib/wasi/index/abort
49454945
unreachable
@@ -4951,7 +4951,7 @@
49514951
else
49524952
i32.const 144
49534953
i32.const 656
4954-
i32.const 1879
4954+
i32.const 1907
49554955
i32.const 7
49564956
call $~lib/wasi/index/abort
49574957
unreachable
@@ -4969,7 +4969,7 @@
49694969
if
49704970
i32.const 144
49714971
i32.const 656
4972-
i32.const 1884
4972+
i32.const 1912
49734973
i32.const 7
49744974
call $~lib/wasi/index/abort
49754975
unreachable

Diff for: tests/compiler/std-wasi/crypto.release.wat

+3-3
Original file line numberDiff line numberDiff line change
@@ -3717,7 +3717,7 @@
37173717
if
37183718
i32.const 1472
37193719
i32.const 1680
3720-
i32.const 1870
3720+
i32.const 1898
37213721
i32.const 5
37223722
call $~lib/wasi/index/abort
37233723
unreachable
@@ -3736,7 +3736,7 @@
37363736
else
37373737
i32.const 1168
37383738
i32.const 1680
3739-
i32.const 1879
3739+
i32.const 1907
37403740
i32.const 7
37413741
call $~lib/wasi/index/abort
37423742
unreachable
@@ -3751,7 +3751,7 @@
37513751
if
37523752
i32.const 1168
37533753
i32.const 1680
3754-
i32.const 1884
3754+
i32.const 1912
37553755
i32.const 7
37563756
call $~lib/wasi/index/abort
37573757
unreachable

0 commit comments

Comments
 (0)