Skip to content

fix & improve reallocateUnsafe #368

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 2 commits into from
Dec 8, 2018
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
20 changes: 9 additions & 11 deletions std/assembly/internal/arraybuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -40,25 +40,23 @@ export function reallocateUnsafe(buffer: ArrayBuffer, newByteLength: i32): Array
assert(newByteLength <= MAX_BLENGTH);
if (newByteLength <= <i32>(computeSize(oldByteLength) - HEADER_SIZE)) { // fast path: zero out additional space
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
memory.fill(
changetype<usize>(buffer) + HEADER_SIZE + <usize>oldByteLength,
0,
<usize>(newByteLength - oldByteLength)
);
} else { // slow path: copy to new buffer
let newBuffer = allocateUnsafe(newByteLength);
memory.copy(
changetype<usize>(newBuffer) + HEADER_SIZE,
changetype<usize>(buffer) + HEADER_SIZE,
<usize>oldByteLength
);
memory.fill(
changetype<usize>(newBuffer) + HEADER_SIZE + <usize>oldByteLength,
0,
<usize>(newByteLength - oldByteLength)
);
return newBuffer;
if (!isManaged<ArrayBuffer>()) {
memory.free(changetype<usize>(buffer));
}
buffer = newBuffer;
}
memory.fill(
changetype<usize>(buffer) + HEADER_SIZE + <usize>oldByteLength,
0,
<usize>(newByteLength - oldByteLength)
);
} else if (newByteLength < oldByteLength) { // fast path: override size
// TBD: worth to copy and release if size is significantly less than before?
assert(newByteLength >= 0);
Expand Down
36 changes: 13 additions & 23 deletions tests/compiler/std/array.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -5,12 +5,12 @@
(type $iiiv (func (param i32 i32 i32)))
(type $iiiii (func (param i32 i32 i32 i32) (result i32)))
(type $iiii (func (param i32 i32 i32) (result i32)))
(type $iv (func (param i32)))
(type $iiv (func (param i32 i32)))
(type $iiif (func (param i32 i32 i32) (result f32)))
(type $F (func (result f64)))
(type $Iv (func (param i64)))
(type $ffi (func (param f32 f32) (result i32)))
(type $iv (func (param i32)))
(type $FFi (func (param f64 f64) (result i32)))
(type $Fi (func (param f64) (result i32)))
(type $iiiiiv (func (param i32 i32 i32 i32 i32)))
Expand Down Expand Up @@ -2229,7 +2229,6 @@
(func $~lib/internal/arraybuffer/reallocateUnsafe (; 14 ;) (type $iii) (param $0 i32) (param $1 i32) (result i32)
(local $2 i32)
(local $3 i32)
(local $4 i32)
get_local $1
get_local $0
i32.load
Expand Down Expand Up @@ -2263,39 +2262,30 @@
get_local $0
get_local $1
i32.store
get_local $0
i32.const 8
i32.add
get_local $2
i32.add
i32.const 0
get_local $1
get_local $2
i32.sub
call $~lib/internal/memory/memset
else
get_local $1
call $~lib/internal/arraybuffer/allocateUnsafe
tee_local $3
i32.const 8
i32.add
tee_local $4
get_local $0
i32.const 8
i32.add
get_local $2
call $~lib/internal/memory/memmove
get_local $2
get_local $4
i32.add
i32.const 0
get_local $1
get_local $2
i32.sub
call $~lib/internal/memory/memset
get_local $3
return
set_local $0
end
get_local $0
i32.const 8
i32.add
get_local $2
i32.add
i32.const 0
get_local $1
get_local $2
i32.sub
call $~lib/internal/memory/memset
else
get_local $1
get_local $2
Expand All @@ -2307,7 +2297,7 @@
if
i32.const 0
i32.const 40
i32.const 64
i32.const 62
i32.const 4
call $~lib/env/abort
unreachable
Expand Down
Loading