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

File diff suppressed because it is too large Load Diff

View File

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

View File

@ -2449,6 +2449,7 @@
(local $3 i32) (local $3 i32)
(local $4 i32) (local $4 i32)
(local $5 i32) (local $5 i32)
(local $6 i32)
(block (block
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
@ -2463,8 +2464,9 @@
) )
(get_local $2) (get_local $2)
) )
(call $std:array/Array#__grow (block
(get_local $0) (block
(set_local $5
(select (select
(tee_local $3 (tee_local $3
(i32.add (i32.add
@ -2488,57 +2490,76 @@
) )
) )
(if (if
(get_local $2) (i32.eqz
(block $break|0 (i32.gt_s
(get_local $5)
(i32.load offset=4
(get_local $0)
)
)
)
(unreachable)
)
(block (block
(set_local $5 (set_local $6
(get_local $2) (call $std:heap/allocate_memory
(i32.mul
(get_local $5)
(i32.const 4)
)
)
) )
) )
(loop $continue|0
(if (if
(i32.gt_u (i32.load
(get_local $5) (get_local $0)
(i32.const 0)
) )
(block (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 (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.add
(i32.load (i32.load
(get_local $0) (get_local $0)
) )
(i32.mul
(get_local $5)
(i32.const 4) (i32.const 4)
) )
)
(i32.load
(i32.add
(i32.load (i32.load
(get_local $0) (get_local $0)
) )
(i32.mul (i32.mul
(i32.sub (get_local $2)
(get_local $5)
(i32.const 1)
)
(i32.const 4) (i32.const 4)
) )
) )
) )
)
(set_local $5
(i32.sub
(get_local $5)
(i32.const 1)
)
)
(br $continue|0)
)
)
)
)
)
(i32.store (i32.store
(i32.load (i32.load
(get_local $0) (get_local $0)