gpu.js icon indicating copy to clipboard operation
gpu.js copied to clipboard

feat: speed up compile by map

Open wwwzbwcom opened this issue 4 years ago • 13 comments

speed up compile by using map instead of array map is more than 30x faster

const mathFunctionsMap = {
  abs: true,
  acos: true,
  acosh: true,
  asin: true,
  asinh: true,
  atan: true,
  atan2: true,
  atanh: true,
  cbrt: true,
  ceil: true,
  clz32: true,
  cos: true,
  cosh: true,
  expm1: true,
  exp: true,
  floor: true,
  fround: true,
  imul: true,
  log: true,
  log2: true,
  log10: true,
  log1p: true,
  max: true,
  min: true,
  pow: true,
  random: true,
  round: true,
  sign: true,
  sin: true,
  sinh: true,
  sqrt: true,
  tan: true,
  tanh: true,
  trunc: true,
};


function isAstMathFunctionMap(ast) {
  return !!mathFunctionsMap[ast];
}

function isAstMathFunctionList(ast) {
  const mathFunctions = [
    "abs",
    "acos",
    "acosh",
    "asin",
    "asinh",
    "atan",
    "atan2",
    "atanh",
    "cbrt",
    "ceil",
    "clz32",
    "cos",
    "cosh",
    "expm1",
    "exp",
    "floor",
    "fround",
    "imul",
    "log",
    "log2",
    "log10",
    "log1p",
    "max",
    "min",
    "pow",
    "random",
    "round",
    "sign",
    "sin",
    "sinh",
    "sqrt",
    "tan",
    "tanh",
    "trunc",
  ];

  return mathFunctions.indexOf(ast) > -1;
}

console.time("isAstMathFunctionMap");
// console.log(isAstMathFunctionSet(""));
// console.log(isAstMathFunctionSet("trunc"));
for (let i = 0; i < 1e8; i++) {
    isAstMathFunctionMap("trunc")
}
console.timeEnd("isAstMathFunctionMap");


console.time("isAstMathFunctionList");
for (let i = 0; i < 1e8; i++) {
  isAstMathFunctionList("trunc");
}
console.timeEnd("isAstMathFunctionList");

/*
isAstMathFunctionMap: 75.357ms
isAstMathFunctionList: 3.747s
*/

wwwzbwcom avatar Sep 03 '21 12:09 wwwzbwcom

It's taking longer because you are defining the array in each call. If the array is a constant, the array is actually faster.

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

@harshkhandeparkar This doesnt affect much, array is still much slower:

Also, in the source code we defining array in function, so I do this in the test

const mathFunctionsMap = {
  abs: true,
  acos: true,
  acosh: true,
  asin: true,
  asinh: true,
  atan: true,
  atan2: true,
  atanh: true,
  cbrt: true,
  ceil: true,
  clz32: true,
  cos: true,
  cosh: true,
  expm1: true,
  exp: true,
  floor: true,
  fround: true,
  imul: true,
  log: true,
  log2: true,
  log10: true,
  log1p: true,
  max: true,
  min: true,
  pow: true,
  random: true,
  round: true,
  sign: true,
  sin: true,
  sinh: true,
  sqrt: true,
  tan: true,
  tanh: true,
  trunc: true,
};


function isAstMathFunctionMap(ast) {
  return !!mathFunctionsMap[ast];
}


const mathFunctions = [
    "abs",
    "acos",
    "acosh",
    "asin",
    "asinh",
    "atan",
    "atan2",
    "atanh",
    "cbrt",
    "ceil",
    "clz32",
    "cos",
    "cosh",
    "expm1",
    "exp",
    "floor",
    "fround",
    "imul",
    "log",
    "log2",
    "log10",
    "log1p",
    "max",
    "min",
    "pow",
    "random",
    "round",
    "sign",
    "sin",
    "sinh",
    "sqrt",
    "tan",
    "tanh",
    "trunc",
  ];

function isAstMathFunctionList(ast) {
  return mathFunctions.indexOf(ast) > -1;
}

console.time("isAstMathFunctionMap");
// console.log(isAstMathFunctionSet(""));
// console.log(isAstMathFunctionSet("trunc"));
for (let i = 0; i < 1e8; i++) {
    isAstMathFunctionMap("trunc")
}
console.timeEnd("isAstMathFunctionMap");


console.time("isAstMathFunctionList");
for (let i = 0; i < 1e8; i++) {
  isAstMathFunctionList("trunc");
}
console.timeEnd("isAstMathFunctionList");

/*
isAstMathFunctionMap: 66.243ms
isAstMathFunctionList: 3.532s
*/

wwwzbwcom avatar Sep 03 '21 13:09 wwwzbwcom

Checking the first element by indexOf is also slower:

const mathFunctionsMap = {
  abs: true,
  acos: true,
  acosh: true,
  asin: true,
  asinh: true,
  atan: true,
  atan2: true,
  atanh: true,
  cbrt: true,
  ceil: true,
  clz32: true,
  cos: true,
  cosh: true,
  expm1: true,
  exp: true,
  floor: true,
  fround: true,
  imul: true,
  log: true,
  log2: true,
  log10: true,
  log1p: true,
  max: true,
  min: true,
  pow: true,
  random: true,
  round: true,
  sign: true,
  sin: true,
  sinh: true,
  sqrt: true,
  tan: true,
  tanh: true,
  trunc: true,
};


function isAstMathFunctionMap(ast) {
  return !!mathFunctionsMap[ast];
}


const mathFunctions = [
    "abs",
    "acos",
    "acosh",
    "asin",
    "asinh",
    "atan",
    "atan2",
    "atanh",
    "cbrt",
    "ceil",
    "clz32",
    "cos",
    "cosh",
    "expm1",
    "exp",
    "floor",
    "fround",
    "imul",
    "log",
    "log2",
    "log10",
    "log1p",
    "max",
    "min",
    "pow",
    "random",
    "round",
    "sign",
    "sin",
    "sinh",
    "sqrt",
    "tan",
    "tanh",
    "trunc",
  ];

function isAstMathFunctionList(ast) {
  return mathFunctions.indexOf(ast) > -1;
}

console.time("isAstMathFunctionMap");
// console.log(isAstMathFunctionSet(""));
// console.log(isAstMathFunctionSet("trunc"));
for (let i = 0; i < 1e8; i++) {
    isAstMathFunctionMap("abs")
}
console.timeEnd("isAstMathFunctionMap");


console.time("isAstMathFunctionList");
for (let i = 0; i < 1e8; i++) {
  isAstMathFunctionList("abs");
}
console.timeEnd("isAstMathFunctionList");

/*
const mathFunctionsMap = {
  abs: true,
  acos: true,
  acosh: true,
  asin: true,
  asinh: true,
  atan: true,
  atan2: true,
  atanh: true,
  cbrt: true,
  ceil: true,
  clz32: true,
  cos: true,
  cosh: true,
  expm1: true,
  exp: true,
  floor: true,
  fround: true,
  imul: true,
  log: true,
  log2: true,
  log10: true,
  log1p: true,
  max: true,
  min: true,
  pow: true,
  random: true,
  round: true,
  sign: true,
  sin: true,
  sinh: true,
  sqrt: true,
  tan: true,
  tanh: true,
  trunc: true,
};


function isAstMathFunctionMap(ast) {
  return !!mathFunctionsMap[ast];
}


const mathFunctions = [
    "abs",
    "acos",
    "acosh",
    "asin",
    "asinh",
    "atan",
    "atan2",
    "atanh",
    "cbrt",
    "ceil",
    "clz32",
    "cos",
    "cosh",
    "expm1",
    "exp",
    "floor",
    "fround",
    "imul",
    "log",
    "log2",
    "log10",
    "log1p",
    "max",
    "min",
    "pow",
    "random",
    "round",
    "sign",
    "sin",
    "sinh",
    "sqrt",
    "tan",
    "tanh",
    "trunc",
  ];

function isAstMathFunctionList(ast) {
  return mathFunctions.indexOf(ast) > -1;
}

console.time("isAstMathFunctionMap");
// console.log(isAstMathFunctionSet(""));
// console.log(isAstMathFunctionSet("trunc"));
for (let i = 0; i < 1e8; i++) {
    isAstMathFunctionMap("abs")
}
console.timeEnd("isAstMathFunctionMap");


console.time("isAstMathFunctionList");
for (let i = 0; i < 1e8; i++) {
  isAstMathFunctionList("abs");
}
console.timeEnd("isAstMathFunctionList");

/*
isAstMathFunctionMap: 74.071ms
isAstMathFunctionList: 375.602ms
*/

wwwzbwcom avatar Sep 03 '21 13:09 wwwzbwcom

I did a little more testing too. I found that using array.includes is much faster than indexOf. And the includes is faster than using an object.

findArrIncludes: 512ms
findArrIndexOf: 18376ms
findObj: 5201ms
findMap: 19002ms

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

I used 10^9 operations here and also used a key in between the array (clz32) instead of at last. Here's the code: final code attached below

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

Further testing reveals how the time taken changes when the element to be searched changes:

findArrIncludes_first: 561ms
findArrIncludes_middle: 605ms
findArrIncludes_end: 631ms
findArrIndexOf_first: 8968ms
findArrIndexOf_middle: 22198ms
findArrIndexOf_end: 37412ms
findObj_first: 4891ms
findObj_middle: 24635ms
findObj_end: 25857ms
findMap_first: 11336ms
findMap_middle: 20039ms
findMap_end: 9927ms

indexOf and Object both take more time to search an element at the end than at the beginning. includes on the other hand, takes almost the same amount of time. Map.has is inconsistent for some reason.

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

@harshkhandeparkar This doesnt affect much, array is still much slower:

Somehow, my initial test gave different results. The later tests are consistent with yours. Nice find! Although, changing to includes might increase the performance even further.

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

@harshkhandeparkar This doesnt affect much, array is still much slower:

Somehow, my initial test gave different results. The later tests are consistent with yours. Nice find! Although, changing to includes might increase the performance even further.

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower, and if test them seperatly, they have similar speed, but they are always much more faster than findArrIndexOf

wwwzbwcom avatar Sep 03 '21 13:09 wwwzbwcom

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

harshkhandeparkar avatar Sep 03 '21 13:09 harshkhandeparkar

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

Maybe gc or cache related?

wwwzbwcom avatar Sep 03 '21 13:09 wwwzbwcom

If you swap the order of the findArrIncludes and findObj, findObj will be faster and findArrIncludes will be slower

Why?

Maybe gc or cache related?

Hmm, perhaps. I tested them separately and found that includes still has the benefit of constant search time.

findArrIncludes_first 470 ms
findArrIncludes_middle 463 ms
findArrIncludes_end 235 ms

and

findObj_first 472 ms
findObj_middle 17960 ms
findObj_end 19666 ms

harshkhandeparkar avatar Sep 03 '21 14:09 harshkhandeparkar

attaching the code as a file here and deleting the above snippets. speed-test.js.txt

harshkhandeparkar avatar Sep 03 '21 14:09 harshkhandeparkar

Hmm, perhaps. I tested them separately and found that includes still has the benefit of constant search time.

findArrIncludes_first 470 ms
findArrIncludes_middle 463 ms
findArrIncludes_end 235 ms

and

findObj_first 472 ms
findObj_middle 17960 ms
findObj_end 19666 ms

Can confirm this is true, thanks a lot for your help!

lets switch to includes

wwwzbwcom avatar Sep 03 '21 14:09 wwwzbwcom