mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 23:12:19 +00:00
Use move_memory in std Array#unshift
This commit is contained in:
parent
ad469ca445
commit
827bb4afe8
@ -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
@ -2168,16 +2168,18 @@
|
||||
(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)
|
||||
(block
|
||||
(if
|
||||
(i32.le_s
|
||||
(tee_local $2
|
||||
(select
|
||||
(tee_local $3
|
||||
(tee_local $2
|
||||
(i32.add
|
||||
(i32.load offset=8
|
||||
(get_local $0)
|
||||
@ -2185,64 +2187,82 @@
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(tee_local $4
|
||||
(tee_local $3
|
||||
(i32.mul
|
||||
(get_local $2)
|
||||
(get_local $4)
|
||||
(i32.const 2)
|
||||
)
|
||||
)
|
||||
(i32.gt_s
|
||||
(get_local $2)
|
||||
(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
|
||||
(get_local $2)
|
||||
(loop $continue|0
|
||||
(if
|
||||
(i32.gt_u
|
||||
(get_local $2)
|
||||
(i32.const 0)
|
||||
(i32.load
|
||||
(get_local $0)
|
||||
)
|
||||
(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
|
||||
(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.mul
|
||||
(get_local $2)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
(i32.load
|
||||
(i32.add
|
||||
(i32.load
|
||||
(get_local $0)
|
||||
)
|
||||
(i32.mul
|
||||
(i32.sub
|
||||
(get_local $2)
|
||||
(i32.const 1)
|
||||
)
|
||||
(get_local $4)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(set_local $2
|
||||
(i32.sub
|
||||
(get_local $2)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(br $continue|0)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(i32.store
|
||||
(i32.load
|
||||
(get_local $0)
|
||||
@ -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)
|
||||
|
@ -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,8 +2464,9 @@
|
||||
)
|
||||
(get_local $2)
|
||||
)
|
||||
(call $std:array/Array#__grow
|
||||
(get_local $0)
|
||||
(block
|
||||
(block
|
||||
(set_local $5
|
||||
(select
|
||||
(tee_local $3
|
||||
(i32.add
|
||||
@ -2488,57 +2490,76 @@
|
||||
)
|
||||
)
|
||||
(if
|
||||
(get_local $2)
|
||||
(block $break|0
|
||||
(i32.eqz
|
||||
(i32.gt_s
|
||||
(get_local $5)
|
||||
(i32.load offset=4
|
||||
(get_local $0)
|
||||
)
|
||||
)
|
||||
)
|
||||
(unreachable)
|
||||
)
|
||||
(block
|
||||
(set_local $5
|
||||
(get_local $2)
|
||||
(set_local $6
|
||||
(call $std:heap/allocate_memory
|
||||
(i32.mul
|
||||
(get_local $5)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(loop $continue|0
|
||||
(if
|
||||
(i32.gt_u
|
||||
(get_local $5)
|
||||
(i32.const 0)
|
||||
(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.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)
|
||||
)
|
||||
(get_local $2)
|
||||
(i32.const 4)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(set_local $5
|
||||
(i32.sub
|
||||
(get_local $5)
|
||||
(i32.const 1)
|
||||
)
|
||||
)
|
||||
(br $continue|0)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
)
|
||||
(i32.store
|
||||
(i32.load
|
||||
(get_local $0)
|
||||
|
Loading…
x
Reference in New Issue
Block a user