Skip to content

Commit f1f6930

Browse files
authored
Update n-body to latest AS version (#19)
1 parent 15900f1 commit f1f6930

File tree

7 files changed

+60
-82
lines changed

7 files changed

+60
-82
lines changed

n-body/README.md

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,26 +28,27 @@ to start a local server.
2828

2929
To run the benchmark:
3030

31-
```
31+
```bash
3232
$> npm test [steps=1000000]
3333
```
3434

3535
Benchmark
36+
3637
=========
3738

3839
***Environment:***
40+
3941
- MacBook Pro (15-inch, 2019)
40-
- macOS 10.15.3
41-
- node.js v13.8.0
42+
- macOS 12.4.0
43+
- node.js v18.4.0
4244
- rustc 1.42.0-nightly (3a3f4a7cb 2019-12-28)
4345

4446
***Results:***
4547

4648
| Target | Time, ***ms*** | Size, ***KB*** |
4749
|-------------------------|-----------------|----------------|
48-
| **AssemblyScript WASM** | **1602** | **1.6** |
49-
| AssemblyScript ASMJS | 2368 | 10* |
50-
| JavaScript | 1616 | 5* |
51-
| Rust WASM | 1618 | 2 |
50+
| **AssemblyScript WASM** | **1599** | **1.8** |
51+
| JavaScript | 11608 | 5* |
52+
| Rust WASM | 1631 | 2 |
5253

5354
___* unminified___

n-body/asconfig.json

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
{
2+
"options": {
3+
"runtime": "stub",
4+
"importMemory": true,
5+
"sourceMap": true
6+
},
7+
"targets": {
8+
"wasm": {
9+
"outFile": "build/as_nbody.wasm",
10+
"textFile": "build/as_nbody.wat",
11+
"optimizeLevel": 3,
12+
"shrinkLevel": 0,
13+
"converge": true,
14+
"noAssert": true
15+
}
16+
}
17+
}

n-body/build/as_nbody.wasm

44 Bytes
Binary file not shown.

n-body/package.json

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -4,17 +4,16 @@
44
"license": "Apache-2.0",
55
"private": true,
66
"scripts": {
7-
"asbuild:wasm": "asc assembly/index.ts -b build/as_nbody.wasm -t build/as_nbody.wat -O3 --runtime stub --noAssert --importMemory",
8-
"asbuild:js": "asc assembly/index.ts -j build/as_nbody.js -O3 --runtime stub --noAssert && node scripts/postprocess-js",
9-
"asbuild": "npm run asbuild:wasm && npm run asbuild:js",
10-
"tsbuild": "tsc -p assembly -t ES2017 -m commonjs --outDir build",
7+
"asbuild:wasm": "asc assembly/index.ts --target wasm",
8+
"asbuild": "npm run asbuild:wasm",
9+
"tsbuild": "tsc -p assembly -t esnext -m commonjs --outDir build && node scripts/postprocess",
1110
"rsbuild": "cd rust && RUSTFLAGS='-C link-arg=-s' cargo +nightly build --release",
1211
"build": "npm run asbuild && npm run tsbuild && npm run rsbuild",
1312
"start": "npx serve",
1413
"test": "node --no-wasm-bounds-checks --no-wasm-stack-checks --expose-gc tests"
1514
},
1615
"devDependencies": {
1716
"assemblyscript": "latest",
18-
"typescript": "^3.9.7"
17+
"typescript": "^4.7.4"
1918
}
2019
}

n-body/scripts/postprocess-js.js

Lines changed: 0 additions & 8 deletions
This file was deleted.

n-body/scripts/postprocess.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
const fs = require("fs");
2+
const path = require("path");
3+
4+
const filename = path.join(__dirname, "..", "build" , "index.js");
5+
const source = fs.readFileSync(filename, { encoding: "utf8" });
6+
7+
function removeUnchecked(str) {
8+
return str
9+
.replace(/unchecked\((.*?)\)/g, (_, _1) => _1)
10+
.replace(/unchecked\((.*?)\)/g, (_, _1) => _1);
11+
}
12+
13+
fs.writeFileSync(filename, removeUnchecked(source));

n-body/tests/index.js

Lines changed: 18 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,41 +1,13 @@
11
const fs = require("fs");
22

3-
// Load WASM version
3+
// Load AssemblyScript version
44
const nbodyAS = require("../assembly/index.js");
5-
var nbodyRS;
6-
try {
7-
nbodyRS = require("../rust/index.js");
8-
} catch (e) {}
5+
// Load Rust/wasm version
6+
let nbodyRS;
7+
try { nbodyRS = require("../rust/index.js"); } catch (e) {}
98

109
// Load JS version
11-
var src = fs.readFileSync(__dirname + "/../build/as_nbody.js", "utf8")
12-
.replace(/const retasmFunc[^$]*$/g, "");
13-
14-
const nbodyAS_JS = eval(src + ";asmFunc")({
15-
Int8Array,
16-
Int16Array,
17-
Int32Array,
18-
Uint8Array,
19-
Uint16Array,
20-
Uint32Array,
21-
Float32Array,
22-
Float64Array,
23-
Math
24-
}, {
25-
abort: () => { throw Error(); }
26-
}, new ArrayBuffer(0x10000));
27-
28-
// Load JS version
29-
src = fs.readFileSync(__dirname + "/../build/index.js", "utf8");
30-
const scopeJS = {
31-
require: () => {},
32-
exports: {},
33-
unchecked: expr => expr
34-
};
35-
36-
const nbodyJS = new Function(
37-
...Object.keys(scopeJS).concat(src + "\nreturn exports"))(...Object.values(scopeJS)
38-
);
10+
const nbodyJS = require("../build/index.js");
3911

4012
function gcCollect() {
4113
if (global.gc) {
@@ -58,45 +30,29 @@ function test(nbody, steps) {
5830
return t;
5931
}
6032

61-
var steps = process.argv.length > 2 ? parseInt(process.argv[2], 10) : 20000000;
62-
63-
function prologue(name, steps) {
64-
console.log("Performing " + steps + " steps (" + name + ") ...");
65-
}
33+
const steps = process.argv.length > 2
34+
? parseInt(process.argv[2], 10)
35+
: 20000000;
6636

67-
function epilogue(time) {
68-
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
37+
function bench(name, fn) {
38+
console.log(`Performing ${steps} steps (${name}) ...`);
39+
const time = test(fn, steps);
40+
console.log(`Took ${(time[0] * 1e3 + time[1] / 1e6).toFixed(0)} ms`);
6941
}
7042

7143
console.log("\nCOLD SERIES:\n");
7244

73-
prologue("AssemblyScript WASM", steps);
74-
epilogue(test(nbodyAS, steps));
75-
76-
prologue("AssemblyScript JS", steps);
77-
epilogue(test(nbodyAS_JS, steps));
78-
79-
prologue("JS", steps);
80-
epilogue(test(nbodyJS, steps));
81-
45+
bench("AssemblyScript WASM", nbodyAS);
46+
bench("JS", nbodyJS);
8247
if (nbodyRS) {
83-
prologue("Rust WASM", steps);
84-
epilogue(test(nbodyRS, steps));
48+
bench("Rust WASM", nbodyRS);
8549
}
8650

8751
console.log("\nWARMED UP SERIES:\n");
8852
sleep(1000);
8953

90-
prologue("AssemblyScript WASM", steps);
91-
epilogue(test(nbodyAS, steps));
92-
93-
prologue("AssemblyScript JS", steps);
94-
epilogue(test(nbodyAS_JS, steps));
95-
96-
prologue("JS", steps);
97-
epilogue(test(nbodyJS, steps));
98-
54+
bench("AssemblyScript WASM", nbodyAS);
55+
bench("JS", nbodyJS);
9956
if (nbodyRS) {
100-
prologue("Rust WASM", steps);
101-
epilogue(test(nbodyRS, steps));
57+
bench("Rust WASM", nbodyRS);
10258
}

0 commit comments

Comments
 (0)