Always try to eliminate branches if tree-shaking is enabled

This commit is contained in:
dcodeIO
2018-04-24 23:11:11 +02:00
parent 97e7158fff
commit 391db28fe2
38 changed files with 782 additions and 789 deletions

View File

@ -1,5 +1,8 @@
import "allocator/arena";
// From The Computer Language Benchmarks Game
// http://benchmarksgame.alioth.debian.org
const SOLAR_MASS = 4.0 * Math.PI * Math.PI;
const DAYS_PER_YEAR = 365.24;
@ -79,9 +82,9 @@ function Neptune(): Body {
class NBodySystem {
bodies: Body[];
constructor(bodies: Body[]) {
constructor(
public bodies: Body[]
) {
var px = 0.0;
var py = 0.0;
var pz = 0.0;
@ -93,16 +96,17 @@ class NBodySystem {
py += b.vy * m;
pz += b.vz * m;
}
this.bodies = bodies;
this.bodies[0].offsetMomentum(px, py, pz);
bodies[0].offsetMomentum(px, py, pz);
}
advance(dt: f64): void {
var bodies = this.bodies;
var size = bodies.length;
var size: u32 = bodies.length;
// var buffer = changetype<usize>(bodies.buffer_);
for (let i = 0; i < size; ++i) {
for (let i: u32 = 0; i < size; ++i) {
let bodyi = bodies[i];
// let bodyi = load<Body>(buffer + i * sizeof<Body>(), 8);
let ix = bodyi.x;
let iy = bodyi.y;
@ -113,8 +117,10 @@ class NBodySystem {
let bivz = bodyi.vz;
let bodyim = bodyi.mass;
for (let j = i + 1; j < size; ++j) {
for (let j: u32 = i + 1; j < size; ++j) {
let bodyj = bodies[j];
// let bodyj = load<Body>(buffer + j * sizeof<Body>(), 8);
let dx = ix - bodyj.x;
let dy = iy - bodyj.y;
let dz = iz - bodyj.z;
@ -148,9 +154,8 @@ class NBodySystem {
energy(): f64 {
var e = 0.0;
var bodies = this.bodies;
var size = bodies.length;
for (let i = 0; i < size; ++i) {
for (let i: u32 = 0, size: u32 = bodies.length; i < size; ++i) {
let bodyi = bodies[i];
let ix = bodyi.x;
@ -165,7 +170,7 @@ class NBodySystem {
e += 0.5 * bim * (vx * vx + vy * vy + vz * vz);
for (let j = i + 1; j < size; ++j) {
for (let j: u32 = i + 1; j < size; ++j) {
let bodyj = bodies[j];
let dx = ix - bodyj.x;
let dy = iy - bodyj.y;