Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

feat: speed up compile by map #713

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

Open
wwwzbwcom wants to merge 1 commit into gpujs:develop
base: develop
Choose a base branch
Loading
from wwwzbwcom:speed-up-compile-by-map

Conversation

@wwwzbwcom
Copy link

@wwwzbwcom wwwzbwcom commented Sep 3, 2021

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

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.

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

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

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

Copy link

harshkhandeparkar commented Sep 3, 2021
edited
Loading

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

Copy link

harshkhandeparkar commented Sep 3, 2021
edited
Loading

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.

Copy link

harshkhandeparkar commented Sep 3, 2021
edited
Loading

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

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

Copy link

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

Why?

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?

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

Copy link

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

wwwzbwcom reacted with thumbs up emoji

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

harshkhandeparkar reacted with thumbs up emoji

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Reviewers

No reviews

Assignees

No one assigned

Labels

None yet

Projects

None yet

Milestone

No milestone

Development

Successfully merging this pull request may close these issues.

AltStyle によって変換されたページ (->オリジナル) /