Fix n-body visualization

This commit is contained in:
dcodeIO 2018-07-22 02:55:46 +02:00
parent 813d2e33c8
commit a1e2071da6
15 changed files with 134 additions and 43 deletions

View File

@ -70,10 +70,8 @@ Examples
* **[WASM parser](./lib/parse)**<br /> * **[WASM parser](./lib/parse)**<br />
A WebAssembly binary parser in WebAssembly. A WebAssembly binary parser in WebAssembly.
Benchmarks * **[N-body system](./examples/n-body)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/n-body/index.html) ]<br />
--------- An implementation of the N-body system from the [Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/).
* **[n-body](./examples/n-body)**<br />
Compare performance and produced binary size with n-body example from Computer Language Benchmarks Game.
Building Building
-------- --------

2
dist/asc.js vendored

File diff suppressed because one or more lines are too long

1
dist/asc.js.map vendored

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

File diff suppressed because one or more lines are too long

View File

@ -1,4 +1,4 @@
N-Body-System N-body system
============= =============
An [AssemblyScript](http://assemblyscript.org) example. This is actually a benchmark - visualizing it just so happened. An [AssemblyScript](http://assemblyscript.org) example. This is actually a benchmark - visualizing it just so happened.

View File

@ -203,3 +203,8 @@ export function step(): float {
export function bench(steps: u32): void { export function bench(steps: u32): void {
for (let i: u32 = 0; i < steps; ++i) system.advance(0.01); for (let i: u32 = 0; i < steps; ++i) system.advance(0.01);
} }
export function getBody(index: i32): Body | null {
var bodies = system.bodies;
return <u32>index < <u32>bodies.length ? bodies[index] : null;
}

View File

@ -477,6 +477,19 @@ function asmFunc(global, env, buffer) {
}; };
} }
function assembly_index_getBody($0) {
$0 = $0 | 0;
var $1 = 0, $22 = 0, $20 = 0;
$1 = HEAPU32[assembly_index_system >> 2] | 0;
if ($0 >>> 0 < (HEAP32[($1 + 4 | 0) >> 2] | 0) >>> 0) {
$1 = HEAPU32[$1 >> 2] | 0;
if ($0 >>> 0 < ((HEAP32[$1 >> 2] | 0) >>> 2 | 0) >>> 0) $20 = HEAPU32[(($1 + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
$22 = $20;
} else $22 = 0;
$0 = $22;
return $0 | 0;
}
function start() { function start() {
$lib_allocator_arena_startOffset = 40; $lib_allocator_arena_startOffset = 40;
$lib_allocator_arena_offset = $lib_allocator_arena_startOffset; $lib_allocator_arena_offset = $lib_allocator_arena_startOffset;
@ -521,6 +534,7 @@ function asmFunc(global, env, buffer) {
}), }),
init: assembly_index_init, init: assembly_index_init,
step: assembly_index_step, step: assembly_index_step,
bench: assembly_index_bench bench: assembly_index_bench,
getBody: assembly_index_getBody
}; };
} }

Binary file not shown.

View File

@ -19,6 +19,7 @@
(export "init" (func $assembly/index/init)) (export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step)) (export "step" (func $assembly/index/step))
(export "bench" (func $assembly/index/bench)) (export "bench" (func $assembly/index/bench))
(export "getBody" (func $assembly/index/getBody))
(start $start) (start $start)
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32) (func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32) (local $1 i32)
@ -1356,7 +1357,50 @@
) )
) )
) )
(func $start (; 11 ;) (type $v) (func $assembly/index/getBody (; 11 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
(tee_local $0
(if (result i32)
(i32.lt_u
(get_local $0)
(i32.load offset=4
(tee_local $1
(i32.load
(get_global $assembly/index/system)
)
)
)
)
(if (result i32)
(i32.lt_u
(get_local $0)
(i32.shr_u
(i32.load
(tee_local $1
(i32.load
(get_local $1)
)
)
)
(i32.const 2)
)
)
(i32.load offset=8
(i32.add
(get_local $1)
(i32.shl
(get_local $0)
(i32.const 2)
)
)
)
(unreachable)
)
(i32.const 0)
)
)
)
(func $start (; 12 ;) (type $v)
(set_global $~lib/allocator/arena/startOffset (set_global $~lib/allocator/arena/startOffset
(i32.const 40) (i32.const 40)
) )

View File

@ -32,6 +32,7 @@
(export "init" (func $assembly/index/init)) (export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step)) (export "step" (func $assembly/index/step))
(export "bench" (func $assembly/index/bench)) (export "bench" (func $assembly/index/bench))
(export "getBody" (func $assembly/index/getBody))
(start $start) (start $start)
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32) (func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32) (local $1 i32)
@ -2238,7 +2239,37 @@
) )
) )
) )
(func $start (; 25 ;) (type $v) (func $assembly/index/getBody (; 25 ;) (type $ii) (param $0 i32) (result i32)
(local $1 i32)
;;@ assembly/index.ts:208:2
(set_local $1
;;@ assembly/index.ts:208:15
(i32.load
(get_global $assembly/index/system)
)
)
;;@ assembly/index.ts:209:59
(if (result i32)
;;@ assembly/index.ts:209:9
(i32.lt_u
(get_local $0)
;;@ assembly/index.ts:209:22
(call $~lib/array/Array<Body>#get:length
;;@ assembly/index.ts:209:27
(get_local $1)
)
)
;;@ assembly/index.ts:209:43
(call $~lib/array/Array<Body>#__get
(get_local $1)
;;@ assembly/index.ts:209:50
(get_local $0)
)
;;@ assembly/index.ts:209:59
(i32.const 0)
)
)
(func $start (; 26 ;) (type $v)
(set_global $~lib/allocator/arena/startOffset (set_global $~lib/allocator/arena/startOffset
;;@ ~lib/allocator/arena.ts:12:25 ;;@ ~lib/allocator/arena.ts:12:25
(i32.and (i32.and

View File

@ -39,7 +39,10 @@ ctx.imageSmoothingEnabled = false;
fetch("build/optimized.wasm") fetch("build/optimized.wasm")
.then(response => response.arrayBuffer()) .then(response => response.arrayBuffer())
.then(buffer => WebAssembly.instantiate(buffer, { .then(buffer => WebAssembly.instantiate(buffer, {
env: { abort: function() {} } env: {
memory: new WebAssembly.Memory({ initial: 1 }),
abort: function() { throw Error("abort called"); }
}
})) }))
.then(module => { .then(module => {
var exports = module.instance.exports; var exports = module.instance.exports;

View File

@ -35,21 +35,23 @@ var state = State.INIT;
var white = 0; var white = 0;
// From and to spaces // From and to spaces
var from: ManagedObjectSet; var from: ManagedObjectList;
var to: ManagedObjectSet; var to: ManagedObjectList;
var iter: ManagedObject; var iter: ManagedObject;
// ╒═══════════════ Managed object layout (32-bit) ════════════════╕ // ╒═══════════════ Managed object layout (32-bit) ════════════════╕
// 3 2 1 // 3 2 1
// 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits // 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 9 8 7 6 5 4 3 2 1 0 bits
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐ // ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐
// │ next │ F │ ◄─┐ = nextWithFlags // │ next │0│ C │ ◄─┐ = nextWithColor
// ├─────────────────────────────────────────────────────────┴────┤ │ usize // ├─────────────────────────────────────────────────────────┴────┤ │ usize
// │ prev │ ◄─┘ // │ prev │ ◄─┘
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘ // ├───────────────────────────────────────────────────────────────┤
// │ visitFn │
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘ ◄─ user-space reference
// │ ... data ... │ // │ ... data ... │
// └───────────────────────────────────────────────────────────────┘ // └───────────────────────────────────────────────────────────────┘
// F: flags // C: color
/** Represents a managed object in memory, consisting of a header followed by the object's data. */ /** Represents a managed object in memory, consisting of a header followed by the object's data. */
@unmanaged @unmanaged
@ -61,18 +63,18 @@ class ManagedObject {
/** Pointer to the previous object. */ /** Pointer to the previous object. */
prev: ManagedObject; prev: ManagedObject;
/** Visitor function called with the payload reference. */ /** Visitor function called with the user-space reference. */
visitFn: (ref: usize) => void; visitFn: (ref: usize) => void;
/** Size of a managed object after alignment. */ /** Size of a managed object after alignment. */
static readonly SIZE: usize = (offsetof<ManagedObject>() + AL_MASK) & ~AL_MASK; static readonly SIZE: usize = (offsetof<ManagedObject>() + AL_MASK) & ~AL_MASK;
/** Gets the pointer to the next object in the list. */ /** Gets the pointer to the next object. */
get next(): ManagedObject { get next(): ManagedObject {
return changetype<ManagedObject>(this.nextWithColor & ~3); return changetype<ManagedObject>(this.nextWithColor & ~3);
} }
/** Sets the pointer to the next object in the list. */ /** Sets the pointer to the next object. */
set next(obj: ManagedObject) { set next(obj: ManagedObject) {
this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & 3); this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & 3);
} }
@ -107,9 +109,9 @@ class ManagedObject {
} }
} }
/** A set of managed objects. Used for the from and to spaces. */ /** A list of managed objects. Used for the from and to spaces. */
@unmanaged @unmanaged
class ManagedObjectSet extends ManagedObject { class ManagedObjectList extends ManagedObject {
/** Inserts an object. */ /** Inserts an object. */
push(obj: ManagedObject): void { push(obj: ManagedObject): void {
@ -135,10 +137,10 @@ function step(): void {
switch (state) { switch (state) {
case State.INIT: { case State.INIT: {
if (TRACE) trace("gc~step/INIT"); if (TRACE) trace("gc~step/INIT");
from = changetype<ManagedObjectSet>(memory.allocate(ManagedObject.SIZE)); from = changetype<ManagedObjectList>(memory.allocate(ManagedObject.SIZE));
from.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error from.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error
from.clear(); from.clear();
to = changetype<ManagedObjectSet>(memory.allocate(ManagedObject.SIZE)); to = changetype<ManagedObjectList>(memory.allocate(ManagedObject.SIZE));
to.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error to.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error
to.clear(); to.clear();
iter = to; iter = to;

View File

@ -119,7 +119,7 @@
(get_local $0) (get_local $0)
) )
) )
(func $~lib/collector/itcm/ManagedObjectSet#clear (; 4 ;) (type $iv) (param $0 i32) (func $~lib/collector/itcm/ManagedObjectList#clear (; 4 ;) (type $iv) (param $0 i32)
(i32.store (i32.store
(get_local $0) (get_local $0)
(get_local $0) (get_local $0)
@ -178,7 +178,7 @@
(get_local $1) (get_local $1)
) )
) )
(func $~lib/collector/itcm/ManagedObjectSet#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32) (func $~lib/collector/itcm/ManagedObjectList#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
(local $2 i32) (local $2 i32)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
@ -217,7 +217,7 @@
(call $~lib/collector/itcm/ManagedObject#unlink (call $~lib/collector/itcm/ManagedObject#unlink
(get_local $0) (get_local $0)
) )
(call $~lib/collector/itcm/ManagedObjectSet#push (call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
(get_local $0) (get_local $0)
) )
@ -306,7 +306,7 @@
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
(i32.const -1) (i32.const -1)
) )
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
) )
(set_global $~lib/collector/itcm/to (set_global $~lib/collector/itcm/to
@ -318,7 +318,7 @@
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
(i32.const -1) (i32.const -1)
) )
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
) )
(set_global $~lib/collector/itcm/iter (set_global $~lib/collector/itcm/iter
@ -426,7 +426,7 @@
) )
) )
(block (block
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
) )
(set_global $~lib/collector/itcm/state (set_global $~lib/collector/itcm/state
@ -461,7 +461,7 @@
(get_local $2) (get_local $2)
(get_global $~lib/collector/itcm/white) (get_global $~lib/collector/itcm/white)
) )
(call $~lib/collector/itcm/ManagedObjectSet#push (call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
(get_local $2) (get_local $2)
) )

View File

@ -154,7 +154,7 @@
) )
) )
) )
(func $~lib/collector/itcm/ManagedObjectSet#clear (; 4 ;) (type $iv) (param $0 i32) (func $~lib/collector/itcm/ManagedObjectList#clear (; 4 ;) (type $iv) (param $0 i32)
(i32.store (i32.store
(get_local $0) (get_local $0)
(get_local $0) (get_local $0)
@ -219,7 +219,7 @@
(get_local $1) (get_local $1)
) )
) )
(func $~lib/collector/itcm/ManagedObjectSet#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32) (func $~lib/collector/itcm/ManagedObjectList#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
(local $2 i32) (local $2 i32)
(set_local $2 (set_local $2
(i32.load offset=4 (i32.load offset=4
@ -258,7 +258,7 @@
(call $~lib/collector/itcm/ManagedObject#unlink (call $~lib/collector/itcm/ManagedObject#unlink
(get_local $0) (get_local $0)
) )
(call $~lib/collector/itcm/ManagedObjectSet#push (call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
(get_local $0) (get_local $0)
) )
@ -378,7 +378,7 @@
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
(i32.const -1) (i32.const -1)
) )
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
) )
(set_global $~lib/collector/itcm/to (set_global $~lib/collector/itcm/to
@ -390,7 +390,7 @@
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
(i32.const -1) (i32.const -1)
) )
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
) )
(set_global $~lib/collector/itcm/iter (set_global $~lib/collector/itcm/iter
@ -511,7 +511,7 @@
) )
) )
(block (block
(call $~lib/collector/itcm/ManagedObjectSet#clear (call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to) (get_global $~lib/collector/itcm/to)
) )
(set_global $~lib/collector/itcm/state (set_global $~lib/collector/itcm/state
@ -552,7 +552,7 @@
(get_local $2) (get_local $2)
(get_global $~lib/collector/itcm/white) (get_global $~lib/collector/itcm/white)
) )
(call $~lib/collector/itcm/ManagedObjectSet#push (call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/from) (get_global $~lib/collector/itcm/from)
(get_local $2) (get_local $2)
) )