Skip to content

[std] Optimize reverse method for Arrays #2016

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

Merged
merged 10 commits into from
Aug 13, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 2 additions & 12 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import { BLOCK_MAXSIZE } from "./rt/common";
import { COMPARATOR, SORT } from "./util/sort";
import { REVERSE } from "./util/bytes";
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
import { idof, isArray as builtin_isArray } from "./builtins";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_ILLEGALGENTYPE, E_EMPTYARRAY, E_HOLEYARRAY } from "./util/error";
Expand Down Expand Up @@ -431,18 +432,7 @@ export class Array<T> {
}

reverse(): Array<T> {
var len = this.length_;
if (len) {
let front = this.dataStart;
let back = this.dataStart + (<usize>(len - 1) << alignof<T>());
while (front < back) {
let temp = load<T>(front);
store<T>(front, load<T>(back));
store<T>(back, temp);
front += sizeof<T>();
back -= sizeof<T>();
}
}
REVERSE<T>(this.dataStart, this.length_);
return this;
}

Expand Down
48 changes: 23 additions & 25 deletions std/assembly/typedarray.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { COMPARATOR, SORT } from "./util/sort";
import { E_INDEXOUTOFRANGE, E_INVALIDLENGTH, E_NOTIMPLEMENTED } from "./util/error";
import { joinIntegerArray, joinFloatArray } from "./util/string";
import { REVERSE } from "./util/bytes";
import { idof } from "./builtins";
import { ArrayBufferView } from "./arraybuffer";

Expand Down Expand Up @@ -124,7 +125,8 @@ export class Int8Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, i8>(this);
REVERSE<u8>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -264,7 +266,8 @@ export class Uint8Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, u8>(this);
REVERSE<u8>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -404,7 +407,8 @@ export class Uint8ClampedArray extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, u8>(this);
REVERSE<u8>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -544,7 +548,8 @@ export class Int16Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, i16>(this);
REVERSE<u16>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -684,7 +689,8 @@ export class Uint16Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, u16>(this);
REVERSE<u16>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -824,7 +830,8 @@ export class Int32Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, i32>(this);
REVERSE<u32>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -964,7 +971,8 @@ export class Uint32Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, u32>(this);
REVERSE<u32>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -1104,7 +1112,8 @@ export class Int64Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, i64>(this);
REVERSE<u64>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -1244,7 +1253,8 @@ export class Uint64Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, u64>(this);
REVERSE<u64>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -1384,7 +1394,8 @@ export class Float32Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, f32>(this);
REVERSE<f32>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -1524,7 +1535,8 @@ export class Float64Array extends ArrayBufferView {
}

reverse(): this {
return REVERSE<this, f64>(this);
REVERSE<f64>(this.dataStart, this.length);
return this;
}

join(separator: string = ","): string {
Expand Down Expand Up @@ -1844,20 +1856,6 @@ function FOREACH<TArray extends ArrayBufferView, T>(
}
}

// @ts-ignore: decorator
@inline
function REVERSE<TArray extends ArrayBufferView, T>(array: TArray): TArray {
var ptr = array.dataStart;
for (let front: usize = 0, back: usize = array.length - 1; front < back; ++front, --back) {
let frontPtr = ptr + (front << alignof<T>());
let backPtr = ptr + (back << alignof<T>());
let temp = load<T>(frontPtr);
store<T>(frontPtr, load<T>(backPtr));
store<T>(backPtr, temp);
}
return array;
}

// @ts-ignore: decorator
@inline
function WRAP<TArray extends ArrayBufferView, T>(
Expand Down
54 changes: 54 additions & 0 deletions std/assembly/util/bytes.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
export function REVERSE<T>(ptr: usize, len: usize): void {
if (len > 1) {
let
i: usize = 0,
tail: usize,
hlen: usize = len >> 1;

if (ASC_SHRINK_LEVEL < 1) {
if (sizeof<T>() == 1) {
// TODO: Decide later: Does we need this fast path cases?
//
// if (len == 4) {
// store<u32>(ptr, bswap(load<u32>(ptr)));
// return;
// }
// if (len == 8) {
// store<u64>(ptr, bswap(load<u64>(ptr)));
// return;
// }
tail = len - 8;
while (i + 7 < hlen) {
let front = ptr + i;
let back = ptr + tail - i;
let temp = bswap(load<u64>(front));
store<u64>(front, bswap(load<u64>(back)));
store<u64>(back, temp);
i += 8;
}
}

if (sizeof<T>() == 2) {
tail = len - 2;
while (i + 1 < hlen) {
let front = ptr + (i << 1);
let back = ptr + (tail - i << 1);
let temp = rotr(load<u32>(back), 16);
store<u32>(back, rotr(load<u32>(front), 16));
store<u32>(front, temp);
i += 2;
}
}
}

tail = len - 1;
while (i < hlen) {
let front = ptr + (i << alignof<T>());
let back = ptr + (tail - i << alignof<T>());
let temp = load<T>(front);
store<T>(front, load<T>(back));
store<T>(back, temp);
i++;
}
}
}
6 changes: 3 additions & 3 deletions tests/compiler/assert-nonnull.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@
if
i32.const 1184
i32.const 1248
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -261,7 +261,7 @@
if
i32.const 1184
i32.const 1248
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -277,7 +277,7 @@
if
i32.const 1296
i32.const 1248
i32.const 110
i32.const 111
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down
6 changes: 3 additions & 3 deletions tests/compiler/assert-nonnull.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -310,7 +310,7 @@
if
i32.const 160
i32.const 224
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -335,7 +335,7 @@
if
i32.const 272
i32.const 224
i32.const 110
i32.const 111
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -366,7 +366,7 @@
if
i32.const 160
i32.const 224
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/class.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2724,7 +2724,7 @@
if
i32.const 432
i32.const 480
i32.const 64
i32.const 65
i32.const 60
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/extends-baseaggregate.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2554,7 +2554,7 @@
if
i32.const 1616
i32.const 1664
i32.const 17
i32.const 18
i32.const 48
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/extends-baseaggregate.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3761,7 +3761,7 @@
if
i32.const 592
i32.const 640
i32.const 17
i32.const 18
i32.const 48
call $~lib/builtins/abort
unreachable
Expand Down
2 changes: 1 addition & 1 deletion tests/compiler/infer-array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2844,7 +2844,7 @@
end
i32.const 1280
i32.const 1488
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down
18 changes: 9 additions & 9 deletions tests/compiler/infer-array.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3708,7 +3708,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -3734,7 +3734,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -3760,7 +3760,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -3786,7 +3786,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -3828,7 +3828,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4604,7 +4604,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4650,7 +4650,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand Down Expand Up @@ -4696,7 +4696,7 @@
if
i32.const 256
i32.const 464
i32.const 106
i32.const 107
i32.const 42
call $~lib/builtins/abort
unreachable
Expand All @@ -4721,7 +4721,7 @@
if
i32.const 976
i32.const 464
i32.const 110
i32.const 111
i32.const 40
call $~lib/builtins/abort
unreachable
Expand Down
Loading