Skip to content

Commit a4d5f09

Browse files
MaxGraeydcodeIO
authored andcommitted
Optimize stub's realloc (AssemblyScript#917)
1 parent 0bea37c commit a4d5f09

13 files changed

+139
-186
lines changed

std/assembly/rt/index.d.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
declare function __alloc(size: usize, id: u32): usize;
22
declare function __realloc(ref: usize, size: usize): usize;
33
declare function __free(ref: usize): void;
4+
declare function __reset(): void;
45
declare function __retain(ref: usize): usize;
56
declare function __release(ref: usize): void;
67
declare function __collect(): void;

std/assembly/rt/pure.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,9 +46,9 @@ import { onincrement, ondecrement, onfree, onalloc } from "./rtrace";
4646
// @ts-ignore: decorator
4747
@inline const COLOR_PURPLE: u32 = 3 << COLOR_SHIFT;
4848
// @ts-ignore: decorator
49-
@inline const COLOR_RED: u32 = 4 << COLOR_SHIFT;
49+
// @inline const COLOR_RED: u32 = 4 << COLOR_SHIFT;
5050
// @ts-ignore: decorator
51-
@inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT;
51+
// @inline const COLOR_ORANGE: u32 = 5 << COLOR_SHIFT;
5252

5353
// @ts-ignore: decorator
5454
@inline const VISIT_DECREMENT = 1; // guard 0
@@ -260,3 +260,8 @@ export function __retain(ref: usize): usize {
260260
export function __release(ref: usize): void {
261261
if (ref > __heap_base) decrement(changetype<Block>(ref - BLOCK_OVERHEAD));
262262
}
263+
264+
// @ts-ignore: decorator
265+
@global @unsafe
266+
export function __reset(): void {
267+
}

std/assembly/rt/stub.ts

Lines changed: 11 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -44,22 +44,21 @@ export function __realloc(ptr: usize, size: usize): usize {
4444
var block = changetype<BLOCK>(ptr - BLOCK_OVERHEAD);
4545
var actualSize = block.mmInfo;
4646
if (DEBUG) assert(block.gcInfo == -1);
47+
var isLast = ptr + actualSize == offset;
48+
var alignedSize = (size + AL_MASK) & ~AL_MASK;
4749
if (size > actualSize) {
48-
if (ptr + actualSize == offset) { // last block: grow
50+
if (isLast) { // last block: grow
4951
if (size > BLOCK_MAXSIZE) unreachable();
50-
actualSize = (size + AL_MASK) & ~AL_MASK;
51-
maybeGrowMemory(ptr + actualSize);
52-
block.mmInfo = actualSize;
52+
maybeGrowMemory(ptr + alignedSize);
53+
block.mmInfo = alignedSize;
5354
} else { // copy to new block at least double the size
54-
actualSize = max<usize>((size + AL_MASK) & ~AL_MASK, actualSize << 1);
55-
let newPtr = __alloc(actualSize, block.rtId);
55+
let newPtr = __alloc(max<usize>(alignedSize, actualSize << 1), block.rtId);
5656
memory.copy(newPtr, ptr, block.rtSize);
5757
block = changetype<BLOCK>((ptr = newPtr) - BLOCK_OVERHEAD);
5858
}
59-
} else if (ptr + actualSize == offset) { // last block: shrink
60-
actualSize = (size + AL_MASK) & ~AL_MASK;
61-
offset = ptr + actualSize;
62-
block.mmInfo = actualSize;
59+
} else if (isLast) { // last block: shrink
60+
offset = ptr + alignedSize;
61+
block.mmInfo = alignedSize;
6362
}
6463
block.rtSize = size;
6564
return ptr;
@@ -78,7 +77,7 @@ export function __free(ptr: usize): void {
7877

7978
// @ts-ignore: decorator
8079
@unsafe @global
81-
function __reset(): void { // special
80+
export function __reset(): void { // special
8281
offset = startOffset;
8382
}
8483

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

9695
// @ts-ignore: decorator
9796
@global @unsafe
98-
export function __visit(ref: usize, cookie: u32): void {
97+
function __visit(ref: usize, cookie: u32): void {
9998
}
10099

101100
// @ts-ignore: decorator

tests/compiler/number.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1455,7 +1455,7 @@
14551455
if
14561456
i32.const 0
14571457
i32.const 1304
1458-
i32.const 71
1458+
i32.const 70
14591459
i32.const 2
14601460
call $~lib/builtins/abort
14611461
unreachable
@@ -1470,7 +1470,7 @@
14701470
if
14711471
i32.const 0
14721472
i32.const 1304
1473-
i32.const 73
1473+
i32.const 72
14741474
i32.const 13
14751475
call $~lib/builtins/abort
14761476
unreachable

tests/compiler/number.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3298,7 +3298,7 @@
32983298
if
32993299
i32.const 0
33003300
i32.const 1752
3301-
i32.const 71
3301+
i32.const 70
33023302
i32.const 2
33033303
call $~lib/builtins/abort
33043304
unreachable
@@ -3315,7 +3315,7 @@
33153315
if
33163316
i32.const 0
33173317
i32.const 1752
3318-
i32.const 73
3318+
i32.const 72
33193319
i32.const 13
33203320
call $~lib/builtins/abort
33213321
unreachable

tests/compiler/resolve-binary.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1630,7 +1630,7 @@
16301630
if
16311631
i32.const 0
16321632
i32.const 1424
1633-
i32.const 71
1633+
i32.const 70
16341634
i32.const 2
16351635
call $~lib/builtins/abort
16361636
unreachable
@@ -1645,7 +1645,7 @@
16451645
if
16461646
i32.const 0
16471647
i32.const 1424
1648-
i32.const 73
1648+
i32.const 72
16491649
i32.const 13
16501650
call $~lib/builtins/abort
16511651
unreachable

tests/compiler/resolve-binary.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4494,7 +4494,7 @@
44944494
if
44954495
i32.const 0
44964496
i32.const 1872
4497-
i32.const 71
4497+
i32.const 70
44984498
i32.const 2
44994499
call $~lib/builtins/abort
45004500
unreachable
@@ -4511,7 +4511,7 @@
45114511
if
45124512
i32.const 0
45134513
i32.const 1872
4514-
i32.const 73
4514+
i32.const 72
45154515
i32.const 13
45164516
call $~lib/builtins/abort
45174517
unreachable

tests/compiler/resolve-elementaccess.optimized.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1540,7 +1540,7 @@
15401540
if
15411541
i32.const 0
15421542
i32.const 1432
1543-
i32.const 71
1543+
i32.const 70
15441544
i32.const 2
15451545
call $~lib/builtins/abort
15461546
unreachable
@@ -1555,7 +1555,7 @@
15551555
if
15561556
i32.const 0
15571557
i32.const 1432
1558-
i32.const 73
1558+
i32.const 72
15591559
i32.const 13
15601560
call $~lib/builtins/abort
15611561
unreachable

tests/compiler/resolve-elementaccess.untouched.wat

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3430,7 +3430,7 @@
34303430
if
34313431
i32.const 0
34323432
i32.const 1880
3433-
i32.const 71
3433+
i32.const 70
34343434
i32.const 2
34353435
call $~lib/builtins/abort
34363436
unreachable
@@ -3447,7 +3447,7 @@
34473447
if
34483448
i32.const 0
34493449
i32.const 1880
3450-
i32.const 73
3450+
i32.const 72
34513451
i32.const 13
34523452
call $~lib/builtins/abort
34533453
unreachable

tests/compiler/rt/stub-realloc.optimized.wat

Lines changed: 24 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -299,6 +299,7 @@
299299
(local $2 i32)
300300
(local $3 i32)
301301
(local $4 i32)
302+
(local $5 i32)
302303
local.get $0
303304
i32.const 15
304305
i32.and
@@ -320,7 +321,7 @@
320321
i32.sub
321322
local.tee $3
322323
i32.load
323-
local.set $2
324+
local.set $4
324325
local.get $3
325326
i32.load offset=4
326327
i32.const -1
@@ -333,47 +334,45 @@
333334
call $~lib/builtins/abort
334335
unreachable
335336
end
337+
global.get $~lib/rt/stub/offset
338+
local.get $0
339+
local.get $4
340+
i32.add
341+
i32.eq
342+
local.set $5
336343
local.get $1
337-
local.get $2
344+
i32.const 15
345+
i32.add
346+
i32.const -16
347+
i32.and
348+
local.set $2
349+
local.get $1
350+
local.get $4
338351
i32.gt_u
339352
if
340-
global.get $~lib/rt/stub/offset
341-
local.get $0
342-
local.get $2
343-
i32.add
344-
i32.eq
353+
local.get $5
345354
if
346355
local.get $1
347356
i32.const 1073741808
348357
i32.gt_u
349358
if
350359
unreachable
351360
end
352-
local.get $1
353-
i32.const 15
354-
i32.add
355-
i32.const -16
356-
i32.and
357-
local.tee $2
358361
local.get $0
362+
local.get $2
359363
i32.add
360364
call $~lib/rt/stub/maybeGrowMemory
361365
local.get $3
362366
local.get $2
363367
i32.store
364368
else
365-
local.get $1
366-
i32.const 15
367-
i32.add
368-
i32.const -16
369-
i32.and
370-
local.tee $4
371369
local.get $2
370+
local.get $4
372371
i32.const 1
373372
i32.shl
374-
local.tee $2
375-
local.get $4
373+
local.tee $4
376374
local.get $2
375+
local.get $4
377376
i32.gt_u
378377
select
379378
local.get $3
@@ -391,19 +390,10 @@
391390
local.set $3
392391
end
393392
else
394-
global.get $~lib/rt/stub/offset
395-
local.get $0
396-
local.get $2
397-
i32.add
398-
i32.eq
393+
local.get $5
399394
if
400-
local.get $1
401-
i32.const 15
402-
i32.add
403-
i32.const -16
404-
i32.and
405-
local.tee $2
406395
local.get $0
396+
local.get $2
407397
i32.add
408398
global.set $~lib/rt/stub/offset
409399
local.get $3
@@ -429,7 +419,7 @@
429419
if
430420
i32.const 0
431421
i32.const 24
432-
i32.const 71
422+
i32.const 70
433423
i32.const 2
434424
call $~lib/builtins/abort
435425
unreachable
@@ -444,7 +434,7 @@
444434
if
445435
i32.const 0
446436
i32.const 24
447-
i32.const 73
437+
i32.const 72
448438
i32.const 13
449439
call $~lib/builtins/abort
450440
unreachable

0 commit comments

Comments
 (0)