WebAssembly-benchmark
WebAssembly-benchmark copied to clipboard
Quality of bench
I have some investigation into code. Actually only for CollisionDetection test and found some issues. In my opinion.
- jsCollisionDetection is wrong functionality. It shoud be:
function jsCollisionDetection(positions, radiuses, res, n) {
var count = 0;
for (var i = 0; i < n; i++) {
var p = positions[i];
var r = radiuses[i];
var collision = false;
for (var j = i+1; j < n; j++) {
var p2 = positions[j];
var r2 = radiuses[j];
var dx = p.x - p2.x;
var dy = p.y - p2.y;
var dz = p.z - p2.z;
var d = Math.sqrt(dx*dx + dy*dy + dz*dz);
if (r + r2 >= d) { // <---------------------------- 2 fixes ---------------------------
collision = true;
count++;
break;
}
}
var index = (i / 8) | 0;
var pos = 7 - (i % 8);
if (!collision) {
res[index] &= ~(1 << pos);
} else {
res[index] |= (1 << pos);
}
}
return count;
}
- binaryen and emscripten does not yet optimize as much as modern JS-engines. So we should optimize c++ manually:
int collisionDetection(struct position *positions,
double *radiuses,
unsigned char *res, int n) {
int count = 0;
for (int i = 0; i < n; i++) {
struct position p = positions[i];
double r = radiuses[i];
bool collision = false;
for (int j = i+1; j < n; j++) {
struct position p2 = positions[j];
double r2 = radiuses[j];
double dx = p.x - p2.x;
double dy = p.y - p2.y;
double dz = p.z - p2.z;
double d = sqrt(dx*dx + dy*dy + dz*dz);
if (r + r2 >= d) {
collision = true;
count++;
break;
}
}
unsigned int index = i >> 3;
unsigned int pos = 7 - (i & 7);
unsigned int mask = 1 << pos;
unsigned char current = res[index];
res[index] = collision ? current | mask : current & ~mask;
}
return count;
}
-
You should be carried beyond init and fill memory routines from
wsCollisionDetectionand measure onlyfunctions.collisionDetection(pointer1, pointer2, pointer3, n)procedure. -
You should use loop with 32-64 iterations for warm-up instead only one call for earlier turn on JIT hot-patching.