1
0
mirror of https://github.com/fluencelabs/assemblyscript synced 2025-06-23 03:31:44 +00:00

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

@ -70,10 +70,8 @@ Examples
* **[WASM parser](./lib/parse)**<br />
A WebAssembly binary parser in WebAssembly.
Benchmarks
---------
* **[n-body](./examples/n-body)**<br />
Compare performance and produced binary size with n-body example from Computer Language Benchmarks Game.
* **[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/).
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

@ -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.

@ -203,3 +203,8 @@ export function step(): float {
export function bench(steps: u32): void {
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;
}

@ -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() {
$lib_allocator_arena_startOffset = 40;
$lib_allocator_arena_offset = $lib_allocator_arena_startOffset;
@ -521,6 +534,7 @@ function asmFunc(global, env, buffer) {
}),
init: assembly_index_init,
step: assembly_index_step,
bench: assembly_index_bench
bench: assembly_index_bench,
getBody: assembly_index_getBody
};
}

Binary file not shown.

@ -19,6 +19,7 @@
(export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step))
(export "bench" (func $assembly/index/bench))
(export "getBody" (func $assembly/index/getBody))
(start $start)
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result 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
(i32.const 40)
)

@ -32,6 +32,7 @@
(export "init" (func $assembly/index/init))
(export "step" (func $assembly/index/step))
(export "bench" (func $assembly/index/bench))
(export "getBody" (func $assembly/index/getBody))
(start $start)
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result 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
;;@ ~lib/allocator/arena.ts:12:25
(i32.and

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

@ -35,21 +35,23 @@ var state = State.INIT;
var white = 0;
// From and to spaces
var from: ManagedObjectSet;
var to: ManagedObjectSet;
var from: ManagedObjectList;
var to: ManagedObjectList;
var iter: ManagedObject;
// ╒═══════════════ Managed object layout (32-bit) ════════════════╕
// 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
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐
// │ next │ F │ ◄─┐ = nextWithFlags
// ├─────────────────────────────────────────────────────────┴────┤ │ usize
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐
// │ next │0│ C │ ◄─┐ = nextWithColor
// ├─────────────────────────────────────────────────────────┴────┤ │ usize
// │ prev │ ◄─┘
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘
// ├───────────────────────────────────────────────────────────────┤
// │ visitFn │
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘ ◄─ user-space reference
// │ ... data ... │
// └───────────────────────────────────────────────────────────────┘
// F: flags
// C: color
/** Represents a managed object in memory, consisting of a header followed by the object's data. */
@unmanaged
@ -61,18 +63,18 @@ class ManagedObject {
/** Pointer to the previous object. */
prev: ManagedObject;
/** Visitor function called with the payload reference. */
/** Visitor function called with the user-space reference. */
visitFn: (ref: usize) => void;
/** Size of a managed object after alignment. */
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 {
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) {
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
class ManagedObjectSet extends ManagedObject {
class ManagedObjectList extends ManagedObject {
/** Inserts an object. */
push(obj: ManagedObject): void {
@ -135,10 +137,10 @@ function step(): void {
switch (state) {
case State.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.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.clear();
iter = to;

@ -119,7 +119,7 @@
(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
(get_local $0)
(get_local $0)
@ -178,7 +178,7 @@
(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)
(set_local $2
(i32.load offset=4
@ -217,7 +217,7 @@
(call $~lib/collector/itcm/ManagedObject#unlink
(get_local $0)
)
(call $~lib/collector/itcm/ManagedObjectSet#push
(call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/to)
(get_local $0)
)
@ -306,7 +306,7 @@
(get_global $~lib/collector/itcm/from)
(i32.const -1)
)
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/from)
)
(set_global $~lib/collector/itcm/to
@ -318,7 +318,7 @@
(get_global $~lib/collector/itcm/to)
(i32.const -1)
)
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to)
)
(set_global $~lib/collector/itcm/iter
@ -426,7 +426,7 @@
)
)
(block
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to)
)
(set_global $~lib/collector/itcm/state
@ -461,7 +461,7 @@
(get_local $2)
(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_local $2)
)

@ -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
(get_local $0)
(get_local $0)
@ -219,7 +219,7 @@
(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)
(set_local $2
(i32.load offset=4
@ -258,7 +258,7 @@
(call $~lib/collector/itcm/ManagedObject#unlink
(get_local $0)
)
(call $~lib/collector/itcm/ManagedObjectSet#push
(call $~lib/collector/itcm/ManagedObjectList#push
(get_global $~lib/collector/itcm/to)
(get_local $0)
)
@ -378,7 +378,7 @@
(get_global $~lib/collector/itcm/from)
(i32.const -1)
)
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/from)
)
(set_global $~lib/collector/itcm/to
@ -390,7 +390,7 @@
(get_global $~lib/collector/itcm/to)
(i32.const -1)
)
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to)
)
(set_global $~lib/collector/itcm/iter
@ -511,7 +511,7 @@
)
)
(block
(call $~lib/collector/itcm/ManagedObjectSet#clear
(call $~lib/collector/itcm/ManagedObjectList#clear
(get_global $~lib/collector/itcm/to)
)
(set_global $~lib/collector/itcm/state
@ -552,7 +552,7 @@
(get_local $2)
(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_local $2)
)