joshforisha / fast-simplex-noise-js

Fast simplex noise implemented in TypeScript
The Unlicense
66 stars 7 forks source link

Performance improvements #14

Open mattdesl opened 1 year ago

mattdesl commented 1 year ago

I'm working on some optimizations and notice it could be sped up in V8. This is tested with node v19.9.0 and latest Chrome on a MacBook Air M2.

I specifically tested the 2D version but assume the same will hold across all. I've attached the code at the end of this post.

Optimizations:

With 10M iterations: ~1.4s before, ~0.4s after.

Note the exponentiation change introduces a very small loss of precision. For example the noise value might be -0.9466193945512852 instead of -0.9466193945512853 (the precision loss always seems to be below 0.000000000000001).

Benchmark using this PRNG for constancy.

import { PRNG } from "./prng.js";
console.time("noise");
const rnd = PRNG("1234").random;
const noise = makeNoise2D(rnd);
let sum = 0;
const count = 1e7;
for (let i = 0; i < count; i++) {
  sum += noise(rnd(), rnd());
}
console.timeEnd("noise");
console.log(sum / count);

Code changes:

const G2 = (3.0 - Math.sqrt(3.0)) / 6.0;
const F2 = 0.5 * (Math.sqrt(3.0) - 1.0);
const Grad = new Float32Array([
  1, 1, -1, 1, 1, -1, -1, -1, 1, 0, -1, 0, 1, 0, -1, 0, 0, 1, 0, -1, 0, 1, 0,
  -1,
]);

export function makeNoise2D(random = Math.random) {
  const p = new Uint8Array(256);
  for (let i = 0; i < 256; i++) p[i] = i;

  let n;
  let q;
  for (let i = 255; i > 0; i--) {
    n = Math.floor((i + 1) * random());
    q = p[i];
    p[i] = p[n];
    p[n] = q;
  }

  const perm = new Uint8Array(512);
  const permMod12 = new Uint8Array(512);
  for (let i = 0; i < 512; i++) {
    perm[i] = p[i & 255];
    permMod12[i] = perm[i] % 12;
  }

  return (x, y) => {
    // Skew the input space to determine which simplex cell we're in
    const s = (x + y) * F2; // Hairy factor for 2D
    const i = Math.floor(x + s);
    const j = Math.floor(y + s);
    const t = (i + j) * G2;
    const X0 = i - t; // Unskew the cell origin back to (x,y) space
    const Y0 = j - t;
    const x0 = x - X0; // The x,y distances from the cell origin
    const y0 = y - Y0;

    // Determine which simplex we are in.
    const i1 = x0 > y0 ? 1 : 0;
    const j1 = x0 > y0 ? 0 : 1;

    // Offsets for corners
    const x1 = x0 - i1 + G2;
    const y1 = y0 - j1 + G2;
    const x2 = x0 - 1.0 + 2.0 * G2;
    const y2 = y0 - 1.0 + 2.0 * G2;

    // Work out the hashed gradient indices of the three simplex corners
    const ii = i & 255;
    const jj = j & 255;
    const idx0 = permMod12[ii + perm[jj]] * 2;
    const idx1 = permMod12[ii + i1 + perm[jj + j1]] * 2;
    const idx2 = permMod12[ii + 1 + perm[jj + 1]] * 2;

    // Calculate the contribution from the three corners
    const t0 = 0.5 - x0 * x0 - y0 * y0;
    const n0 =
      t0 < 0
        ? 0.0
        : t0 * t0 * t0 * t0 * (Grad[idx0] * x0 + Grad[idx0 + 1] * y0);

    const t1 = 0.5 - x1 * x1 - y1 * y1;
    const n1 =
      t1 < 0
        ? 0.0
        : t1 * t1 * t1 * t1 * (Grad[idx1] * x1 + Grad[idx1 + 1] * y1);

    const t2 = 0.5 - x2 * x2 - y2 * y2;
    const n2 =
      t2 < 0
        ? 0.0
        : t2 * t2 * t2 * t2 * (Grad[idx2] * x2 + Grad[idx2 + 1] * y2);

    // Add contributions from each corner to get the final noise value.
    // The result is scaled to return values in the interval [-1, 1]
    return 70.14805770653952 * (n0 + n1 + n2);
  };
}
MaxGraey commented 1 year ago

You can improve his even more. Instead t * t * t * t * (Grad...) just use (t *= t, t *= t) * (Grad...)

Also instead:

    // 10 ops
    const x1 = x0 - i1 + G2;
    const y1 = y0 - j1 + G2;
    const x2 = x0 - 1.0 + 2.0 * G2;
    const y2 = y0 - 1.0 + 2.0 * G2;

you can use:

    // 8 ops
    const xG = x0 + G2;
    const yG = y0 + G2;
    const x1 = xG - i1;
    const y1 = yG - j1;
    const x2 = xG + G2 - 1.0;
    const y2 = yG + G2 - 1.0;

I don't think all these improvements will give a strong performance boost, but it's worth checking out nonetheless