Skip to content

Refactor unsafe #477

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

Closed
wants to merge 6 commits into from
Closed
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
16 changes: 8 additions & 8 deletions std/assembly/array.ts
Original file line number Diff line number Diff line change
@@ -1,10 +1,10 @@
import {
MAX_BLENGTH,
HEADER_SIZE,
allocateUnsafe,
reallocateUnsafe,
reallocateZeroedUnsafe,
LOAD,
STORE
STORE,
allocateZeroedUnsafe
} from "./internal/arraybuffer";

import {
Expand Down Expand Up @@ -44,7 +44,7 @@ export class Array<T> {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
var byteLength = length << alignof<T>();
var buffer = allocateUnsafe(byteLength);
var buffer = allocateZeroedUnsafe(byteLength);
this.buffer_ = buffer;
this.length_ = length;
memory.fill(
Expand All @@ -65,7 +65,7 @@ export class Array<T> {
if (<u32>length > <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length > <u32>MAX_LENGTH) throw new RangeError("Invalid array length");
buffer = reallocateUnsafe(buffer, length << alignof<T>());
buffer = reallocateZeroedUnsafe(buffer, length << alignof<T>());
this.buffer_ = buffer;
}
this.length_ = length;
Expand Down Expand Up @@ -105,7 +105,7 @@ export class Array<T> {
if (<u32>index >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>index >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocateUnsafe(buffer, (index + 1) << alignof<T>());
buffer = reallocateZeroedUnsafe(buffer, (index + 1) << alignof<T>());
this.buffer_ = buffer;
this.length_ = index + 1;
}
Expand Down Expand Up @@ -180,7 +180,7 @@ export class Array<T> {
if (<u32>length >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocateUnsafe(buffer, newLength << alignof<T>());
buffer = reallocateZeroedUnsafe(buffer, newLength << alignof<T>());
this.buffer_ = buffer;
}
this.length_ = newLength;
Expand Down Expand Up @@ -325,7 +325,7 @@ export class Array<T> {
if (<u32>length >= <u32>capacity) {
const MAX_LENGTH = MAX_BLENGTH >>> alignof<T>();
if (<u32>length >= <u32>MAX_LENGTH) throw new Error("Invalid array length");
buffer = reallocateUnsafe(buffer, newLength << alignof<T>());
buffer = reallocateZeroedUnsafe(buffer, newLength << alignof<T>());
capacity = buffer.byteLength >>> alignof<T>();
this.buffer_ = buffer;
}
Expand Down
6 changes: 3 additions & 3 deletions std/assembly/arraybuffer.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import {
HEADER_SIZE,
MAX_BLENGTH,
allocateUnsafe
allocateZeroedUnsafe
} from "./internal/arraybuffer";

import {
Expand Down Expand Up @@ -45,7 +45,7 @@ export class ArrayBuffer {

constructor(length: i32, unsafe: bool = false) {
if (<u32>length > <u32>MAX_BLENGTH) throw new RangeError("Invalid array buffer length");
var buffer = allocateUnsafe(length);
var buffer = allocateZeroedUnsafe(length);
if (!unsafe) memory.fill(changetype<usize>(buffer) + HEADER_SIZE, 0, <usize>length);
return buffer;
}
Expand All @@ -57,7 +57,7 @@ export class ArrayBuffer {
if (end < 0) end = max(len + end, 0);
else end = min(end, len);
var newLen = max(end - begin, 0);
var buffer = allocateUnsafe(newLen);
var buffer = allocateZeroedUnsafe(newLen);
memory.copy(changetype<usize>(buffer) + HEADER_SIZE, changetype<usize>(this) + HEADER_SIZE + begin, newLen);
return buffer;
}
Expand Down
94 changes: 59 additions & 35 deletions std/assembly/internal/arraybuffer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,49 +19,73 @@ function computeSize(byteLength: i32): usize {
}

// Low-level utility

function __gc(ref: usize): void {}

export function allocateUnsafe(byteLength: i32): ArrayBuffer {
@inline function _resize(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
return buffer;
}

@inline function _allocateUnsafe(byteLength: i32): ArrayBuffer {
assert(<u32>byteLength <= <u32>MAX_BLENGTH);
var buffer: usize;
if (isManaged<ArrayBuffer>()) {
buffer = __gc_allocate(computeSize(byteLength), __gc); // tslint:disable-line
} else {
buffer = memory.allocate(computeSize(byteLength));
}
store<i32>(buffer, byteLength, offsetof<ArrayBuffer>("byteLength"));
var buffer: usize = isManaged<ArrayBuffer>()
? __gc_allocate(computeSize(byteLength), __gc) // tslint:disable-line
: memory.allocate(computeSize(byteLength));
return _resize(changetype<ArrayBuffer>(buffer), byteLength);
}

export function allocateZeroedUnsafe(byteLength: i32): ArrayBuffer {
var buffer = _allocateUnsafe(byteLength);

// zero out the buffer
memory.fill(buffer.data, 0, byteLength);
return changetype<ArrayBuffer>(buffer);
}

export function allocateUnsafe(byteLength: i32): ArrayBuffer {
return _allocateUnsafe(byteLength);
}

/**
* This inline grow method allocates a new ArrayBuffer, then frees the old one.
*/
@inline function _growUnsafe(buffer: ArrayBuffer, oldByteLength: i32, newByteLength: i32): ArrayBuffer {
assert(newByteLength > buffer.byteLength && newByteLength <= MAX_BLENGTH);
var newBuffer = _allocateUnsafe(newByteLength);
memory.copy(newBuffer.data, buffer.data, oldByteLength);
if (!isManaged<ArrayBuffer>()) {
memory.free(changetype<usize>(buffer));
}
return newBuffer;
}

/**
* This function perform
*/
@inline function _realocateUnsafe(buffer: ArrayBuffer, oldByteLength: i32, newByteLength: i32): ArrayBuffer {
// non-negative values should be unreachable()
assert(newByteLength >= 0);
// fastest path: buffer should be the same length
if (newByteLength === oldByteLength) return buffer;
// fast path: byteLength is less than or equal to currently allocated size (this is safe)
if (newByteLength <= <i32>(computeSize(oldByteLength) - HEADER_SIZE)) {
return _resize(buffer, newByteLength);
}

// slow path: buffer needs new allocation (_growUnsafe frees the old buffer)
return _growUnsafe(buffer, oldByteLength, newByteLength);
}

export function reallocateUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
return _realocateUnsafe(buffer, buffer.byteLength, newByteLength);
}

export function reallocateZeroedUnsafe(buffer: ArrayBuffer, newByteLength: i32): ArrayBuffer {
var oldByteLength = buffer.byteLength;
if (newByteLength > oldByteLength) {
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"));
} 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
);
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);
store<i32>(changetype<usize>(buffer), newByteLength, offsetof<ArrayBuffer>("byteLength"));
}
buffer = _realocateUnsafe(buffer, oldByteLength, newByteLength);

// zero out the allocated values that weren't copied
memory.fill(buffer.data + oldByteLength, 0, newByteLength - oldByteLength);
return buffer;
}

Expand Down
6 changes: 3 additions & 3 deletions std/assembly/internal/typedarray.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import {
HEADER_SIZE as AB_HEADER_SIZE,
MAX_BLENGTH as AB_MAX_BLENGTH,
allocateUnsafe,
LOAD,
STORE
STORE,
allocateZeroedUnsafe
} from "./arraybuffer";

import {
Expand All @@ -22,7 +22,7 @@ export abstract class TypedArray<T> {
const MAX_LENGTH = <u32>AB_MAX_BLENGTH / sizeof<T>();
if (<u32>length > MAX_LENGTH) throw new RangeError("Invalid typed array length");
var byteLength = length << alignof<T>();
var buffer = allocateUnsafe(byteLength);
var buffer = allocateZeroedUnsafe(byteLength);
memory.fill(changetype<usize>(buffer) + AB_HEADER_SIZE, 0, <usize>byteLength);
this.buffer = buffer;
this.byteOffset = 0;
Expand Down
65 changes: 35 additions & 30 deletions tests/compiler/std/array-literal.optimized.wat
Original file line number Diff line number Diff line change
Expand Up @@ -91,34 +91,7 @@
global.set $~lib/allocator/arena/offset
local.get $1
)
(func $~lib/internal/arraybuffer/allocateUnsafe (; 2 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
local.get $0
i32.const 1073741816
i32.gt_u
if
i32.const 0
i32.const 168
i32.const 26
i32.const 2
call $~lib/env/abort
unreachable
end
i32.const 1
i32.const 32
local.get $0
i32.const 7
i32.add
i32.clz
i32.sub
i32.shl
call $~lib/allocator/arena/__memory_allocate
local.tee $1
local.get $0
i32.store
local.get $1
)
(func $~lib/internal/memory/memset (; 3 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(func $~lib/internal/memory/memset (; 2 ;) (type $FUNCSIG$vii) (param $0 i32) (param $1 i32)
(local $2 i32)
local.get $1
i32.eqz
Expand Down Expand Up @@ -337,11 +310,43 @@
end
end
)
(func $~lib/internal/arraybuffer/allocateZeroedUnsafe (; 3 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
local.get $0
i32.const 1073741816
i32.gt_u
if
i32.const 0
i32.const 168
i32.const 30
i32.const 2
call $~lib/env/abort
unreachable
end
i32.const 1
i32.const 32
local.get $0
i32.const 7
i32.add
i32.clz
i32.sub
i32.shl
call $~lib/allocator/arena/__memory_allocate
local.tee $1
local.get $0
i32.store
local.get $1
i32.const 8
i32.add
local.get $0
call $~lib/internal/memory/memset
local.get $1
)
(func $~lib/array/Array<i8>#constructor (; 4 ;) (type $FUNCSIG$i) (result i32)
(local $0 i32)
(local $1 i32)
i32.const 3
call $~lib/internal/arraybuffer/allocateUnsafe
call $~lib/internal/arraybuffer/allocateZeroedUnsafe
local.set $1
i32.const 8
call $~lib/allocator/arena/__memory_allocate
Expand All @@ -368,7 +373,7 @@
(local $0 i32)
(local $1 i32)
i32.const 12
call $~lib/internal/arraybuffer/allocateUnsafe
call $~lib/internal/arraybuffer/allocateZeroedUnsafe
local.set $1
i32.const 8
call $~lib/allocator/arena/__memory_allocate
Expand Down
Loading