Improve memory usage for weakHeap sorting (#68)

This commit is contained in:
Max Graey 2018-04-08 02:03:26 +03:00 committed by Daniel Wirtz
parent 9731958738
commit ac2281b14b
3 changed files with 31 additions and 31 deletions

View File

@ -36,14 +36,14 @@ export function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32):
const typeShift = alignof<T>();
const intShift = alignof<i32>();
var blen = (len + 7) >> 3;
var blen = (len + 31) >> 5;
var bitset = allocate_memory(blen << intShift);
set_memory(bitset, 0, blen << intShift);
for (i = len - 1; i > 0; i--) {
j = i;
while ((j & 1) == ((load<i32>(bitset + ((j >> 4) << intShift)) >> ((j >> 1) & 7)) & 1)) {
while ((j & 1) == ((load<i32>(bitset + ((j >> 6) << intShift)) >> ((j >> 1) & 31)) & 1)) {
j >>= 1;
}
@ -54,8 +54,8 @@ export function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32):
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((i >> 3) << intShift),
load<i32>(bitset + ((i >> 3) << intShift)) ^ (1 << (i & 7))
bitset + ((i >> 5) << intShift),
load<i32>(bitset + ((i >> 5) << intShift)) ^ (1 << (i & 31))
);
store<T>(arr.__memory + (i << typeShift), a); // arr[i] = a;
store<T>(arr.__memory + (p << typeShift), b); // arr[p] = b;
@ -73,7 +73,7 @@ export function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32):
store<T>(arr.__memory + (i << typeShift), a);
let x = 1;
while ((y = (x << 1) + ((load<i32>(bitset + ((x >> 3) << intShift)) >> (x & 7)) & 1)) < i) {
while ((y = (x << 1) + ((load<i32>(bitset + ((x >> 5) << intShift)) >> (x & 31)) & 1)) < i) {
x = y;
}
@ -83,8 +83,8 @@ export function weakHeapSort<T>(arr: Array<T>, comparator: (a: T, b: T) => i32):
if (comparator(a, b) < 0) {
store<i32>(
bitset + ((x >> 3) << intShift),
load<i32>(bitset + ((x >> 3) << intShift)) ^ (1 << (x & 7))
bitset + ((x >> 5) << intShift),
load<i32>(bitset + ((x >> 5) << intShift)) ^ (1 << (x & 31))
);
store<T>(arr.__memory + (x << typeShift), a); // arr[x] = a;

View File

@ -4030,9 +4030,9 @@
(get_local $0)
)
)
(i32.const 7)
(i32.const 31)
)
(i32.const 3)
(i32.const 5)
)
)
(i32.const 2)
@ -4076,7 +4076,7 @@
(i32.shl
(i32.shr_s
(get_local $2)
(i32.const 4)
(i32.const 6)
)
(i32.const 2)
)
@ -4087,7 +4087,7 @@
(get_local $2)
(i32.const 1)
)
(i32.const 7)
(i32.const 31)
)
)
(i32.const 1)
@ -4156,7 +4156,7 @@
(i32.shl
(i32.shr_s
(get_local $3)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4168,7 +4168,7 @@
(i32.shl
(i32.shr_s
(get_local $3)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4178,7 +4178,7 @@
(i32.const 1)
(i32.and
(get_local $3)
(i32.const 7)
(i32.const 31)
)
)
)
@ -4287,7 +4287,7 @@
(i32.shl
(i32.shr_s
(get_local $2)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4295,7 +4295,7 @@
)
(i32.and
(get_local $2)
(i32.const 7)
(i32.const 31)
)
)
(i32.const 1)
@ -4360,7 +4360,7 @@
(i32.shl
(i32.shr_s
(get_local $2)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4372,7 +4372,7 @@
(i32.shl
(i32.shr_s
(get_local $2)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4382,7 +4382,7 @@
(i32.const 1)
(i32.and
(get_local $2)
(i32.const 7)
(i32.const 31)
)
)
)

View File

@ -4635,9 +4635,9 @@
(i32.shr_s
(i32.add
(get_local $2)
(i32.const 7)
(i32.const 31)
)
(i32.const 3)
(i32.const 5)
)
)
(set_local $10
@ -4690,7 +4690,7 @@
(i32.shl
(i32.shr_s
(get_local $4)
(i32.const 4)
(i32.const 6)
)
(i32.const 2)
)
@ -4701,7 +4701,7 @@
(get_local $4)
(i32.const 1)
)
(i32.const 7)
(i32.const 31)
)
)
(i32.const 1)
@ -4774,7 +4774,7 @@
(i32.shl
(i32.shr_s
(get_local $3)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4786,7 +4786,7 @@
(i32.shl
(i32.shr_s
(get_local $3)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4796,7 +4796,7 @@
(i32.const 1)
(i32.and
(get_local $3)
(i32.const 7)
(i32.const 31)
)
)
)
@ -4910,7 +4910,7 @@
(i32.shl
(i32.shr_s
(get_local $11)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -4918,7 +4918,7 @@
)
(i32.and
(get_local $11)
(i32.const 7)
(i32.const 31)
)
)
(i32.const 1)
@ -4988,7 +4988,7 @@
(i32.shl
(i32.shr_s
(get_local $11)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -5000,7 +5000,7 @@
(i32.shl
(i32.shr_s
(get_local $11)
(i32.const 3)
(i32.const 5)
)
(i32.const 2)
)
@ -5010,7 +5010,7 @@
(i32.const 1)
(i32.and
(get_local $11)
(i32.const 7)
(i32.const 31)
)
)
)