Skip to content

Optimize stub's realloc #917

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

Merged
merged 5 commits into from
Oct 20, 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
1 change: 1 addition & 0 deletions std/assembly/rt/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare function __alloc(size: usize, id: u32): usize;
declare function __realloc(ref: usize, size: usize): usize;
declare function __free(ref: usize): void;
declare function __reset(): void;
declare function __retain(ref: usize): usize;
declare function __release(ref: usize): void;
declare function __collect(): void;
Expand Down
9 changes: 7 additions & 2 deletions std/assembly/rt/pure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ import { onincrement, ondecrement, onfree, onalloc } from "./rtrace";
// @ts-ignore: decorator
@inline const COLOR_PURPLE: u32 = 3 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const COLOR_RED: u32 = 4 << COLOR_SHIFT;
// @inline const COLOR_RED: u32 = 4 << COLOR_SHIFT;
// @ts-ignore: decorator
@inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT;
// @inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT;

// @ts-ignore: decorator
@inline const VISIT_DECREMENT = 1; // guard 0
Expand Down Expand Up @@ -260,3 +260,8 @@ export function __retain(ref: usize): usize {
export function __release(ref: usize): void {
if (ref > __heap_base) decrement(changetype<Block>(ref - BLOCK_OVERHEAD));
}

// @ts-ignore: decorator
@global @unsafe
export function __reset(): void {
}
23 changes: 11 additions & 12 deletions std/assembly/rt/stub.ts
Original file line number Diff line number Diff line change
Expand Up @@ -44,22 +44,21 @@ export function __realloc(ptr: usize, size: usize): usize {
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
var actualSize = block.mmInfo;
if (DEBUG) assert(block.gcInfo == -1);
var isLast = ptr + actualSize == offset;
var alignedSize = (size + AL_MASK) & ~AL_MASK;
if (size > actualSize) {
if (ptr + actualSize == offset) { // last block: grow
if (isLast) { // last block: grow
if (size > BLOCK_MAXSIZE) unreachable();
actualSize = (size + AL_MASK) & ~AL_MASK;
maybeGrowMemory(ptr + actualSize);
block.mmInfo = actualSize;
maybeGrowMemory(ptr + alignedSize);
block.mmInfo = alignedSize;
} else { // copy to new block at least double the size
actualSize = max<usize>((size + AL_MASK) & ~AL_MASK, actualSize << 1);
let newPtr = __alloc(actualSize, block.rtId);
let newPtr = __alloc(max<usize>(alignedSize, actualSize << 1), block.rtId);
memory.copy(newPtr, ptr, block.rtSize);
block = changetype<BLOCK>((ptr = newPtr) - BLOCK_OVERHEAD);
}
} else if (ptr + actualSize == offset) { // last block: shrink
actualSize = (size + AL_MASK) & ~AL_MASK;
offset = ptr + actualSize;
block.mmInfo = actualSize;
} else if (isLast) { // last block: shrink
offset = ptr + alignedSize;
block.mmInfo = alignedSize;
}
block.rtSize = size;
return ptr;
Expand All @@ -78,7 +77,7 @@ export function __free(ptr: usize): void {

// @ts-ignore: decorator
@unsafe @global
function __reset(): void { // special
export function __reset(): void { // special
offset = startOffset;
}

Expand All @@ -95,7 +94,7 @@ export function __release(ref: usize): void {

// @ts-ignore: decorator
@global @unsafe
export function __visit(ref: usize, cookie: u32): void {
function __visit(ref: usize, cookie: u32): void {
}

// @ts-ignore: decorator
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/number.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1455,7 +1455,7 @@
if
i32.const 0
i32.const 1304
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -1470,7 +1470,7 @@
if
i32.const 0
i32.const 1304
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/number.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3298,7 +3298,7 @@
if
i32.const 0
i32.const 1752
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -3315,7 +3315,7 @@
if
i32.const 0
i32.const 1752
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/resolve-binary.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1630,7 +1630,7 @@
if
i32.const 0
i32.const 1424
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -1645,7 +1645,7 @@
if
i32.const 0
i32.const 1424
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/resolve-binary.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -4494,7 +4494,7 @@
if
i32.const 0
i32.const 1872
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -4511,7 +4511,7 @@
if
i32.const 0
i32.const 1872
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/resolve-elementaccess.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -1540,7 +1540,7 @@
if
i32.const 0
i32.const 1432
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -1555,7 +1555,7 @@
if
i32.const 0
i32.const 1432
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
4 changes: 2 additions & 2 deletions tests/compiler/resolve-elementaccess.untouched.wat
Original file line number Diff line number Diff line change
Expand Up @@ -3430,7 +3430,7 @@
if
i32.const 0
i32.const 1880
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -3447,7 +3447,7 @@
if
i32.const 0
i32.const 1880
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
58 changes: 24 additions & 34 deletions tests/compiler/rt/stub-realloc.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,7 @@
(local $2 i32)
(local $3 i32)
(local $4 i32)
(local $5 i32)
local.get $0
i32.const 15
i32.and
Expand All @@ -320,7 +321,7 @@
i32.sub
local.tee $3
i32.load
local.set $2
local.set $4
local.get $3
i32.load offset=4
i32.const -1
Expand All @@ -333,47 +334,45 @@
call $~lib/builtins/abort
unreachable
end
global.get $~lib/rt/stub/offset
local.get $0
local.get $4
i32.add
i32.eq
local.set $5
local.get $1
local.get $2
i32.const 15
i32.add
i32.const -16
i32.and
local.set $2
local.get $1
local.get $4
i32.gt_u
if
global.get $~lib/rt/stub/offset
local.get $0
local.get $2
i32.add
i32.eq
local.get $5
if
local.get $1
i32.const 1073741808
i32.gt_u
if
unreachable
end
local.get $1
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $2
local.get $0
local.get $2
i32.add
call $~lib/rt/stub/maybeGrowMemory
local.get $3
local.get $2
i32.store
else
local.get $1
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $4
local.get $2
local.get $4
i32.const 1
i32.shl
local.tee $2
local.get $4
local.tee $4
local.get $2
local.get $4
i32.gt_u
select
local.get $3
Expand All @@ -391,19 +390,10 @@
local.set $3
end
else
global.get $~lib/rt/stub/offset
local.get $0
local.get $2
i32.add
i32.eq
local.get $5
if
local.get $1
i32.const 15
i32.add
i32.const -16
i32.and
local.tee $2
local.get $0
local.get $2
i32.add
global.set $~lib/rt/stub/offset
local.get $3
Expand All @@ -429,7 +419,7 @@
if
i32.const 0
i32.const 24
i32.const 71
i32.const 70
i32.const 2
call $~lib/builtins/abort
unreachable
Expand All @@ -444,7 +434,7 @@
if
i32.const 0
i32.const 24
i32.const 73
i32.const 72
i32.const 13
call $~lib/builtins/abort
unreachable
Expand Down
Loading