Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve stub RT reallocation #799

Merged
merged 1 commit into from
Sep 1, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 26 additions & 12 deletions std/assembly/rt/stub.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { AL_MASK, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE } from "rt/common";
import { AL_MASK, BLOCK, BLOCK_OVERHEAD, BLOCK_MAXSIZE, AL_SIZE, DEBUG } from "rt/common";

// @ts-ignore: decorator
@lazy
Expand All @@ -9,7 +9,7 @@ var startOffset: usize = (__heap_base + AL_MASK) & ~AL_MASK;
var offset: usize = startOffset;

function maybeGrowMemory(newOffset: usize): void {
newOffset = (newOffset + AL_MASK) & ~AL_MASK;
// assumes newOffset is aligned
var pagesBefore = memory.size();
var maxOffset = <usize>pagesBefore << 16;
if (newOffset > maxOffset) {
Expand All @@ -27,8 +27,11 @@ function maybeGrowMemory(newOffset: usize): void {
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 actualSize = max<usize>((size + AL_MASK) & ~AL_MASK, AL_SIZE);
maybeGrowMemory(ptr + actualSize);
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
block.mmInfo = actualSize;
if (DEBUG) block.gcInfo = -1;
block.rtId = id;
block.rtSize = size;
return ptr;
Expand All @@ -39,27 +42,38 @@ export function __alloc(size: usize, id: u32): usize {
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;
var actualSize = block.mmInfo;
if (DEBUG) assert(block.gcInfo == -1);
if (size > actualSize) {
if (isLast) { // maybe grow
if (ptr + actualSize == offset) { // last block: grow
if (size > BLOCK_MAXSIZE) unreachable();
maybeGrowMemory(ptr + size);
actualSize = (size + AL_MASK) & ~AL_MASK;
maybeGrowMemory(ptr + actualSize);
block.mmInfo = actualSize;
} 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);
actualSize = max<usize>((size + AL_MASK) & ~AL_MASK, actualSize << 1);
let newPtr = __alloc(actualSize, block.rtId);
memory.copy(newPtr, ptr, block.rtSize);
block = changetype<BLOCK>((ptr = newPtr) - BLOCK_OVERHEAD);
}
} else if (isLast) { // shrink
offset = (ptr + size + AL_MASK) & ~AL_MASK;
} else if (ptr + actualSize == offset) { // last block: shrink
actualSize = (size + AL_MASK) & ~AL_MASK;
offset = ptr + actualSize;
block.mmInfo = actualSize;
}
block.rtSize = size;
return ptr;
}

// @ts-ignore: decorator
@unsafe @global
export function __free(ref: usize): void {
export function __free(ptr: usize): void {
assert(ptr != 0 && !(ptr & AL_MASK)); // must exist and be aligned
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
if (DEBUG) assert(block.gcInfo == -1);
if (ptr + block.mmInfo == offset) { // last block: discard
offset = changetype<usize>(block);
}
}

// @ts-ignore: decorator
Expand Down
34 changes: 21 additions & 13 deletions tests/compiler/call-super.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,6 @@
(local $1 i32)
(local $2 i32)
local.get $0
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $0
memory.size
local.tee $2
i32.const 16
Expand Down Expand Up @@ -62,6 +57,7 @@
(func $~lib/rt/stub/__alloc (; 2 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
local.get $0
i32.const 1073741808
i32.gt_u
Expand All @@ -71,25 +67,37 @@
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.tee $2
local.get $0
i32.const 1
local.tee $3
local.get $0
i32.const 1
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $2
i32.const 16
local.get $2
i32.const 16
i32.gt_u
select
local.tee $4
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
local.get $3
i32.const 16
i32.sub
local.tee $3
local.tee $2
local.get $4
i32.store
local.get $2
i32.const -1
i32.store offset=4
local.get $2
local.get $1
i32.store offset=8
local.get $3
local.get $2
local.get $0
i32.store offset=12
local.get $2
local.get $3
)
(func $call-super/A#constructor (; 3 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
Expand Down
31 changes: 19 additions & 12 deletions tests/compiler/call-super.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,6 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
local.get $0
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
local.set $0
memory.size
local.set $1
local.get $1
Expand Down Expand Up @@ -81,6 +73,7 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
local.get $0
i32.const 1073741808
i32.gt_u
Expand All @@ -91,25 +84,39 @@
i32.const 16
i32.add
local.set $2
local.get $2
local.get $0
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
local.tee $3
i32.const 1
i32.const 16
local.tee $4
local.get $3
local.get $4
i32.gt_u
select
local.set $5
local.get $2
local.get $5
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
i32.const 16
i32.sub
local.set $5
local.set $6
local.get $6
local.get $5
i32.store
local.get $6
i32.const -1
i32.store offset=4
local.get $6
local.get $1
i32.store offset=8
local.get $5
local.get $6
local.get $0
i32.store offset=12
local.get $2
Expand Down
34 changes: 21 additions & 13 deletions tests/compiler/constructor.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,6 @@
(local $1 i32)
(local $2 i32)
local.get $0
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $0
memory.size
local.tee $2
i32.const 16
Expand Down Expand Up @@ -68,6 +63,7 @@
(func $~lib/rt/stub/__alloc (; 1 ;) (type $FUNCSIG$iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
local.get $0
i32.const 1073741808
i32.gt_u
Expand All @@ -77,25 +73,37 @@
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.tee $2
local.get $0
i32.const 1
local.tee $3
local.get $0
i32.const 1
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $2
i32.const 16
local.get $2
i32.const 16
i32.gt_u
select
local.tee $4
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
local.get $3
i32.const 16
i32.sub
local.tee $3
local.tee $2
local.get $4
i32.store
local.get $2
i32.const -1
i32.store offset=4
local.get $2
local.get $1
i32.store offset=8
local.get $3
local.get $2
local.get $0
i32.store offset=12
local.get $2
local.get $3
)
(func $start:constructor (; 2 ;) (type $FUNCSIG$v)
(local $0 i32)
Expand Down
31 changes: 19 additions & 12 deletions tests/compiler/constructor.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -28,14 +28,6 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
local.get $0
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
local.set $0
memory.size
local.set $1
local.get $1
Expand Down Expand Up @@ -89,6 +81,7 @@
(local $3 i32)
(local $4 i32)
(local $5 i32)
(local $6 i32)
local.get $0
i32.const 1073741808
i32.gt_u
Expand All @@ -99,25 +92,39 @@
i32.const 16
i32.add
local.set $2
local.get $2
local.get $0
i32.const 15
i32.add
i32.const 15
i32.const -1
i32.xor
i32.and
local.tee $3
i32.const 1
i32.const 16
local.tee $4
local.get $3
local.get $4
i32.gt_u
select
local.set $5
local.get $2
local.get $5
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $2
i32.const 16
i32.sub
local.set $5
local.set $6
local.get $6
local.get $5
i32.store
local.get $6
i32.const -1
i32.store offset=4
local.get $6
local.get $1
i32.store offset=8
local.get $5
local.get $6
local.get $0
i32.store offset=12
local.get $2
Expand Down
23 changes: 12 additions & 11 deletions tests/compiler/exports.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,6 @@
(local $1 i32)
(local $2 i32)
local.get $0
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $0
memory.size
local.tee $2
i32.const 16
Expand Down Expand Up @@ -113,20 +108,26 @@
global.get $~lib/rt/stub/offset
i32.const 16
i32.add
local.tee $1
i32.const 4
local.tee $2
i32.const 16
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $1
local.get $2
i32.const 16
i32.sub
local.tee $2
local.tee $1
i32.const 16
i32.store
local.get $1
i32.const -1
i32.store offset=4
local.get $1
local.get $0
i32.store offset=8
local.get $2
local.get $1
i32.const 4
i32.store offset=12
local.get $1
local.get $2
)
(func $exports/Car#get:numDoors (; 5 ;) (type $FUNCSIG$ii) (param $0 i32) (result i32)
local.get $0
Expand Down
Loading