Skip to content

Commit 813d2e3

Browse files
MaxGraeydcodeIO
authored andcommitted
Update n-body sources and binary for rust. Add bench results (AssemblyScript#172)
1 parent 00fb45f commit 813d2e3

15 files changed

+602
-677
lines changed

README.md

+5
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,11 @@ 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.
77+
7378
Building
7479
--------
7580

examples/n-body/README.md

+20
Original file line numberDiff line numberDiff line change
@@ -25,3 +25,23 @@ To run the benchmark:
2525
```
2626
$> npm run test [steps=1000000]
2727
```
28+
29+
Benchmark
30+
=========
31+
32+
***Environment:***
33+
- MacBook Pro (Retina, 15-inch, Late 2013)
34+
- macOS 10.13.5
35+
- node.js v10.6.0
36+
- rustc 1.29.0-nightly (254f8796b 2018-07-13)
37+
38+
***Results:***
39+
40+
| Target | Time, ***ms*** | Size, ***KB*** |
41+
|-------------------------|-----------------|----------------|
42+
| **AssemblyScript WASM** | **3167** | **2** |
43+
| AssemblyScript ASMJS | 3633 | 21* |
44+
| JavaScript | 2628 | 5* |
45+
| Rust WASM | 3876 | 15 |
46+
47+
___* unminified___

examples/n-body/assembly/index.js

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ const compiled = new WebAssembly.Module(
77

88
const imports = {
99
env: {
10+
memory: new WebAssembly.Memory({ initial: 10 }),
1011
abort: (filename, line, column) => {
1112
throw Error("abort called at " + line + ":" + colum);
1213
}

examples/n-body/assembly/index.ts

+21-28
Original file line numberDiff line numberDiff line change
@@ -36,62 +36,60 @@ function Sun(): Body {
3636

3737
function Jupiter(): Body {
3838
return new Body(
39-
4.84143144246472090e+00,
39+
4.84143144246472090e+00,
4040
-1.16032004402742839e+00,
4141
-1.03622044471123109e-01,
42-
1.66007664274403694e-03 * DAYS_PER_YEAR,
43-
7.69901118419740425e-03 * DAYS_PER_YEAR,
42+
1.66007664274403694e-03 * DAYS_PER_YEAR,
43+
7.69901118419740425e-03 * DAYS_PER_YEAR,
4444
-6.90460016972063023e-05 * DAYS_PER_YEAR,
45-
9.54791938424326609e-04 * SOLAR_MASS
45+
9.54791938424326609e-04 * SOLAR_MASS
4646
);
4747
}
4848

4949
function Saturn(): Body {
5050
return new Body(
51-
8.34336671824457987e+00,
52-
4.12479856412430479e+00,
51+
8.34336671824457987e+00,
52+
4.12479856412430479e+00,
5353
-4.03523417114321381e-01,
5454
-2.76742510726862411e-03 * DAYS_PER_YEAR,
55-
4.99852801234917238e-03 * DAYS_PER_YEAR,
56-
2.30417297573763929e-05 * DAYS_PER_YEAR,
57-
2.85885980666130812e-04 * SOLAR_MASS
55+
4.99852801234917238e-03 * DAYS_PER_YEAR,
56+
2.30417297573763929e-05 * DAYS_PER_YEAR,
57+
2.85885980666130812e-04 * SOLAR_MASS
5858
);
5959
}
6060

6161
function Uranus(): Body {
6262
return new Body(
63-
1.28943695621391310e+01,
63+
1.28943695621391310e+01,
6464
-1.51111514016986312e+01,
6565
-2.23307578892655734e-01,
66-
2.96460137564761618e-03 * DAYS_PER_YEAR,
67-
2.37847173959480950e-03 * DAYS_PER_YEAR,
66+
2.96460137564761618e-03 * DAYS_PER_YEAR,
67+
2.37847173959480950e-03 * DAYS_PER_YEAR,
6868
-2.96589568540237556e-05 * DAYS_PER_YEAR,
69-
4.36624404335156298e-05 * SOLAR_MASS
69+
4.36624404335156298e-05 * SOLAR_MASS
7070
);
7171
}
7272

7373
function Neptune(): Body {
7474
return new Body(
75-
1.53796971148509165e+01,
75+
1.53796971148509165e+01,
7676
-2.59193146099879641e+01,
77-
1.79258772950371181e-01,
78-
2.68067772490389322e-03 * DAYS_PER_YEAR,
79-
1.62824170038242295e-03 * DAYS_PER_YEAR,
77+
1.79258772950371181e-01,
78+
2.68067772490389322e-03 * DAYS_PER_YEAR,
79+
1.62824170038242295e-03 * DAYS_PER_YEAR,
8080
-9.51592254519715870e-05 * DAYS_PER_YEAR,
81-
5.15138902046611451e-05 * SOLAR_MASS
81+
5.15138902046611451e-05 * SOLAR_MASS
8282
);
8383
}
8484

8585
class NBodySystem {
8686

87-
constructor(
88-
public bodies: Body[]
89-
) {
87+
constructor(public bodies: Body[]) {
9088
var px: float = 0.0;
9189
var py: float = 0.0;
9290
var pz: float = 0.0;
9391
var size = bodies.length;
94-
for (let i = 0; i < size; i++) {
92+
for (let i = 0; i < size; ++i) {
9593
let b = unchecked(bodies[i]);
9694
let m = b.mass;
9795
px += b.vx * m;
@@ -197,16 +195,11 @@ export function init(): void {
197195
]);
198196
}
199197

200-
export function getBody(index: i32): Body | null {
201-
var bodies = system.bodies;
202-
return <u32>index < <u32>bodies.length ? bodies[index] : null;
203-
}
204-
205198
export function step(): float {
206199
system.advance(0.01);
207200
return system.energy();
208201
}
209202

210203
export function bench(steps: u32): void {
211-
for (let i: u32 = 0; i < steps; i++) system.advance(0.01);
204+
for (let i: u32 = 0; i < steps; ++i) system.advance(0.01);
212205
}

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

-14
Original file line numberDiff line numberDiff line change
@@ -341,19 +341,6 @@ function asmFunc(global, env, buffer) {
341341
assembly_index_system = assembly_index_NBodySystem_constructor(0 | 0, $1 | 0) | 0;
342342
}
343343

344-
function assembly_index_getBody($0) {
345-
$0 = $0 | 0;
346-
var $1 = 0, $22 = 0, $20 = 0;
347-
$1 = HEAPU32[assembly_index_system >> 2] | 0;
348-
if ($0 >>> 0 < (HEAP32[($1 + 4 | 0) >> 2] | 0) >>> 0) {
349-
$1 = HEAPU32[$1 >> 2] | 0;
350-
if ($0 >>> 0 < ((HEAP32[$1 >> 2] | 0) >>> 2 | 0) >>> 0) $20 = HEAPU32[(($1 + ($0 << 2 | 0) | 0) + 8 | 0) >> 2] | 0; else abort();
351-
$22 = $20;
352-
} else $22 = 0;
353-
$0 = $22;
354-
return $0 | 0;
355-
}
356-
357344
function assembly_index_NBodySystem_advance($0, $1) {
358345
$0 = $0 | 0;
359346
$1 = +$1;
@@ -533,7 +520,6 @@ function asmFunc(global, env, buffer) {
533520
}
534521
}),
535522
init: assembly_index_init,
536-
getBody: assembly_index_getBody,
537523
step: assembly_index_step,
538524
bench: assembly_index_bench
539525
};

examples/n-body/build/index.js

+4-11
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
11
"use strict";
22
Object.defineProperty(exports, "__esModule", { value: true });
33
require("allocator/arena");
4-
// From The Computer Language Benchmarks Game
5-
// http://benchmarksgame.alioth.debian.org
6-
const SOLAR_MASS = 4.0 * Math.PI * Math.PI;
4+
const SOLAR_MASS = (4.0 * Math.PI * Math.PI);
75
const DAYS_PER_YEAR = 365.24;
86
class Body {
97
constructor(x, y, z, vx, vy, vz, mass) {
@@ -44,7 +42,7 @@ class NBodySystem {
4442
var py = 0.0;
4543
var pz = 0.0;
4644
var size = bodies.length;
47-
for (let i = 0; i < size; i++) {
45+
for (let i = 0; i < size; ++i) {
4846
let b = unchecked(bodies[i]);
4947
let m = b.mass;
5048
px += b.vx * m;
@@ -107,7 +105,7 @@ class NBodySystem {
107105
let bim = bodyi.mass;
108106
e += 0.5 * bim * (vx * vx + vy * vy + vz * vz);
109107
for (let j = i + 1; j < size; ++j) {
110-
let bodyj = bodies[j];
108+
let bodyj = unchecked(bodies[j]);
111109
let dx = ix - bodyj.x;
112110
let dy = iy - bodyj.y;
113111
let dz = iz - bodyj.z;
@@ -129,18 +127,13 @@ function init() {
129127
]);
130128
}
131129
exports.init = init;
132-
function getBody(index) {
133-
var bodies = system.bodies;
134-
return index < bodies.length ? bodies[index] : null;
135-
}
136-
exports.getBody = getBody;
137130
function step() {
138131
system.advance(0.01);
139132
return system.energy();
140133
}
141134
exports.step = step;
142135
function bench(steps) {
143-
for (let i = 0; i < steps; i++)
136+
for (let i = 0; i < steps; ++i)
144137
system.advance(0.01);
145138
}
146139
exports.bench = bench;

examples/n-body/build/optimized.wasm

-60 Bytes
Binary file not shown.

examples/n-body/build/optimized.wat

+6-50
Original file line numberDiff line numberDiff line change
@@ -10,14 +10,13 @@
1010
(type $iF (func (param i32) (result f64)))
1111
(type $iv (func (param i32)))
1212
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
13+
(import "env" "memory" (memory $0 1))
1314
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
1415
(global $~lib/allocator/arena/offset (mut i32) (i32.const 0))
1516
(global $assembly/index/system (mut i32) (i32.const 0))
16-
(memory $0 1)
1717
(data (i32.const 8) "\0d\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
1818
(export "memory" (memory $0))
1919
(export "init" (func $assembly/index/init))
20-
(export "getBody" (func $assembly/index/getBody))
2120
(export "step" (func $assembly/index/step))
2221
(export "bench" (func $assembly/index/bench))
2322
(start $start)
@@ -823,50 +822,7 @@
823822
)
824823
)
825824
)
826-
(func $assembly/index/getBody (; 7 ;) (type $ii) (param $0 i32) (result i32)
827-
(local $1 i32)
828-
(tee_local $0
829-
(if (result i32)
830-
(i32.lt_u
831-
(get_local $0)
832-
(i32.load offset=4
833-
(tee_local $1
834-
(i32.load
835-
(get_global $assembly/index/system)
836-
)
837-
)
838-
)
839-
)
840-
(if (result i32)
841-
(i32.lt_u
842-
(get_local $0)
843-
(i32.shr_u
844-
(i32.load
845-
(tee_local $1
846-
(i32.load
847-
(get_local $1)
848-
)
849-
)
850-
)
851-
(i32.const 2)
852-
)
853-
)
854-
(i32.load offset=8
855-
(i32.add
856-
(get_local $1)
857-
(i32.shl
858-
(get_local $0)
859-
(i32.const 2)
860-
)
861-
)
862-
)
863-
(unreachable)
864-
)
865-
(i32.const 0)
866-
)
867-
)
868-
)
869-
(func $assembly/index/NBodySystem#advance (; 8 ;) (type $iFv) (param $0 i32) (param $1 f64)
825+
(func $assembly/index/NBodySystem#advance (; 7 ;) (type $iFv) (param $0 i32) (param $1 f64)
870826
(local $2 i32)
871827
(local $3 f64)
872828
(local $4 i32)
@@ -1170,7 +1126,7 @@
11701126
)
11711127
)
11721128
)
1173-
(func $assembly/index/NBodySystem#energy (; 9 ;) (type $iF) (param $0 i32) (result f64)
1129+
(func $assembly/index/NBodySystem#energy (; 8 ;) (type $iF) (param $0 i32) (result f64)
11741130
(local $1 f64)
11751131
(local $2 i32)
11761132
(local $3 i32)
@@ -1367,7 +1323,7 @@
13671323
)
13681324
(get_local $1)
13691325
)
1370-
(func $assembly/index/step (; 10 ;) (type $F) (result f64)
1326+
(func $assembly/index/step (; 9 ;) (type $F) (result f64)
13711327
(call $assembly/index/NBodySystem#advance
13721328
(get_global $assembly/index/system)
13731329
(f64.const 0.01)
@@ -1376,7 +1332,7 @@
13761332
(get_global $assembly/index/system)
13771333
)
13781334
)
1379-
(func $assembly/index/bench (; 11 ;) (type $iv) (param $0 i32)
1335+
(func $assembly/index/bench (; 10 ;) (type $iv) (param $0 i32)
13801336
(local $1 i32)
13811337
(block $break|0
13821338
(loop $repeat|0
@@ -1400,7 +1356,7 @@
14001356
)
14011357
)
14021358
)
1403-
(func $start (; 12 ;) (type $v)
1359+
(func $start (; 11 ;) (type $v)
14041360
(set_global $~lib/allocator/arena/startOffset
14051361
(i32.const 40)
14061362
)
13.7 KB
Binary file not shown.

0 commit comments

Comments
 (0)