mirror of
https://github.com/fluencelabs/assemblyscript
synced 2025-04-25 15:12:12 +00:00
Update n-body sources and binary for rust. Add bench results (#172)
This commit is contained in:
parent
00fb45fcad
commit
813d2e33c8
@ -70,6 +70,11 @@ 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.
|
||||
|
||||
Building
|
||||
--------
|
||||
|
||||
|
@ -25,3 +25,23 @@ To run the benchmark:
|
||||
```
|
||||
$> npm run test [steps=1000000]
|
||||
```
|
||||
|
||||
Benchmark
|
||||
=========
|
||||
|
||||
***Environment:***
|
||||
- MacBook Pro (Retina, 15-inch, Late 2013)
|
||||
- macOS 10.13.5
|
||||
- node.js v10.6.0
|
||||
- rustc 1.29.0-nightly (254f8796b 2018-07-13)
|
||||
|
||||
***Results:***
|
||||
|
||||
| Target | Time, ***ms*** | Size, ***KB*** |
|
||||
|-------------------------|-----------------|----------------|
|
||||
| **AssemblyScript WASM** | **3167** | **2** |
|
||||
| AssemblyScript ASMJS | 3633 | 21* |
|
||||
| JavaScript | 2628 | 5* |
|
||||
| Rust WASM | 3876 | 15 |
|
||||
|
||||
___* unminified___
|
||||
|
@ -7,6 +7,7 @@ const compiled = new WebAssembly.Module(
|
||||
|
||||
const imports = {
|
||||
env: {
|
||||
memory: new WebAssembly.Memory({ initial: 10 }),
|
||||
abort: (filename, line, column) => {
|
||||
throw Error("abort called at " + line + ":" + colum);
|
||||
}
|
||||
|
@ -36,62 +36,60 @@ function Sun(): Body {
|
||||
|
||||
function Jupiter(): Body {
|
||||
return new Body(
|
||||
4.84143144246472090e+00,
|
||||
4.84143144246472090e+00,
|
||||
-1.16032004402742839e+00,
|
||||
-1.03622044471123109e-01,
|
||||
1.66007664274403694e-03 * DAYS_PER_YEAR,
|
||||
7.69901118419740425e-03 * DAYS_PER_YEAR,
|
||||
1.66007664274403694e-03 * DAYS_PER_YEAR,
|
||||
7.69901118419740425e-03 * DAYS_PER_YEAR,
|
||||
-6.90460016972063023e-05 * DAYS_PER_YEAR,
|
||||
9.54791938424326609e-04 * SOLAR_MASS
|
||||
9.54791938424326609e-04 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Saturn(): Body {
|
||||
return new Body(
|
||||
8.34336671824457987e+00,
|
||||
4.12479856412430479e+00,
|
||||
8.34336671824457987e+00,
|
||||
4.12479856412430479e+00,
|
||||
-4.03523417114321381e-01,
|
||||
-2.76742510726862411e-03 * DAYS_PER_YEAR,
|
||||
4.99852801234917238e-03 * DAYS_PER_YEAR,
|
||||
2.30417297573763929e-05 * DAYS_PER_YEAR,
|
||||
2.85885980666130812e-04 * SOLAR_MASS
|
||||
4.99852801234917238e-03 * DAYS_PER_YEAR,
|
||||
2.30417297573763929e-05 * DAYS_PER_YEAR,
|
||||
2.85885980666130812e-04 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Uranus(): Body {
|
||||
return new Body(
|
||||
1.28943695621391310e+01,
|
||||
1.28943695621391310e+01,
|
||||
-1.51111514016986312e+01,
|
||||
-2.23307578892655734e-01,
|
||||
2.96460137564761618e-03 * DAYS_PER_YEAR,
|
||||
2.37847173959480950e-03 * DAYS_PER_YEAR,
|
||||
2.96460137564761618e-03 * DAYS_PER_YEAR,
|
||||
2.37847173959480950e-03 * DAYS_PER_YEAR,
|
||||
-2.96589568540237556e-05 * DAYS_PER_YEAR,
|
||||
4.36624404335156298e-05 * SOLAR_MASS
|
||||
4.36624404335156298e-05 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
function Neptune(): Body {
|
||||
return new Body(
|
||||
1.53796971148509165e+01,
|
||||
1.53796971148509165e+01,
|
||||
-2.59193146099879641e+01,
|
||||
1.79258772950371181e-01,
|
||||
2.68067772490389322e-03 * DAYS_PER_YEAR,
|
||||
1.62824170038242295e-03 * DAYS_PER_YEAR,
|
||||
1.79258772950371181e-01,
|
||||
2.68067772490389322e-03 * DAYS_PER_YEAR,
|
||||
1.62824170038242295e-03 * DAYS_PER_YEAR,
|
||||
-9.51592254519715870e-05 * DAYS_PER_YEAR,
|
||||
5.15138902046611451e-05 * SOLAR_MASS
|
||||
5.15138902046611451e-05 * SOLAR_MASS
|
||||
);
|
||||
}
|
||||
|
||||
class NBodySystem {
|
||||
|
||||
constructor(
|
||||
public bodies: Body[]
|
||||
) {
|
||||
constructor(public bodies: Body[]) {
|
||||
var px: float = 0.0;
|
||||
var py: float = 0.0;
|
||||
var pz: float = 0.0;
|
||||
var size = bodies.length;
|
||||
for (let i = 0; i < size; i++) {
|
||||
for (let i = 0; i < size; ++i) {
|
||||
let b = unchecked(bodies[i]);
|
||||
let m = b.mass;
|
||||
px += b.vx * m;
|
||||
@ -197,16 +195,11 @@ export function init(): void {
|
||||
]);
|
||||
}
|
||||
|
||||
export function getBody(index: i32): Body | null {
|
||||
var bodies = system.bodies;
|
||||
return <u32>index < <u32>bodies.length ? bodies[index] : null;
|
||||
}
|
||||
|
||||
export function step(): float {
|
||||
system.advance(0.01);
|
||||
return system.energy();
|
||||
}
|
||||
|
||||
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);
|
||||
}
|
||||
|
@ -341,19 +341,6 @@ function asmFunc(global, env, buffer) {
|
||||
assembly_index_system = assembly_index_NBodySystem_constructor(0 | 0, $1 | 0) | 0;
|
||||
}
|
||||
|
||||
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 assembly_index_NBodySystem_advance($0, $1) {
|
||||
$0 = $0 | 0;
|
||||
$1 = +$1;
|
||||
@ -533,7 +520,6 @@ function asmFunc(global, env, buffer) {
|
||||
}
|
||||
}),
|
||||
init: assembly_index_init,
|
||||
getBody: assembly_index_getBody,
|
||||
step: assembly_index_step,
|
||||
bench: assembly_index_bench
|
||||
};
|
||||
|
@ -1,9 +1,7 @@
|
||||
"use strict";
|
||||
Object.defineProperty(exports, "__esModule", { value: true });
|
||||
require("allocator/arena");
|
||||
// From The Computer Language Benchmarks Game
|
||||
// http://benchmarksgame.alioth.debian.org
|
||||
const SOLAR_MASS = 4.0 * Math.PI * Math.PI;
|
||||
const SOLAR_MASS = (4.0 * Math.PI * Math.PI);
|
||||
const DAYS_PER_YEAR = 365.24;
|
||||
class Body {
|
||||
constructor(x, y, z, vx, vy, vz, mass) {
|
||||
@ -44,7 +42,7 @@ class NBodySystem {
|
||||
var py = 0.0;
|
||||
var pz = 0.0;
|
||||
var size = bodies.length;
|
||||
for (let i = 0; i < size; i++) {
|
||||
for (let i = 0; i < size; ++i) {
|
||||
let b = unchecked(bodies[i]);
|
||||
let m = b.mass;
|
||||
px += b.vx * m;
|
||||
@ -107,7 +105,7 @@ class NBodySystem {
|
||||
let bim = bodyi.mass;
|
||||
e += 0.5 * bim * (vx * vx + vy * vy + vz * vz);
|
||||
for (let j = i + 1; j < size; ++j) {
|
||||
let bodyj = bodies[j];
|
||||
let bodyj = unchecked(bodies[j]);
|
||||
let dx = ix - bodyj.x;
|
||||
let dy = iy - bodyj.y;
|
||||
let dz = iz - bodyj.z;
|
||||
@ -129,18 +127,13 @@ function init() {
|
||||
]);
|
||||
}
|
||||
exports.init = init;
|
||||
function getBody(index) {
|
||||
var bodies = system.bodies;
|
||||
return index < bodies.length ? bodies[index] : null;
|
||||
}
|
||||
exports.getBody = getBody;
|
||||
function step() {
|
||||
system.advance(0.01);
|
||||
return system.energy();
|
||||
}
|
||||
exports.step = step;
|
||||
function bench(steps) {
|
||||
for (let i = 0; i < steps; i++)
|
||||
for (let i = 0; i < steps; ++i)
|
||||
system.advance(0.01);
|
||||
}
|
||||
exports.bench = bench;
|
||||
|
Binary file not shown.
@ -10,14 +10,13 @@
|
||||
(type $iF (func (param i32) (result f64)))
|
||||
(type $iv (func (param i32)))
|
||||
(import "env" "abort" (func $~lib/env/abort (param i32 i32 i32 i32)))
|
||||
(import "env" "memory" (memory $0 1))
|
||||
(global $~lib/allocator/arena/startOffset (mut i32) (i32.const 0))
|
||||
(global $~lib/allocator/arena/offset (mut i32) (i32.const 0))
|
||||
(global $assembly/index/system (mut i32) (i32.const 0))
|
||||
(memory $0 1)
|
||||
(data (i32.const 8) "\0d\00\00\00~\00l\00i\00b\00/\00a\00r\00r\00a\00y\00.\00t\00s")
|
||||
(export "memory" (memory $0))
|
||||
(export "init" (func $assembly/index/init))
|
||||
(export "getBody" (func $assembly/index/getBody))
|
||||
(export "step" (func $assembly/index/step))
|
||||
(export "bench" (func $assembly/index/bench))
|
||||
(start $start)
|
||||
@ -823,50 +822,7 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $assembly/index/getBody (; 7 ;) (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 $assembly/index/NBodySystem#advance (; 8 ;) (type $iFv) (param $0 i32) (param $1 f64)
|
||||
(func $assembly/index/NBodySystem#advance (; 7 ;) (type $iFv) (param $0 i32) (param $1 f64)
|
||||
(local $2 i32)
|
||||
(local $3 f64)
|
||||
(local $4 i32)
|
||||
@ -1170,7 +1126,7 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $assembly/index/NBodySystem#energy (; 9 ;) (type $iF) (param $0 i32) (result f64)
|
||||
(func $assembly/index/NBodySystem#energy (; 8 ;) (type $iF) (param $0 i32) (result f64)
|
||||
(local $1 f64)
|
||||
(local $2 i32)
|
||||
(local $3 i32)
|
||||
@ -1367,7 +1323,7 @@
|
||||
)
|
||||
(get_local $1)
|
||||
)
|
||||
(func $assembly/index/step (; 10 ;) (type $F) (result f64)
|
||||
(func $assembly/index/step (; 9 ;) (type $F) (result f64)
|
||||
(call $assembly/index/NBodySystem#advance
|
||||
(get_global $assembly/index/system)
|
||||
(f64.const 0.01)
|
||||
@ -1376,7 +1332,7 @@
|
||||
(get_global $assembly/index/system)
|
||||
)
|
||||
)
|
||||
(func $assembly/index/bench (; 11 ;) (type $iv) (param $0 i32)
|
||||
(func $assembly/index/bench (; 10 ;) (type $iv) (param $0 i32)
|
||||
(local $1 i32)
|
||||
(block $break|0
|
||||
(loop $repeat|0
|
||||
@ -1400,7 +1356,7 @@
|
||||
)
|
||||
)
|
||||
)
|
||||
(func $start (; 12 ;) (type $v)
|
||||
(func $start (; 11 ;) (type $v)
|
||||
(set_global $~lib/allocator/arena/startOffset
|
||||
(i32.const 40)
|
||||
)
|
||||
|
Binary file not shown.
File diff suppressed because it is too large
Load Diff
@ -3,8 +3,8 @@
|
||||
"version": "1.0.0",
|
||||
"private": true,
|
||||
"scripts": {
|
||||
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate",
|
||||
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --validate --noDebug --noAssert",
|
||||
"asbuild:untouched": "asc assembly/index.ts -b build/untouched.wasm -t build/untouched.wat --sourceMap --validate --importMemory",
|
||||
"asbuild:optimized": "asc assembly/index.ts -b build/optimized.wasm -t build/optimized.wat -O3 --validate --noDebug --noAssert --importMemory",
|
||||
"asbuild:asmjs": "asc assembly/index.ts -a build/index.asm.js -O3 --validate --noDebug --noAssert",
|
||||
"asbuild": "npm run asbuild:untouched && npm run asbuild:optimized && npm run asbuild:asmjs",
|
||||
"tsbuild": "tsc -p assembly -t ES2017 -m commonjs --outDir build",
|
||||
|
@ -9,4 +9,4 @@ crate-type = ["cdylib"]
|
||||
|
||||
[profile.release]
|
||||
lto = true
|
||||
opt-level = 's'
|
||||
opt-level = 3
|
||||
|
@ -7,6 +7,7 @@ const compiled = new WebAssembly.Module(
|
||||
|
||||
const imports = {
|
||||
env: {
|
||||
memory: new WebAssembly.Memory({ initial: 17 }),
|
||||
abort: (filename, line, column) => {
|
||||
throw Error("abort called at " + line + ":" + colum);
|
||||
}
|
||||
|
@ -1,22 +1,20 @@
|
||||
// Code adopted from https://benchmarksgame-team.pages.debian.net/benchmarksgame/program/nbody-rust-1.html
|
||||
|
||||
#![feature(core_intrinsics, lang_items)]
|
||||
#![feature(core_intrinsics, panic_implementation)]
|
||||
#![no_std]
|
||||
|
||||
#[lang = "panic_fmt"]
|
||||
extern "C" fn panic_fmt(_args: ::core::fmt::Arguments, _file: &'static str, _line: u32) -> ! {
|
||||
use core::intrinsics;
|
||||
unsafe {
|
||||
intrinsics::abort();
|
||||
}
|
||||
use core::intrinsics;
|
||||
use core::panic::PanicInfo;
|
||||
|
||||
#[panic_implementation]
|
||||
#[no_mangle]
|
||||
pub fn panic(_info: &PanicInfo) -> ! {
|
||||
unsafe { intrinsics::abort() }
|
||||
}
|
||||
|
||||
#[inline(always)]
|
||||
fn sqrt(x: f64) -> f64 {
|
||||
use core::intrinsics;
|
||||
unsafe {
|
||||
intrinsics::sqrtf64(x)
|
||||
}
|
||||
unsafe { intrinsics::sqrtf64(x) }
|
||||
}
|
||||
|
||||
const PI: f64 = 3.141592653589793;
|
||||
|
@ -41,38 +41,39 @@ function test(nbody, steps) {
|
||||
}
|
||||
|
||||
var steps = process.argv.length > 2 ? parseInt(process.argv[2], 10) : 20000000;
|
||||
var time;
|
||||
|
||||
console.log("Performing " + steps + " steps (AssemblyScript WASM) ...");
|
||||
time = test(nbodyAS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
function prologue(name, steps) {
|
||||
console.log("Performing " + steps + " steps (" + name + ") ...");
|
||||
}
|
||||
|
||||
console.log("Performing " + steps + " steps (AssemblyScript ASMJS) ...");
|
||||
time = test(nbodyAsmJS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
function epilogue(time) {
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
}
|
||||
|
||||
console.log("Performing " + steps + " steps (JS) ...");
|
||||
time = test(nbodyJS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
console.log("\nCOLD SERIES:\n");
|
||||
|
||||
console.log("Performing " + steps + " steps (Rust WASM) ...");
|
||||
time = test(nbodyRS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
prologue("AssemblyScript WASM", steps);
|
||||
epilogue(test(nbodyAS, steps));
|
||||
|
||||
console.log("\nWARMED UP:\n");
|
||||
prologue("AssemblyScript ASMJS", steps);
|
||||
epilogue(test(nbodyAsmJS, steps));
|
||||
|
||||
console.log("Performing " + steps + " steps (AssemblyScript WASM) ...");
|
||||
time = test(nbodyAS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
prologue("JS", steps);
|
||||
epilogue(test(nbodyJS, steps));
|
||||
|
||||
console.log("Performing " + steps + " steps (AssemblyScript ASMJS) ...");
|
||||
time = test(nbodyAsmJS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
prologue("Rust WASM", steps);
|
||||
epilogue(test(nbodyRS, steps));
|
||||
|
||||
console.log("Performing " + steps + " steps (JS) ...");
|
||||
time = test(nbodyJS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
console.log("\nWARMED UP SERIES:\n");
|
||||
|
||||
console.log("Performing " + steps + " steps (Rust WASM) ...");
|
||||
time = test(nbodyRS, steps);
|
||||
console.log("Took " + (time[0] * 1e3 + time[1] / 1e6) + "ms");
|
||||
prologue("AssemblyScript WASM", steps);
|
||||
epilogue(test(nbodyAS, steps));
|
||||
|
||||
prologue("AssemblyScript ASMJS", steps);
|
||||
epilogue(test(nbodyAsmJS, steps));
|
||||
|
||||
prologue("JS", steps);
|
||||
epilogue(test(nbodyJS, steps));
|
||||
|
||||
prologue("Rust WASM", steps);
|
||||
epilogue(test(nbodyRS, steps));
|
||||
|
Loading…
x
Reference in New Issue
Block a user