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

std/portable/index.d.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,8 @@ declare type valueof<T extends unknown[]> = T[0];
3535

3636
/** Compiler target. 0 = JS, 1 = WASM32, 2 = WASM64. */
3737
declare const ASC_TARGET: i32;
38+
/** Runtime type. 0 = Stub, 1 = Minimal, 2 = Incremental. */
39+
declare const ASC_RUNTIME: i32;
3840
/** Provided noAssert option. */
3941
declare const ASC_NO_ASSERT: bool;
4042
/** Provided memoryBase option. */

std/portable/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ var globalScope = typeof window !== "undefined" && window || typeof global !== "
44
if (typeof globalScope.ASC_TARGET === "undefined") {
55

66
globalScope.ASC_TARGET = 0; // Target.JS
7+
globalScope.ASC_RUNTIME = 0; // Runtime.Stub
78
globalScope.ASC_NO_ASSERT = false;
89
globalScope.ASC_MEMORY_BASE = 0;
910
globalScope.ASC_OPTIMIZE_LEVEL = 3;

tests/compiler/NonNullable.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,9 @@
66
(type $i32_i32_i32_i32_i32_=>_i32 (func (param i32 i32 i32 i32 i32) (result i32)))
77
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
88
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
9+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
10+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
11+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
912
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
1013
(global $NonNullable/z (mut i32) (i32.const 224))
1114
(global $~lib/memory/__data_end i32 (i32.const 300))

tests/compiler/asc-constants.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
ASC_TARGET;
2+
ASC_RUNTIME;
23
ASC_NO_ASSERT;
34
ASC_MEMORY_BASE;
45
ASC_OPTIMIZE_LEVEL;

tests/compiler/asc-constants.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
(module
22
(type $none_=>_none (func))
33
(global $~lib/ASC_TARGET i32 (i32.const 1))
4+
(global $~lib/ASC_RUNTIME i32 (i32.const 2))
45
(global $~lib/ASC_NO_ASSERT i32 (i32.const 0))
56
(global $~lib/ASC_MEMORY_BASE i32 (i32.const 0))
67
(global $~lib/ASC_OPTIMIZE_LEVEL i32 (i32.const 0))
@@ -28,6 +29,8 @@
2829
(func $start:asc-constants
2930
i32.const 1
3031
drop
32+
i32.const 2
33+
drop
3134
i32.const 0
3235
drop
3336
i32.const 0

tests/compiler/assert-nonnull.optimized.wat

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@
5555
if
5656
i32.const 1184
5757
i32.const 1248
58-
i32.const 107
58+
i32.const 114
5959
i32.const 42
6060
call $~lib/builtins/abort
6161
unreachable
@@ -261,7 +261,7 @@
261261
if
262262
i32.const 1184
263263
i32.const 1248
264-
i32.const 107
264+
i32.const 114
265265
i32.const 42
266266
call $~lib/builtins/abort
267267
unreachable
@@ -277,7 +277,7 @@
277277
if
278278
i32.const 1296
279279
i32.const 1248
280-
i32.const 111
280+
i32.const 118
281281
i32.const 40
282282
call $~lib/builtins/abort
283283
unreachable

tests/compiler/assert-nonnull.untouched.wat

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,9 @@
55
(type $i32_i32_i32_i32_=>_none (func (param i32 i32 i32 i32)))
66
(type $none_=>_none (func))
77
(import "env" "abort" (func $~lib/builtins/abort (param i32 i32 i32 i32)))
8+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
9+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
10+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
811
(global $~argumentsLength (mut i32) (i32.const 0))
912
(global $~lib/memory/__data_end i32 (i32.const 380))
1013
(global $~lib/memory/__stack_pointer (mut i32) (i32.const 16764))
@@ -310,7 +313,7 @@
310313
if
311314
i32.const 160
312315
i32.const 224
313-
i32.const 107
316+
i32.const 114
314317
i32.const 42
315318
call $~lib/builtins/abort
316319
unreachable
@@ -335,7 +338,7 @@
335338
if
336339
i32.const 272
337340
i32.const 224
338-
i32.const 111
341+
i32.const 118
339342
i32.const 40
340343
call $~lib/builtins/abort
341344
unreachable
@@ -366,7 +369,7 @@
366369
if
367370
i32.const 160
368371
i32.const 224
369-
i32.const 107
372+
i32.const 114
370373
i32.const 42
371374
call $~lib/builtins/abort
372375
unreachable

tests/compiler/builtins.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,9 @@
2121
(global $builtins/s (mut i32) (i32.const 0))
2222
(global $builtins/fn (mut i32) (i32.const 144))
2323
(global $~argumentsLength (mut i32) (i32.const 0))
24+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
25+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
26+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
2427
(global $~lib/ASC_SHRINK_LEVEL i32 (i32.const 0))
2528
(global $~lib/builtins/i8.MIN_VALUE i32 (i32.const -128))
2629
(global $~lib/builtins/i8.MAX_VALUE i32 (i32.const 127))

tests/compiler/call-super.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
1818
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
1919
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
20+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
21+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
22+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
2023
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
2124
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
2225
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))

tests/compiler/class-implements.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,9 @@
1717
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
1818
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
1919
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
20+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
21+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
22+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
2023
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
2124
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
2225
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))

tests/compiler/class-overloading-cast.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
2020
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
2121
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
22+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
23+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
24+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
2225
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
2326
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
2427
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))

tests/compiler/class-overloading.untouched.wat

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
(global $~lib/rt/itcms/iter (mut i32) (i32.const 0))
2020
(global $~lib/rt/itcms/toSpace (mut i32) (i32.const 0))
2121
(global $~lib/rt/itcms/white (mut i32) (i32.const 0))
22+
(global $~lib/shared/runtime/Runtime.Stub i32 (i32.const 0))
23+
(global $~lib/shared/runtime/Runtime.Minimal i32 (i32.const 1))
24+
(global $~lib/shared/runtime/Runtime.Incremental i32 (i32.const 2))
2225
(global $~lib/rt/itcms/fromSpace (mut i32) (i32.const 0))
2326
(global $~lib/rt/tlsf/ROOT (mut i32) (i32.const 0))
2427
(global $~lib/ASC_LOW_MEMORY_LIMIT i32 (i32.const 0))

0 commit comments

Comments
 (0)