Skip to content

Commit a1e2071

Browse files
committed
Fix n-body visualization
1 parent 813d2e3 commit a1e2071

File tree

15 files changed

+134
-43
lines changed

15 files changed

+134
-43
lines changed

README.md

+2-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,8 @@ Examples
7070
* **[WASM parser](./lib/parse)**<br />
7171
A WebAssembly binary parser in WebAssembly.
7272

73-
Benchmarks
74-
---------
75-
* **[n-body](./examples/n-body)**<br />
76-
Compare performance and produced binary size with n-body example from Computer Language Benchmarks Game.
73+
* **[N-body system](./examples/n-body)** [ [demo](https://rawgit.com/AssemblyScript/assemblyscript/master/examples/n-body/index.html) ]<br />
74+
An implementation of the N-body system from the [Computer Language Benchmarks Game](https://benchmarksgame-team.pages.debian.net/benchmarksgame/).
7775

7876
Building
7977
--------

dist/asc.js

-2
This file was deleted.

dist/asc.js.map

-1
This file was deleted.

dist/assemblyscript.js

-2
This file was deleted.

dist/assemblyscript.js.map

-1
This file was deleted.

examples/n-body/README.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
N-Body-System
1+
N-body system
22
=============
33

44
An [AssemblyScript](http://assemblyscript.org) example. This is actually a benchmark - visualizing it just so happened.

examples/n-body/assembly/index.ts

+5
Original file line numberDiff line numberDiff line change
@@ -203,3 +203,8 @@ export function step(): float {
203203
export function bench(steps: u32): void {
204204
for (let i: u32 = 0; i < steps; ++i) system.advance(0.01);
205205
}
206+
207+
export function getBody(index: i32): Body | null {
208+
var bodies = system.bodies;
209+
return <u32>index < <u32>bodies.length ? bodies[index] : null;
210+
}

examples/n-body/build/index.asm.js

+15-1
Original file line numberDiff line numberDiff line change
@@ -477,6 +477,19 @@ function asmFunc(global, env, buffer) {
477477
};
478478
}
479479

480+
function assembly_index_getBody($0) {
481+
$0 = $0 | 0;
482+
var $1 = 0, $22 = 0, $20 = 0;
483+
$1 = HEAPU32[assembly_index_system >> 2] | 0;
484+
if ($0 >>> 0 < (HEAP32[($1 + 4 | 0) >> 2] | 0) >>> 0) {
485+
$1 = HEAPU32[$1 >> 2] | 0;
486+
if ($0 >>> 0 < ((HEAP32[$1 >> 2] | 0) >>> 2 | 0) >>> 0) $20 = HEAPU32[(($1 + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
487+
$22 = $20;
488+
} else $22 = 0;
489+
$0 = $22;
490+
return $0 | 0;
491+
}
492+
480493
function start() {
481494
$lib_allocator_arena_startOffset = 40;
482495
$lib_allocator_arena_offset = $lib_allocator_arena_startOffset;
@@ -521,6 +534,7 @@ function asmFunc(global, env, buffer) {
521534
}),
522535
init: assembly_index_init,
523536
step: assembly_index_step,
524-
bench: assembly_index_bench
537+
bench: assembly_index_bench,
538+
getBody: assembly_index_getBody
525539
};
526540
}

examples/n-body/build/optimized.wasm

69 Bytes
Binary file not shown.

examples/n-body/build/optimized.wat

+45-1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
(export "init" (func $assembly/index/init))
2020
(export "step" (func $assembly/index/step))
2121
(export "bench" (func $assembly/index/bench))
22+
(export "getBody" (func $assembly/index/getBody))
2223
(start $start)
2324
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32)
2425
(local $1 i32)
@@ -1356,7 +1357,50 @@
13561357
)
13571358
)
13581359
)
1359-
(func $start (; 11 ;) (type $v)
1360+
(func $assembly/index/getBody (; 11 ;) (type $ii) (param $0 i32) (result i32)
1361+
(local $1 i32)
1362+
(tee_local $0
1363+
(if (result i32)
1364+
(i32.lt_u
1365+
(get_local $0)
1366+
(i32.load offset=4
1367+
(tee_local $1
1368+
(i32.load
1369+
(get_global $assembly/index/system)
1370+
)
1371+
)
1372+
)
1373+
)
1374+
(if (result i32)
1375+
(i32.lt_u
1376+
(get_local $0)
1377+
(i32.shr_u
1378+
(i32.load
1379+
(tee_local $1
1380+
(i32.load
1381+
(get_local $1)
1382+
)
1383+
)
1384+
)
1385+
(i32.const 2)
1386+
)
1387+
)
1388+
(i32.load offset=8
1389+
(i32.add
1390+
(get_local $1)
1391+
(i32.shl
1392+
(get_local $0)
1393+
(i32.const 2)
1394+
)
1395+
)
1396+
)
1397+
(unreachable)
1398+
)
1399+
(i32.const 0)
1400+
)
1401+
)
1402+
)
1403+
(func $start (; 12 ;) (type $v)
13601404
(set_global $~lib/allocator/arena/startOffset
13611405
(i32.const 40)
13621406
)

examples/n-body/build/untouched.wat

+32-1
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
(export "init" (func $assembly/index/init))
3333
(export "step" (func $assembly/index/step))
3434
(export "bench" (func $assembly/index/bench))
35+
(export "getBody" (func $assembly/index/getBody))
3536
(start $start)
3637
(func $~lib/allocator/arena/__memory_allocate (; 1 ;) (type $ii) (param $0 i32) (result i32)
3738
(local $1 i32)
@@ -2238,7 +2239,37 @@
22382239
)
22392240
)
22402241
)
2241-
(func $start (; 25 ;) (type $v)
2242+
(func $assembly/index/getBody (; 25 ;) (type $ii) (param $0 i32) (result i32)
2243+
(local $1 i32)
2244+
;;@ assembly/index.ts:208:2
2245+
(set_local $1
2246+
;;@ assembly/index.ts:208:15
2247+
(i32.load
2248+
(get_global $assembly/index/system)
2249+
)
2250+
)
2251+
;;@ assembly/index.ts:209:59
2252+
(if (result i32)
2253+
;;@ assembly/index.ts:209:9
2254+
(i32.lt_u
2255+
(get_local $0)
2256+
;;@ assembly/index.ts:209:22
2257+
(call $~lib/array/Array<Body>#get:length
2258+
;;@ assembly/index.ts:209:27
2259+
(get_local $1)
2260+
)
2261+
)
2262+
;;@ assembly/index.ts:209:43
2263+
(call $~lib/array/Array<Body>#__get
2264+
(get_local $1)
2265+
;;@ assembly/index.ts:209:50
2266+
(get_local $0)
2267+
)
2268+
;;@ assembly/index.ts:209:59
2269+
(i32.const 0)
2270+
)
2271+
)
2272+
(func $start (; 26 ;) (type $v)
22422273
(set_global $~lib/allocator/arena/startOffset
22432274
;;@ ~lib/allocator/arena.ts:12:25
22442275
(i32.and

examples/n-body/index.html

+4-1
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,10 @@ <h1>
3939
fetch("build/optimized.wasm")
4040
.then(response => response.arrayBuffer())
4141
.then(buffer => WebAssembly.instantiate(buffer, {
42-
env: { abort: function() {} }
42+
env: {
43+
memory: new WebAssembly.Memory({ initial: 1 }),
44+
abort: function() { throw Error("abort called"); }
45+
}
4346
}))
4447
.then(module => {
4548
var exports = module.instance.exports;

std/assembly/collector/itcm.ts

+16-14
Original file line numberDiff line numberDiff line change
@@ -35,21 +35,23 @@ var state = State.INIT;
3535
var white = 0;
3636

3737
// From and to spaces
38-
var from: ManagedObjectSet;
39-
var to: ManagedObjectSet;
38+
var from: ManagedObjectList;
39+
var to: ManagedObjectList;
4040
var iter: ManagedObject;
4141

4242
// ╒═══════════════ Managed object layout (32-bit) ════════════════╕
4343
// 3 2 1
4444
// 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
45-
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐
46-
// │ next │ F │ ◄─┐ = nextWithFlags
47-
// ├─────────────────────────────────────────────────────────┴────┤ │ usize
45+
// ├─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┴─┼──┴─┤ ┐
46+
// │ next │0│ C │ ◄─┐ = nextWithColor
47+
// ├─────────────────────────────────────────────────────────┴────┤ │ usize
4848
// │ prev │ ◄─┘
49-
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘
49+
// ├───────────────────────────────────────────────────────────────┤
50+
// │ visitFn │
51+
// ╞═══════════════════════════════════════════════════════════════╡ SIZE ┘ ◄─ user-space reference
5052
// │ ... data ... │
5153
// └───────────────────────────────────────────────────────────────┘
52-
// F: flags
54+
// C: color
5355

5456
/** Represents a managed object in memory, consisting of a header followed by the object's data. */
5557
@unmanaged
@@ -61,18 +63,18 @@ class ManagedObject {
6163
/** Pointer to the previous object. */
6264
prev: ManagedObject;
6365

64-
/** Visitor function called with the payload reference. */
66+
/** Visitor function called with the user-space reference. */
6567
visitFn: (ref: usize) => void;
6668

6769
/** Size of a managed object after alignment. */
6870
static readonly SIZE: usize = (offsetof<ManagedObject>() + AL_MASK) & ~AL_MASK;
6971

70-
/** Gets the pointer to the next object in the list. */
72+
/** Gets the pointer to the next object. */
7173
get next(): ManagedObject {
7274
return changetype<ManagedObject>(this.nextWithColor & ~3);
7375
}
7476

75-
/** Sets the pointer to the next object in the list. */
77+
/** Sets the pointer to the next object. */
7678
set next(obj: ManagedObject) {
7779
this.nextWithColor = changetype<usize>(obj) | (this.nextWithColor & 3);
7880
}
@@ -107,9 +109,9 @@ class ManagedObject {
107109
}
108110
}
109111

110-
/** A set of managed objects. Used for the from and to spaces. */
112+
/** A list of managed objects. Used for the from and to spaces. */
111113
@unmanaged
112-
class ManagedObjectSet extends ManagedObject {
114+
class ManagedObjectList extends ManagedObject {
113115

114116
/** Inserts an object. */
115117
push(obj: ManagedObject): void {
@@ -135,10 +137,10 @@ function step(): void {
135137
switch (state) {
136138
case State.INIT: {
137139
if (TRACE) trace("gc~step/INIT");
138-
from = changetype<ManagedObjectSet>(memory.allocate(ManagedObject.SIZE));
140+
from = changetype<ManagedObjectList>(memory.allocate(ManagedObject.SIZE));
139141
from.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error
140142
from.clear();
141-
to = changetype<ManagedObjectSet>(memory.allocate(ManagedObject.SIZE));
143+
to = changetype<ManagedObjectList>(memory.allocate(ManagedObject.SIZE));
142144
to.visitFn = changetype<(ref: usize) => void>(<u32>-1); // would error
143145
to.clear();
144146
iter = to;

tests/compiler/std/gc.optimized.wat

+7-7
Original file line numberDiff line numberDiff line change
@@ -119,7 +119,7 @@
119119
(get_local $0)
120120
)
121121
)
122-
(func $~lib/collector/itcm/ManagedObjectSet#clear (; 4 ;) (type $iv) (param $0 i32)
122+
(func $~lib/collector/itcm/ManagedObjectList#clear (; 4 ;) (type $iv) (param $0 i32)
123123
(i32.store
124124
(get_local $0)
125125
(get_local $0)
@@ -178,7 +178,7 @@
178178
(get_local $1)
179179
)
180180
)
181-
(func $~lib/collector/itcm/ManagedObjectSet#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
181+
(func $~lib/collector/itcm/ManagedObjectList#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
182182
(local $2 i32)
183183
(set_local $2
184184
(i32.load offset=4
@@ -217,7 +217,7 @@
217217
(call $~lib/collector/itcm/ManagedObject#unlink
218218
(get_local $0)
219219
)
220-
(call $~lib/collector/itcm/ManagedObjectSet#push
220+
(call $~lib/collector/itcm/ManagedObjectList#push
221221
(get_global $~lib/collector/itcm/to)
222222
(get_local $0)
223223
)
@@ -306,7 +306,7 @@
306306
(get_global $~lib/collector/itcm/from)
307307
(i32.const -1)
308308
)
309-
(call $~lib/collector/itcm/ManagedObjectSet#clear
309+
(call $~lib/collector/itcm/ManagedObjectList#clear
310310
(get_global $~lib/collector/itcm/from)
311311
)
312312
(set_global $~lib/collector/itcm/to
@@ -318,7 +318,7 @@
318318
(get_global $~lib/collector/itcm/to)
319319
(i32.const -1)
320320
)
321-
(call $~lib/collector/itcm/ManagedObjectSet#clear
321+
(call $~lib/collector/itcm/ManagedObjectList#clear
322322
(get_global $~lib/collector/itcm/to)
323323
)
324324
(set_global $~lib/collector/itcm/iter
@@ -426,7 +426,7 @@
426426
)
427427
)
428428
(block
429-
(call $~lib/collector/itcm/ManagedObjectSet#clear
429+
(call $~lib/collector/itcm/ManagedObjectList#clear
430430
(get_global $~lib/collector/itcm/to)
431431
)
432432
(set_global $~lib/collector/itcm/state
@@ -461,7 +461,7 @@
461461
(get_local $2)
462462
(get_global $~lib/collector/itcm/white)
463463
)
464-
(call $~lib/collector/itcm/ManagedObjectSet#push
464+
(call $~lib/collector/itcm/ManagedObjectList#push
465465
(get_global $~lib/collector/itcm/from)
466466
(get_local $2)
467467
)

tests/compiler/std/gc.untouched.wat

+7-7
Original file line numberDiff line numberDiff line change
@@ -154,7 +154,7 @@
154154
)
155155
)
156156
)
157-
(func $~lib/collector/itcm/ManagedObjectSet#clear (; 4 ;) (type $iv) (param $0 i32)
157+
(func $~lib/collector/itcm/ManagedObjectList#clear (; 4 ;) (type $iv) (param $0 i32)
158158
(i32.store
159159
(get_local $0)
160160
(get_local $0)
@@ -219,7 +219,7 @@
219219
(get_local $1)
220220
)
221221
)
222-
(func $~lib/collector/itcm/ManagedObjectSet#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
222+
(func $~lib/collector/itcm/ManagedObjectList#push (; 9 ;) (type $iiv) (param $0 i32) (param $1 i32)
223223
(local $2 i32)
224224
(set_local $2
225225
(i32.load offset=4
@@ -258,7 +258,7 @@
258258
(call $~lib/collector/itcm/ManagedObject#unlink
259259
(get_local $0)
260260
)
261-
(call $~lib/collector/itcm/ManagedObjectSet#push
261+
(call $~lib/collector/itcm/ManagedObjectList#push
262262
(get_global $~lib/collector/itcm/to)
263263
(get_local $0)
264264
)
@@ -378,7 +378,7 @@
378378
(get_global $~lib/collector/itcm/from)
379379
(i32.const -1)
380380
)
381-
(call $~lib/collector/itcm/ManagedObjectSet#clear
381+
(call $~lib/collector/itcm/ManagedObjectList#clear
382382
(get_global $~lib/collector/itcm/from)
383383
)
384384
(set_global $~lib/collector/itcm/to
@@ -390,7 +390,7 @@
390390
(get_global $~lib/collector/itcm/to)
391391
(i32.const -1)
392392
)
393-
(call $~lib/collector/itcm/ManagedObjectSet#clear
393+
(call $~lib/collector/itcm/ManagedObjectList#clear
394394
(get_global $~lib/collector/itcm/to)
395395
)
396396
(set_global $~lib/collector/itcm/iter
@@ -511,7 +511,7 @@
511511
)
512512
)
513513
(block
514-
(call $~lib/collector/itcm/ManagedObjectSet#clear
514+
(call $~lib/collector/itcm/ManagedObjectList#clear
515515
(get_global $~lib/collector/itcm/to)
516516
)
517517
(set_global $~lib/collector/itcm/state
@@ -552,7 +552,7 @@
552552
(get_local $2)
553553
(get_global $~lib/collector/itcm/white)
554554
)
555-
(call $~lib/collector/itcm/ManagedObjectSet#push
555+
(call $~lib/collector/itcm/ManagedObjectList#push
556556
(get_global $~lib/collector/itcm/from)
557557
(get_local $2)
558558
)

0 commit comments

Comments
 (0)