Skip to content

Commit 8e61e0e

Browse files
MaxGraeydcodeIO
authored andcommitted
Improve reallocateUnsafe and properly free unmanaged objects (#368)
1 parent 3ed83ef commit 8e61e0e

9 files changed

+1283
-1359
lines changed

std/assembly/internal/arraybuffer.ts

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -40,25 +40,23 @@ export function reallocateUnsafe(buffer: ArrayBuffer, newByteLength: i32): Array
4040
assert(newByteLength <= MAX_BLENGTH);
4141
if (newByteLength <= <i32>(computeSize(oldByteLength) - HEADER_SIZE)) { // fast path: zero out additional space
4242
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
43-
memory.fill(
44-
changetype<usize>(buffer) + HEADER_SIZE + <usize>oldByteLength,
45-
0,
46-
<usize>(newByteLength - oldByteLength)
47-
);
4843
} else { // slow path: copy to new buffer
4944
let newBuffer = allocateUnsafe(newByteLength);
5045
memory.copy(
5146
changetype<usize>(newBuffer) + HEADER_SIZE,
5247
changetype<usize>(buffer) + HEADER_SIZE,
5348
<usize>oldByteLength
5449
);
55-
memory.fill(
56-
changetype<usize>(newBuffer) + HEADER_SIZE + <usize>oldByteLength,
57-
0,
58-
<usize>(newByteLength - oldByteLength)
59-
);
60-
return newBuffer;
50+
if (!isManaged<ArrayBuffer>()) {
51+
memory.free(changetype<usize>(buffer));
52+
}
53+
buffer = newBuffer;
6154
}
55+
memory.fill(
56+
changetype<usize>(buffer) + HEADER_SIZE + <usize>oldByteLength,
57+
0,
58+
<usize>(newByteLength - oldByteLength)
59+
);
6260
} else if (newByteLength < oldByteLength) { // fast path: override size
6361
// TBD: worth to copy and release if size is significantly less than before?
6462
assert(newByteLength >= 0);

tests/compiler/std/array.optimized.wat

Lines changed: 13 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -5,12 +5,12 @@
55
(type $iiiv (func (param i32 i32 i32)))
66
(type $iiiii (func (param i32 i32 i32 i32) (result i32)))
77
(type $iiii (func (param i32 i32 i32) (result i32)))
8+
(type $iv (func (param i32)))
89
(type $iiv (func (param i32 i32)))
910
(type $iiif (func (param i32 i32 i32) (result f32)))
1011
(type $F (func (result f64)))
1112
(type $Iv (func (param i64)))
1213
(type $ffi (func (param f32 f32) (result i32)))
13-
(type $iv (func (param i32)))
1414
(type $FFi (func (param f64 f64) (result i32)))
1515
(type $Fi (func (param f64) (result i32)))
1616
(type $iiiiiv (func (param i32 i32 i32 i32 i32)))
@@ -2229,7 +2229,6 @@
22292229
(func $~lib/internal/arraybuffer/reallocateUnsafe (; 14 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
22302230
(local $2 i32)
22312231
(local $3 i32)
2232-
(local $4 i32)
22332232
get_local $1
22342233
get_local $0
22352234
i32.load
@@ -2263,39 +2262,30 @@
22632262
get_local $0
22642263
get_local $1
22652264
i32.store
2266-
get_local $0
2267-
i32.const 8
2268-
i32.add
2269-
get_local $2
2270-
i32.add
2271-
i32.const 0
2272-
get_local $1
2273-
get_local $2
2274-
i32.sub
2275-
call $~lib/internal/memory/memset
22762265
else
22772266
get_local $1
22782267
call $~lib/internal/arraybuffer/allocateUnsafe
22792268
tee_local $3
22802269
i32.const 8
22812270
i32.add
2282-
tee_local $4
22832271
get_local $0
22842272
i32.const 8
22852273
i32.add
22862274
get_local $2
22872275
call $~lib/internal/memory/memmove
2288-
get_local $2
2289-
get_local $4
2290-
i32.add
2291-
i32.const 0
2292-
get_local $1
2293-
get_local $2
2294-
i32.sub
2295-
call $~lib/internal/memory/memset
22962276
get_local $3
2297-
return
2277+
set_local $0
22982278
end
2279+
get_local $0
2280+
i32.const 8
2281+
i32.add
2282+
get_local $2
2283+
i32.add
2284+
i32.const 0
2285+
get_local $1
2286+
get_local $2
2287+
i32.sub
2288+
call $~lib/internal/memory/memset
22992289
else
23002290
get_local $1
23012291
get_local $2
@@ -2307,7 +2297,7 @@
23072297
if
23082298
i32.const 0
23092299
i32.const 40
2310-
i32.const 64
2300+
i32.const 62
23112301
i32.const 4
23122302
call $~lib/env/abort
23132303
unreachable

0 commit comments

Comments
 (0)