Skip to content

Commit 85da1a5

Browse files
authored
Expose new ASC_RUNTIME compiletime constant. Avoid mem cleanups in std containers for Incremental Runtime (AssemblyScript#2122)
1 parent 1510bac commit 85da1a5

File tree

120 files changed

+5672
-5691
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

120 files changed

+5672
-5691
lines changed

cli/asc.js

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -426,9 +426,15 @@ exports.main = function main(argv, options, callback) {
426426
}
427427

428428
// Set up options
429-
var program;
429+
var program, runtime;
430430
const compilerOptions = __pin(assemblyscript.newOptions());
431+
switch (opts.runtime) {
432+
case "stub": runtime = 0; break;
433+
case "minimal": runtime = 1; break;
434+
default: runtime = 2; break;
435+
}
431436
assemblyscript.setTarget(compilerOptions, 0);
437+
assemblyscript.setRuntime(compilerOptions, runtime);
432438
assemblyscript.setNoAssert(compilerOptions, opts.noAssert);
433439
assemblyscript.setExportMemory(compilerOptions, !opts.noExportMemory);
434440
assemblyscript.setImportMemory(compilerOptions, opts.importMemory);

src/common.ts

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ export namespace CommonNames {
158158
export const constructor = "constructor";
159159
// constants
160160
export const ASC_TARGET = "ASC_TARGET";
161-
export const ASC_NO_TREESHAKING = "ASC_NO_TREESHAKING";
161+
export const ASC_RUNTIME = "ASC_RUNTIME";
162162
export const ASC_NO_ASSERT = "ASC_NO_ASSERT";
163163
export const ASC_MEMORY_BASE = "ASC_MEMORY_BASE";
164164
export const ASC_TABLE_BASE = "ASC_TABLE_BASE";
@@ -256,4 +256,5 @@ export namespace CommonNames {
256256
// shared
257257
export { Feature, featureToString } from "../std/assembly/shared/feature";
258258
export { Target } from "../std/assembly/shared/target";
259+
export { Runtime } from "../std/assembly/shared/runtime";
259260
export { Typeinfo, TypeinfoFlags } from "../std/assembly/shared/typeinfo";

src/compiler.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,7 @@ import {
6464
CommonNames,
6565
Feature,
6666
Target,
67+
Runtime,
6768
featureToString
6869
} from "./common";
6970

@@ -212,6 +213,8 @@ export class Options {
212213

213214
/** WebAssembly target. Defaults to {@link Target.WASM32}. */
214215
target: Target = Target.WASM32;
216+
/** Runtime type. Defaults to Incremental GC. */
217+
runtime: Runtime = Runtime.Incremental;
215218
/** If true, replaces assertions with nops. */
216219
noAssert: bool = false;
217220
/** It true, exports the memory to the embedder. */

src/index.ts

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@
3333
* When compiling to WebAssembly `glue/wasm/index.ts` must be included.
3434
*/
3535

36-
import { Target, Feature } from "./common";
36+
import { Target, Runtime, Feature } from "./common";
3737
import { Compiler, Options } from "./compiler";
3838
import { IDLBuilder, TSDBuilder } from "./definitions";
3939
import { DiagnosticMessage, DiagnosticCategory, formatDiagnosticMessage } from "./diagnostics";
@@ -52,6 +52,10 @@ export function setTarget(options: Options, target: Target): void {
5252
options.target = target;
5353
}
5454

55+
export function setRuntime(options: Options, runtime: Runtime): void {
56+
options.runtime = runtime;
57+
}
58+
5559
/** Sets the `noAssert` option. */
5660
export function setNoAssert(options: Options, noAssert: bool): void {
5761
options.noAssert = noAssert;

src/program.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1013,6 +1013,8 @@ export class Program extends DiagnosticEmitter {
10131013
// register compiler hints
10141014
this.registerConstantInteger(CommonNames.ASC_TARGET, Type.i32,
10151015
i64_new(options.isWasm64 ? Target.WASM64 : Target.WASM32));
1016+
this.registerConstantInteger(CommonNames.ASC_RUNTIME, Type.i32,
1017+
i64_new(options.runtime));
10161018
this.registerConstantInteger(CommonNames.ASC_NO_ASSERT, Type.bool,
10171019
i64_new(options.noAssert ? 1 : 0, 0));
10181020
this.registerConstantInteger(CommonNames.ASC_MEMORY_BASE, Type.i32,

std/assembly/array.ts

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="./rt/index.d.ts" />
22

33
import { BLOCK_MAXSIZE } from "./rt/common";
4+
import { Runtime } from "shared/runtime";
45
import { COMPARATOR, SORT } from "./util/sort";
56
import { REVERSE } from "./util/bytes";
67
import { joinBooleanArray, joinIntegerArray, joinFloatArray, joinStringArray, joinReferenceArray } from "./util/string";
@@ -22,7 +23,11 @@ function ensureCapacity(array: usize, newSize: usize, alignLog2: u32, canGrow: b
2223
let newCapacity = max(newSize, MIN_SIZE) << alignLog2;
2324
if (canGrow) newCapacity = max(min(oldCapacity << 1, BLOCK_MAXSIZE), newCapacity);
2425
let newData = __renew(oldData, newCapacity);
25-
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
26+
// __new / __renew already init memory range as zeros in Incremental runtime.
27+
// So try to avoid this.
28+
if (ASC_RUNTIME != Runtime.Incremental) {
29+
memory.fill(newData + oldCapacity, 0, newCapacity - oldCapacity);
30+
}
2631
if (newData !== oldData) { // oldData has been free'd
2732
store<usize>(array, newData, offsetof<ArrayBufferView>("buffer"));
2833
store<usize>(array, newData, offsetof<ArrayBufferView>("dataStart"));
@@ -66,7 +71,9 @@ export class Array<T> {
6671
// reserve capacity for at least MIN_SIZE elements
6772
var bufferSize = max(<usize>length, MIN_SIZE) << alignof<T>();
6873
var buffer = changetype<ArrayBuffer>(__new(bufferSize, idof<ArrayBuffer>()));
69-
memory.fill(changetype<usize>(buffer), 0, bufferSize);
74+
if (ASC_RUNTIME != Runtime.Incremental) {
75+
memory.fill(changetype<usize>(buffer), 0, bufferSize);
76+
}
7077
this.buffer = buffer; // links
7178
this.dataStart = changetype<usize>(buffer);
7279
this.byteLength = <i32>bufferSize;

std/assembly/arraybuffer.ts

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="./rt/index.d.ts" />
22

33
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
4+
import { Runtime } from "shared/runtime";
45
import { idof } from "./builtins";
56
import { E_INVALIDLENGTH } from "./util/error";
67

@@ -17,7 +18,9 @@ export abstract class ArrayBufferView {
1718
protected constructor(length: i32, alignLog2: i32) {
1819
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignLog2) throw new RangeError(E_INVALIDLENGTH);
1920
var buffer = changetype<ArrayBuffer>(__new(length = length << alignLog2, idof<ArrayBuffer>()));
20-
memory.fill(changetype<usize>(buffer), 0, <usize>length);
21+
if (ASC_RUNTIME != Runtime.Incremental) {
22+
memory.fill(changetype<usize>(buffer), 0, <usize>length);
23+
}
2124
this.buffer = buffer; // links
2225
this.dataStart = changetype<usize>(buffer);
2326
this.byteLength = length;
@@ -48,7 +51,9 @@ export abstract class ArrayBufferView {
4851
constructor(length: i32) {
4952
if (<u32>length > <u32>BLOCK_MAXSIZE) throw new RangeError(E_INVALIDLENGTH);
5053
var buffer = changetype<ArrayBuffer>(__new(<usize>length, idof<ArrayBuffer>()));
51-
memory.fill(changetype<usize>(buffer), 0, <usize>length);
54+
if (ASC_RUNTIME != Runtime.Incremental) {
55+
memory.fill(changetype<usize>(buffer), 0, <usize>length);
56+
}
5257
return buffer;
5358
}
5459

std/assembly/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,8 @@ declare type dataref = object | null;
5252

5353
/** Compiler target. 0 = JS, 1 = WASM32, 2 = WASM64. */
5454
declare const ASC_TARGET: i32;
55+
/** Runtime type. 0 = Stub, 1 = Minimal, 2 = Incremental. */
56+
declare const ASC_RUNTIME: i32;
5557
/** Provided noAssert option. */
5658
declare const ASC_NO_ASSERT: bool;
5759
/** Provided memoryBase option. */

std/assembly/shared/runtime.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
// This file is shared with the compiler and must remain portable
2+
3+
/** Runtime types. */
4+
export enum Runtime {
5+
/** Simple bump allocator without GC. */
6+
Stub = 0,
7+
/** Stop the world semi-automatic GC. */
8+
Minimal = 1,
9+
/** incremental GC. */
10+
Incremental = 2,
11+
}

std/assembly/staticarray.ts

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
/// <reference path="./rt/index.d.ts" />
22

33
import { OBJECT, BLOCK_MAXSIZE, TOTAL_OVERHEAD } from "./rt/common";
4+
import { Runtime } from "shared/runtime";
45
import { COMPARATOR, SORT } from "./util/sort";
56
import { REVERSE } from "./util/bytes";
67
import { idof } from "./builtins";
@@ -90,7 +91,9 @@ export class StaticArray<T> {
9091
if (<u32>length > <u32>BLOCK_MAXSIZE >>> alignof<T>()) throw new RangeError(E_INVALIDLENGTH);
9192
var outSize = <usize>length << alignof<T>();
9293
var out = changetype<StaticArray<T>>(__new(outSize, idof<StaticArray<T>>()));
93-
memory.fill(changetype<usize>(out), 0, outSize);
94+
if (ASC_RUNTIME != Runtime.Incremental) {
95+
memory.fill(changetype<usize>(out), 0, outSize);
96+
}
9497
return out;
9598
}
9699

0 commit comments

Comments
 (0)