Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: speed up compile by map #713

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from

Conversation

wwwzbwcom
Copy link

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
*/

@harshkhandeparkar
Copy link

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

@wwwzbwcom
Copy link
Author

@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
Copy link
Author

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
*/

@harshkhandeparkar
Copy link

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
Copy link

harshkhandeparkar commented Sep 3, 2021

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
Copy link

harshkhandeparkar commented Sep 3, 2021

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
Copy link

harshkhandeparkar commented Sep 3, 2021

@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.

@wwwzbwcom
Copy link
Author

@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

@harshkhandeparkar
Copy link

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

Why?

@wwwzbwcom
Copy link
Author

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?

@harshkhandeparkar
Copy link

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
Copy link

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

@wwwzbwcom
Copy link
Author

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 wwwzbwcom force-pushed the speed-up-compile-by-map branch from 984dd10 to 8bc05a5 Compare September 3, 2021 14:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants