Skip to content

Commit

Permalink
Improve stub RT reallocation (#790)
Browse files Browse the repository at this point in the history
  • Loading branch information
dcodeIO authored Aug 28, 2019
1 parent 9c2420b commit 9ba8162
Show file tree
Hide file tree
Showing 39 changed files with 4,175 additions and 1,575 deletions.
51 changes: 32 additions & 19 deletions std/assembly/rt/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,21 +8,26 @@ var startOffset: usize = (__heap_base + AL_MASK) & ~AL_MASK;
@lazy
var offset: usize = startOffset;

// @ts-ignore: decorator
@unsafe @global
export function __alloc(size: usize, id: u32): usize {
if (size > BLOCK_MAXSIZE) unreachable();
var ptr = offset + BLOCK_OVERHEAD;
var newPtr = (ptr + max<usize>(size, 1) + AL_MASK) & ~AL_MASK;
function maybeGrowMemory(newOffset: usize): void {
newOffset = (newOffset + AL_MASK) & ~AL_MASK;
var pagesBefore = memory.size();
if (newPtr > <usize>pagesBefore << 16) {
let pagesNeeded = ((newPtr - ptr + 0xffff) & ~0xffff) >>> 16;
var maxOffset = <usize>pagesBefore << 16;
if (newOffset > maxOffset) {
let pagesNeeded = ((newOffset - maxOffset + 0xffff) & ~0xffff) >>> 16;
let pagesWanted = max(pagesBefore, pagesNeeded); // double memory
if (memory.grow(pagesWanted) < 0) {
if (memory.grow(pagesNeeded) < 0) unreachable(); // out of memory
}
}
offset = newPtr;
offset = newOffset;
}

// @ts-ignore: decorator
@unsafe @global
export function __alloc(size: usize, id: u32): usize {
if (size > BLOCK_MAXSIZE) unreachable();
var ptr = offset + BLOCK_OVERHEAD;
maybeGrowMemory(ptr + max<usize>(size, 1));
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
block.rtId = id;
block.rtSize = size;
Expand All @@ -31,17 +36,25 @@ export function __alloc(size: usize, id: u32): usize {

// @ts-ignore: decorator
@unsafe @global
export function __realloc(ref: usize, size: usize): usize {
var block = changetype<BLOCK>(ref - BLOCK_OVERHEAD);
var oldSize = <usize>block.rtSize;
if (size > oldSize) {
let newRef = __alloc(size, block.rtId);
memory.copy(newRef, ref, oldSize);
ref = newRef;
} else {
block.rtSize = size;
export function __realloc(ptr: usize, size: usize): usize {
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
var actualSize = (<usize>block.rtSize + AL_MASK) & ~AL_MASK;
var isLast = ptr + actualSize == offset;
if (size > actualSize) {
if (isLast) { // maybe grow
if (size > BLOCK_MAXSIZE) unreachable();
maybeGrowMemory(ptr + size);
} else { // copy to new block at least double the size
let newPtr = __alloc(max<usize>(size, actualSize << 1), block.rtId);
memory.copy(newPtr, ptr, actualSize);
block = changetype<BLOCK>((ptr = newPtr) - BLOCK_OVERHEAD);
}
} else if (isLast) { // shrink
offset = (ptr + size + AL_MASK) & ~AL_MASK;
}
return ref;
block.rtSize = size;
return ptr;
}

// @ts-ignore: decorator
Expand Down
98 changes: 52 additions & 46 deletions tests/compiler/call-super.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
(type $FUNCSIG$v (func))
(type $FUNCSIG$ii (func (param i32) (result i32)))
(type $FUNCSIG$iii (func (param i32 i32) (result i32)))
(type $FUNCSIG$vi (func (param i32)))
(type $FUNCSIG$viiii (func (param i32 i32 i32 i32)))
(type $FUNCSIG$i (func (result i32)))
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
Expand All @@ -11,59 +12,42 @@
(global $~lib/rt/stub/offset (mut i32) (i32.const 0))
(export "memory" (memory $0))
(start $start)
(func $~lib/rt/stub/__alloc (; 1 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(func $~lib/rt/stub/maybeGrowMemory (; 1 ;) (type $FUNCSIG$vi) (param $0 i32)
(local $1 i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
local.get $0
i32.const 1073741808
i32.gt_u
if
unreachable
end
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.tee $3
local.get $0
i32.const 1
local.get $0
i32.const 1
i32.gt_u
select
i32.add
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $2
local.tee $0
memory.size
local.tee $4
local.tee $2
i32.const 16
i32.shl
local.tee $1
i32.gt_u
if
local.get $4
local.get $2
local.get $3
local.get $0
local.get $1
i32.sub
i32.const 65535
i32.add
i32.const -65536
i32.and
i32.const 16
i32.shr_u
local.tee $5
local.get $4
local.get $5
local.tee $1
local.get $2
local.get $1
i32.gt_s
select
memory.grow
i32.const 0
i32.lt_s
if
local.get $5
local.get $1
memory.grow
i32.const 0
i32.lt_s
Expand All @@ -72,20 +56,42 @@
end
end
end
local.get $2
local.get $0
global.set $~lib/rt/stub/offset
local.get $3
)
(func $~lib/rt/stub/__alloc (; 2 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
local.get $0
i32.const 1073741808
i32.gt_u
if
unreachable
end
global.get $~lib/rt/stub/offset
i32.const 16
i32.sub
i32.add
local.tee $2
local.get $0
i32.const 1
local.get $0
i32.const 1
i32.gt_u
select
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
i32.const 16
i32.sub
local.tee $3
local.get $1
i32.store offset=8
local.get $2
local.get $3
local.get $0
i32.store offset=12
local.get $3
local.get $2
)
(func $call-super/A#constructor (; 2 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(func $call-super/A#constructor (; 3 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
i32.eqz
if
Expand All @@ -111,7 +117,7 @@
end
local.get $0
)
(func $call-super/B#constructor (; 3 ;) (type $FUNCSIG$i) (result i32)
(func $call-super/B#constructor (; 4 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
i32.const 8
i32.const 4
Expand Down Expand Up @@ -146,7 +152,7 @@
end
local.get $0
)
(func $call-super/test1 (; 4 ;) (type $FUNCSIG$v)
(func $call-super/test1 (; 5 ;) (type $FUNCSIG$v)
(local $0 i32)
call $call-super/B#constructor
local.tee $0
Expand Down Expand Up @@ -174,7 +180,7 @@
unreachable
end
)
(func $call-super/D#constructor (; 5 ;) (type $FUNCSIG$i) (result i32)
(func $call-super/D#constructor (; 6 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
i32.const 8
i32.const 6
Expand Down Expand Up @@ -219,7 +225,7 @@
end
local.get $0
)
(func $call-super/test2 (; 6 ;) (type $FUNCSIG$v)
(func $call-super/test2 (; 7 ;) (type $FUNCSIG$v)
(local $0 i32)
call $call-super/D#constructor
local.tee $0
Expand Down Expand Up @@ -247,7 +253,7 @@
unreachable
end
)
(func $call-super/E#constructor (; 7 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
(func $call-super/E#constructor (; 8 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
i32.eqz
if
Expand All @@ -273,7 +279,7 @@
end
local.get $0
)
(func $call-super/test3 (; 8 ;) (type $FUNCSIG$v)
(func $call-super/test3 (; 9 ;) (type $FUNCSIG$v)
(local $0 i32)
i32.const 8
i32.const 8
Expand Down Expand Up @@ -307,7 +313,7 @@
unreachable
end
)
(func $call-super/H#constructor (; 9 ;) (type $FUNCSIG$i) (result i32)
(func $call-super/H#constructor (; 10 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
i32.const 8
i32.const 10
Expand All @@ -328,7 +334,7 @@
i32.store offset=4
local.get $0
)
(func $call-super/test4 (; 10 ;) (type $FUNCSIG$v)
(func $call-super/test4 (; 11 ;) (type $FUNCSIG$v)
(local $0 i32)
call $call-super/H#constructor
local.tee $0
Expand Down Expand Up @@ -356,7 +362,7 @@
unreachable
end
)
(func $call-super/J#constructor (; 11 ;) (type $FUNCSIG$i) (result i32)
(func $call-super/J#constructor (; 12 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
i32.const 8
i32.const 12
Expand All @@ -377,7 +383,7 @@
i32.store offset=4
local.get $0
)
(func $call-super/test5 (; 12 ;) (type $FUNCSIG$v)
(func $call-super/test5 (; 13 ;) (type $FUNCSIG$v)
(local $0 i32)
call $call-super/J#constructor
local.tee $0
Expand Down Expand Up @@ -405,7 +411,7 @@
unreachable
end
)
(func $start (; 13 ;) (type $FUNCSIG$v)
(func $start (; 14 ;) (type $FUNCSIG$v)
i32.const 64
global.set $~lib/rt/stub/startOffset
global.get $~lib/rt/stub/startOffset
Expand All @@ -416,7 +422,7 @@
call $call-super/test4
call $call-super/test5
)
(func $null (; 14 ;) (type $FUNCSIG$v)
(func $null (; 15 ;) (type $FUNCSIG$v)
nop
)
)
Loading

0 comments on commit 9ba8162

Please sign in to comment.