Skip to content

Commit 3ed83ef

Browse files
committed
Use macro style for more internal helpers; Update dist files
1 parent b585703 commit 3ed83ef

37 files changed

+544
-543
lines changed

dist/asc.js

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/asc.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js

+2-2
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

dist/assemblyscript.js.map

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

std/assembly/array.ts

+6-18
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,9 @@ import {
1414
} from "./internal/string";
1515

1616
import {
17-
defaultComparator,
18-
insertionSort,
19-
weakHeapSort
20-
} from "./internal/array";
17+
COMPARATOR,
18+
SORT
19+
} from "./internal/sort";
2120

2221
import {
2322
itoa,
@@ -403,7 +402,7 @@ export class Array<T> {
403402
return this;
404403
}
405404

406-
sort(comparator: (a: T, b: T) => i32 = defaultComparator<T>()): this {
405+
sort(comparator: (a: T, b: T) => i32 = COMPARATOR<T>()): this {
407406
// TODO remove this when flow will allow trackcing null
408407
assert(comparator); // The comparison function must be a function
409408

@@ -419,19 +418,8 @@ export class Array<T> {
419418
}
420419
return this;
421420
}
422-
423-
if (isReference<T>()) {
424-
// TODO replace this to faster stable sort (TimSort) when it implemented
425-
insertionSort<T>(buffer, 0, length, comparator);
426-
return this;
427-
} else {
428-
if (length < 256) {
429-
insertionSort<T>(buffer, 0, length, comparator);
430-
} else {
431-
weakHeapSort<T>(buffer, 0, length, comparator);
432-
}
433-
return this;
434-
}
421+
SORT<T>(buffer, 0, length, comparator);
422+
return this;
435423
}
436424

437425
join(separator: string = ","): string {

std/assembly/internal/arraybuffer.ts

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
1-
import { AL_MASK, MAX_SIZE_32 } from "./allocator";
1+
import {
2+
AL_MASK,
3+
MAX_SIZE_32
4+
} from "./allocator";
25

36
/** Size of an ArrayBuffer header. */
47
export const HEADER_SIZE: usize = (offsetof<ArrayBuffer>() + AL_MASK) & ~AL_MASK;

std/assembly/internal/hash.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
import {
2-
HEADER_SIZE as STRING_HEADER_SIZE
2+
HEADER_SIZE
33
} from "./string";
44

55
/** Computes the 32-bit hash of a value of any type. */
66
@inline
7-
export function hash<T>(key: T): u32 {
7+
export function HASH<T>(key: T): u32 {
88
// branch-level tree-shaking makes this a `(return (call ...))`
99
if (isString(key)) {
1010
return hashStr(key);
@@ -66,7 +66,7 @@ function hash64(key: u64): u32 {
6666
function hashStr(key: string): u32 {
6767
var v = FNV_OFFSET;
6868
for (let i: usize = 0, k: usize = key.length << 1; i < k; ++i) {
69-
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, STRING_HEADER_SIZE)) * FNV_PRIME;
69+
v = (v ^ <u32>load<u8>(changetype<usize>(key) + i, HEADER_SIZE)) * FNV_PRIME;
7070
}
7171
return v;
7272
}

std/assembly/internal/array.ts renamed to std/assembly/internal/sort.ts

+24-5
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@ import {
44
} from "./arraybuffer";
55

66
import {
7-
compareUnsafe,
7+
compareUnsafe
88
} from "./string";
99

10-
/** Obtains the default comparator for the specified type. */
10+
/** Obtains the default comparator for the specified value type. */
1111
@inline
12-
export function defaultComparator<T>(): (a: T, b: T) => i32 {
12+
export function COMPARATOR<T>(): (a: T, b: T) => i32 {
1313
if (isInteger<T>()) {
1414
if (isSigned<T>() && sizeof<T>() <= 4) {
1515
return (a: T, b: T): i32 => (<i32>(a - b));
@@ -44,8 +44,27 @@ export function defaultComparator<T>(): (a: T, b: T) => i32 {
4444
}
4545
}
4646

47+
@inline
48+
export function SORT<T>(
49+
buffer: ArrayBuffer,
50+
byteOffset: i32,
51+
length: i32,
52+
comparator: (a: T, b: T) => i32
53+
): void {
54+
if (isReference<T>()) {
55+
// TODO replace this to faster stable sort (TimSort) when it implemented
56+
insertionSort<T>(buffer, byteOffset, length, comparator);
57+
} else {
58+
if (length < 256) {
59+
insertionSort<T>(buffer, byteOffset, length, comparator);
60+
} else {
61+
weakHeapSort<T>(buffer, byteOffset, length, comparator);
62+
}
63+
}
64+
}
65+
4766
/** Sorts an Array with the 'Insertion Sort' algorithm. */
48-
export function insertionSort<T>(
67+
function insertionSort<T>(
4968
buffer: ArrayBuffer,
5069
byteOffset: i32,
5170
length: i32,
@@ -65,7 +84,7 @@ export function insertionSort<T>(
6584
}
6685

6786
/** Sorts an Array with the 'Weak Heap Sort' algorithm. */
68-
export function weakHeapSort<T>(
87+
function weakHeapSort<T>(
6988
buffer: ArrayBuffer,
7089
byteOffset: i32,
7190
length: i32,

std/assembly/internal/typedarray.ts

+4-15
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,8 @@ import {
77
} from "./arraybuffer";
88

99
import {
10-
insertionSort,
11-
weakHeapSort
12-
} from "./array";
10+
SORT as SORT_IMPL
11+
} from "./sort";
1312

1413
/** Typed array base class. Not a global object. */
1514
export abstract class TypedArray<T> {
@@ -106,18 +105,8 @@ export function SORT<TArray extends TypedArray<T>, T>(
106105
}
107106
return array;
108107
}
109-
if (isReference<T>()) {
110-
// TODO replace this to faster stable sort (TimSort) when it implemented
111-
insertionSort<T>(buffer, byteOffset, length, comparator);
112-
return array;
113-
} else {
114-
if (length < 256) {
115-
insertionSort<T>(buffer, byteOffset, length, comparator);
116-
} else {
117-
weakHeapSort<T>(buffer, byteOffset, length, comparator);
118-
}
119-
return array;
120-
}
108+
SORT_IMPL<T>(buffer, byteOffset, length, comparator);
109+
return array;
121110
}
122111

123112
@inline

std/assembly/map.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
} from "./internal/arraybuffer";
44

55
import {
6-
hash
6+
HASH
77
} from "./internal/hash";
88

99
// A deterministic hash map based on CloseTable from https://github.com/jorendorff/dht
@@ -80,16 +80,16 @@ export class Map<K,V> {
8080
}
8181

8282
has(key: K): bool {
83-
return this.find(key, hash<K>(key)) !== null;
83+
return this.find(key, HASH<K>(key)) !== null;
8484
}
8585

8686
get(key: K): V {
87-
var entry = this.find(key, hash<K>(key));
87+
var entry = this.find(key, HASH<K>(key));
8888
return entry ? entry.value : <V>unreachable();
8989
}
9090

9191
set(key: K, value: V): void {
92-
var hashCode = hash<K>(key);
92+
var hashCode = HASH<K>(key);
9393
var entry = this.find(key, hashCode);
9494
if (entry) {
9595
entry.value = value;
@@ -120,7 +120,7 @@ export class Map<K,V> {
120120
}
121121

122122
delete(key: K): bool {
123-
var entry = this.find(key, hash<K>(key));
123+
var entry = this.find(key, HASH<K>(key));
124124
if (!entry) return false;
125125
entry.taggedNext |= EMPTY;
126126
--this.entriesCount;
@@ -149,7 +149,7 @@ export class Map<K,V> {
149149
let newEntry = changetype<MapEntry<K,V>>(newPtr);
150150
newEntry.key = oldEntry.key;
151151
newEntry.value = oldEntry.value;
152-
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
152+
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
153153
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
154154
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
155155
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);

std/assembly/set.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import {
33
} from "./internal/arraybuffer";
44

55
import {
6-
hash
6+
HASH
77
} from "./internal/hash";
88

99
// A deterministic hash set based on CloseTable from https://github.com/jorendorff/dht
@@ -78,11 +78,11 @@ export class Set<K> {
7878
}
7979

8080
has(key: K): bool {
81-
return this.find(key, hash(key)) !== null;
81+
return this.find(key, HASH(key)) !== null;
8282
}
8383

8484
add(key: K): void {
85-
var hashCode = hash(key);
85+
var hashCode = HASH(key);
8686
var entry = this.find(key, hashCode);
8787
if (!entry) {
8888
// check if rehashing is necessary
@@ -109,7 +109,7 @@ export class Set<K> {
109109
}
110110

111111
delete(key: K): bool {
112-
var entry = this.find(key, hash<K>(key));
112+
var entry = this.find(key, HASH<K>(key));
113113
if (!entry) return false;
114114
entry.taggedNext |= EMPTY;
115115
--this.entriesCount;
@@ -137,7 +137,7 @@ export class Set<K> {
137137
if (!(oldEntry.taggedNext & EMPTY)) {
138138
let newEntry = changetype<SetEntry<K>>(newPtr);
139139
newEntry.key = oldEntry.key;
140-
let newBucketIndex = hash<K>(oldEntry.key) & newBucketsMask;
140+
let newBucketIndex = HASH<K>(oldEntry.key) & newBucketsMask;
141141
let newBucketPtrBase = changetype<usize>(newBuckets) + <usize>newBucketIndex * BUCKET_SIZE;
142142
newEntry.taggedNext = load<usize>(newBucketPtrBase, HEADER_SIZE_AB);
143143
store<usize>(newBucketPtrBase, newPtr, HEADER_SIZE_AB);

std/assembly/typedarray.ts

+13-13
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ import {
99
} from "./internal/typedarray";
1010

1111
import {
12-
defaultComparator
13-
} from "./internal/array";
12+
COMPARATOR
13+
} from "./internal/sort";
1414

1515
export class Int8Array extends TypedArray<i8> {
1616
static readonly BYTES_PER_ELEMENT: usize = sizeof<i8>();
@@ -19,7 +19,7 @@ export class Int8Array extends TypedArray<i8> {
1919
return FILL<Int8Array, i8>(this, value, start, end);
2020
}
2121

22-
sort(comparator: (a: i8, b: i8) => i32 = defaultComparator<i8>()): Int8Array {
22+
sort(comparator: (a: i8, b: i8) => i32 = COMPARATOR<i8>()): Int8Array {
2323
return SORT<Int8Array, i8>(this, comparator);
2424
}
2525

@@ -53,7 +53,7 @@ export class Uint8Array extends TypedArray<u8> {
5353
return FILL<Uint8Array, u8>(this, value, start, end);
5454
}
5555

56-
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8Array {
56+
sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8Array {
5757
return SORT<Uint8Array, u8>(this, comparator);
5858
}
5959

@@ -97,7 +97,7 @@ export class Uint8ClampedArray extends Uint8Array {
9797
return changetype<Uint8ClampedArray>(super.fill(value, start, end)); // safe because '.fill' reuses 'this'
9898
}
9999

100-
sort(comparator: (a: u8, b: u8) => i32 = defaultComparator<u8>()): Uint8ClampedArray {
100+
sort(comparator: (a: u8, b: u8) => i32 = COMPARATOR<u8>()): Uint8ClampedArray {
101101
return changetype<Uint8ClampedArray>(super.sort(comparator)); // safe because '.sort' reuses 'this'
102102
}
103103

@@ -117,7 +117,7 @@ export class Int16Array extends TypedArray<i16> {
117117
return FILL<Int16Array, i16>(this, value, start, end);
118118
}
119119

120-
sort(comparator: (a: i16, b: i16) => i32 = defaultComparator<i16>()): Int16Array {
120+
sort(comparator: (a: i16, b: i16) => i32 = COMPARATOR<i16>()): Int16Array {
121121
return SORT<Int16Array, i16>(this, comparator);
122122
}
123123

@@ -151,7 +151,7 @@ export class Uint16Array extends TypedArray<u16> {
151151
return FILL<Uint16Array, u16>(this, value, start, end);
152152
}
153153

154-
sort(comparator: (a: u16, b: u16) => i32 = defaultComparator<u16>()): Uint16Array {
154+
sort(comparator: (a: u16, b: u16) => i32 = COMPARATOR<u16>()): Uint16Array {
155155
return SORT<Uint16Array, u16>(this, comparator);
156156
}
157157

@@ -185,7 +185,7 @@ export class Int32Array extends TypedArray<i32> {
185185
return FILL<Int32Array, i32>(this, value, start, end);
186186
}
187187

188-
sort(comparator: (a: i32, b: i32) => i32 = defaultComparator<i32>()): Int32Array {
188+
sort(comparator: (a: i32, b: i32) => i32 = COMPARATOR<i32>()): Int32Array {
189189
return SORT<Int32Array, i32>(this, comparator);
190190
}
191191

@@ -219,7 +219,7 @@ export class Uint32Array extends TypedArray<u32> {
219219
return FILL<Uint32Array, u32>(this, value, start, end);
220220
}
221221

222-
sort(comparator: (a: u32, b: u32) => i32 = defaultComparator<u32>()): Uint32Array {
222+
sort(comparator: (a: u32, b: u32) => i32 = COMPARATOR<u32>()): Uint32Array {
223223
return SORT<Uint32Array, u32>(this, comparator);
224224
}
225225

@@ -253,7 +253,7 @@ export class Int64Array extends TypedArray<i64> {
253253
return FILL<Int64Array, i64>(this, value, start, end);
254254
}
255255

256-
sort(comparator: (a: i64, b: i64) => i32 = defaultComparator<i64>()): Int64Array {
256+
sort(comparator: (a: i64, b: i64) => i32 = COMPARATOR<i64>()): Int64Array {
257257
return SORT<Int64Array, i64>(this, comparator);
258258
}
259259

@@ -287,7 +287,7 @@ export class Uint64Array extends TypedArray<u64> {
287287
return FILL<Uint64Array, u64>(this, value, start, end);
288288
}
289289

290-
sort(comparator: (a: u64, b: u64) => i32 = defaultComparator<u64>()): Uint64Array {
290+
sort(comparator: (a: u64, b: u64) => i32 = COMPARATOR<u64>()): Uint64Array {
291291
return SORT<Uint64Array, u64>(this, comparator);
292292
}
293293

@@ -321,7 +321,7 @@ export class Float32Array extends TypedArray<f32> {
321321
return FILL<Float32Array, f32>(this, value, start, end);
322322
}
323323

324-
sort(comparator: (a: f32, b: f32) => i32 = defaultComparator<f32>()): Float32Array {
324+
sort(comparator: (a: f32, b: f32) => i32 = COMPARATOR<f32>()): Float32Array {
325325
return SORT<Float32Array, f32>(this, comparator);
326326
}
327327

@@ -355,7 +355,7 @@ export class Float64Array extends TypedArray<f64> {
355355
return FILL<Float64Array, f64>(this, value, start, end);
356356
}
357357

358-
sort(comparator: (a: f64, b: f64) => i32 = defaultComparator<f64>()): Float64Array {
358+
sort(comparator: (a: f64, b: f64) => i32 = COMPARATOR<f64>()): Float64Array {
359359
return SORT<Float64Array, f64>(this, comparator);
360360
}
361361

tests/compiler/std/array-literal.optimized.wat

+1-1
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@
9999
if
100100
i32.const 0
101101
i32.const 168
102-
i32.const 23
102+
i32.const 26
103103
i32.const 2
104104
call $~lib/env/abort
105105
unreachable

0 commit comments

Comments
 (0)