Use move_memory in std Array#unshift

This commit is contained in:
dcodeIO 2018-01-14 02:41:13 +01:00
parent ad469ca445
commit 827bb4afe8
4 changed files with 167 additions and 3290 deletions

View File

@ -71,16 +71,19 @@ export class Array<T> {
unshift(element: T): i32 {
var oldCapacity = this.__capacity;
if (<u32>this.length >= oldCapacity)
this.__grow(max(this.length + 1, oldCapacity * 2));
// FIXME: this is inefficient because of two copies, one in __grow and one here
// move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
if (oldCapacity)
for (var index: usize = oldCapacity; index > 0; --index)
store<T>(this.__memory + index * sizeof<T>(), load<T>(this.__memory + (index - 1) * sizeof<T>()));
if (<u32>this.length >= oldCapacity) {
// inlined `this.__grow(max(this.length + 1, oldCapacity * 2))` (avoids moving twice)
var newCapacity = max(this.length + 1, oldCapacity * 2);
assert(newCapacity > this.__capacity);
var newMemory = allocate_memory(<usize>newCapacity * sizeof<T>());
if (this.__memory) {
move_memory(newMemory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
free_memory(this.__memory);
}
this.__memory = newMemory;
this.__capacity = newCapacity;
} else
move_memory(this.__memory + sizeof<T>(), this.__memory, oldCapacity * sizeof<T>());
store<T>(this.__memory, element);
return ++this.length;
}

File diff suppressed because it is too large Load Diff

View File

@ -2168,79 +2168,99 @@
(i32.load offset=8
(get_local $0)
)
(tee_local $2
(tee_local $4
(i32.load offset=4
(get_local $0)
)
)
)
(call $std:array/Array#__grow
(get_local $0)
(select
(tee_local $3
(i32.add
(i32.load offset=8
(get_local $0)
(block
(if
(i32.le_s
(tee_local $2
(select
(tee_local $2
(i32.add
(i32.load offset=8
(get_local $0)
)
(i32.const 1)
)
)
(tee_local $3
(i32.mul
(get_local $4)
(i32.const 2)
)
)
(i32.gt_s
(get_local $2)
(get_local $3)
)
)
(i32.const 1)
)
(i32.load offset=4
(get_local $0)
)
)
(tee_local $4
(unreachable)
)
(set_local $3
(call $std:heap/allocate_memory
(i32.mul
(get_local $2)
(i32.const 2)
(i32.const 4)
)
)
(i32.gt_s
(get_local $3)
(get_local $4)
)
)
)
)
(if
(get_local $2)
(loop $continue|0
(if
(i32.gt_u
(get_local $2)
(i32.const 0)
(i32.load
(get_local $0)
)
(block
(i32.store
(call $std:heap/move_memory
(i32.add
(i32.load
(get_local $0)
)
(i32.mul
(get_local $2)
(i32.const 4)
)
(get_local $3)
(i32.const 4)
)
(i32.load
(i32.add
(i32.load
(get_local $0)
)
(i32.mul
(i32.sub
(get_local $2)
(i32.const 1)
)
(i32.const 4)
)
)
(get_local $0)
)
(i32.mul
(get_local $4)
(i32.const 4)
)
)
(set_local $2
(i32.sub
(get_local $2)
(i32.const 1)
(call $std:heap/free_memory
(i32.load
(get_local $0)
)
)
(br $continue|0)
)
)
(i32.store
(get_local $0)
(get_local $3)
)
(i32.store offset=4
(get_local $0)
(get_local $2)
)
)
(call $std:heap/move_memory
(i32.add
(i32.load
(get_local $0)
)
(i32.const 4)
)
(i32.load
(get_local $0)
)
(i32.mul
(get_local $4)
(i32.const 4)
)
)
)
(i32.store
@ -2251,7 +2271,7 @@
)
(i32.store offset=8
(get_local $0)
(tee_local $3
(tee_local $2
(i32.add
(i32.load offset=8
(get_local $0)
@ -2260,7 +2280,7 @@
)
)
)
(get_local $3)
(get_local $2)
)
(func $std:heap/set_memory (; 9 ;) (type $iiiv) (param $0 i32) (param $1 i32) (param $2 i32)
(local $3 i64)

View File

@ -2449,6 +2449,7 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
(block
(set_local $2
(i32.load offset=4
@ -2463,80 +2464,100 @@
)
(get_local $2)
)
(call $std:array/Array#__grow
(get_local $0)
(select
(tee_local $3
(i32.add
(i32.load offset=8
(get_local $0)
)
(i32.const 1)
)
)
(tee_local $4
(i32.mul
(get_local $2)
(i32.const 2)
)
)
(i32.gt_s
(get_local $3)
(get_local $4)
)
)
)
)
(if
(get_local $2)
(block $break|0
(block
(block
(set_local $5
(get_local $2)
)
)
(loop $continue|0
(if
(i32.gt_u
(get_local $5)
(i32.const 0)
)
(block
(i32.store
(select
(tee_local $3
(i32.add
(i32.load
(i32.load offset=8
(get_local $0)
)
(i32.mul
(get_local $5)
(i32.const 4)
)
)
(i32.load
(i32.add
(i32.load
(get_local $0)
)
(i32.mul
(i32.sub
(get_local $5)
(i32.const 1)
)
(i32.const 4)
)
)
)
)
(set_local $5
(i32.sub
(get_local $5)
(i32.const 1)
)
)
(br $continue|0)
(tee_local $4
(i32.mul
(get_local $2)
(i32.const 2)
)
)
(i32.gt_s
(get_local $3)
(get_local $4)
)
)
)
)
(if
(i32.eqz
(i32.gt_s
(get_local $5)
(i32.load offset=4
(get_local $0)
)
)
)
(unreachable)
)
(block
(set_local $6
(call $std:heap/allocate_memory
(i32.mul
(get_local $5)
(i32.const 4)
)
)
)
)
(if
(i32.load
(get_local $0)
)
(block
(call $std:heap/move_memory
(i32.add
(get_local $6)
(i32.const 4)
)
(i32.load
(get_local $0)
)
(i32.mul
(get_local $2)
(i32.const 4)
)
)
(call $std:heap/free_memory
(i32.load
(get_local $0)
)
)
)
)
(i32.store
(get_local $0)
(get_local $6)
)
(i32.store offset=4
(get_local $0)
(get_local $5)
)
)
(call $std:heap/move_memory
(i32.add
(i32.load
(get_local $0)
)
(i32.const 4)
)
(i32.load
(get_local $0)
)
(i32.mul
(get_local $2)
(i32.const 4)
)
)
)
(i32.store