-
Notifications
You must be signed in to change notification settings - Fork 192
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
[perf] replace Math.max usage with reduce and pure compare #1520
[perf] replace Math.max usage with reduce and pure compare #1520
Conversation
451b301
to
e099bd9
Compare
Here is a comparison results with #1515 (comment) ![]() Last image - math.max + all changes from linked PR. I can't see dramatic improvements in performance, but I see difference in results accuracy here: ![]() |
thanks for running the benchmark and providing results / reasoning / research / etc! it helps a lot with understanding the why behind this change, rather than just the outcome |
@lifeart @NullVoxPopuli please note that the benchmark https://jsben.ch/Qosu7 has some problems.
for (const tag of values) {
result = Math.max(values[tag], result);
// ^ maybe Math.max(tag, result)
} This may have some penalty in speed (actually negligible in my measurements). Anyways, you should pay more attention to the benchmarked code.
Here is the improved benchmark: https://jsben.ch/eNWlG and its results: |
seems like it's worth a PR, if you have the time! 🎉 nice work! |
I'd say that the classical |
tsconfig has for (let i = 0; i < subtag.length; i++) {
const value = subtag[i]![COMPUTE]();
revision = Math.max(value, revision);
} or for (let i = 0; i < subtag.length; i++) {
const tag = subtag[i];
if (tag !== undefined) {
const value = tag[COMPUTE]();
revision = Math.max(value, revision);
}
} Which one do you prefer? Fortunately, both variants show close results in benchmark https://jsben.ch/we4qL on current versions of Chrome, Firefox, and Safari. |
It seems reduce is 5 times faster for our case https://jsben.ch/Qosu7
![image](https://private-user-images.githubusercontent.com/1360552/288228261-3ab23127-f097-4d4c-ae41-e4756f50a36d.png?jwt=eyJhbGciOiJIUzI1NiIsInR5cCI6IkpXVCJ9.eyJpc3MiOiJnaXRodWIuY29tIiwiYXVkIjoicmF3LmdpdGh1YnVzZXJjb250ZW50LmNvbSIsImtleSI6ImtleTUiLCJleHAiOjE3Mzk1MDY5NzYsIm5iZiI6MTczOTUwNjY3NiwicGF0aCI6Ii8xMzYwNTUyLzI4ODIyODI2MS0zYWIyMzEyNy1mMDk3LTRkNGMtYWU0MS1lNDc1NmY1MGEzNmQucG5nP1gtQW16LUFsZ29yaXRobT1BV1M0LUhNQUMtU0hBMjU2JlgtQW16LUNyZWRlbnRpYWw9QUtJQVZDT0RZTFNBNTNQUUs0WkElMkYyMDI1MDIxNCUyRnVzLWVhc3QtMSUyRnMzJTJGYXdzNF9yZXF1ZXN0JlgtQW16LURhdGU9MjAyNTAyMTRUMDQxNzU2WiZYLUFtei1FeHBpcmVzPTMwMCZYLUFtei1TaWduYXR1cmU9MWQwMTU5M2U4YzZiMmQ0MTA2ZDZmZmYzZjdjODBlODI4MjMzNWFmOWQ4ZmM2ZTIzYTRjNzFkNDg4NzRjOWUzNiZYLUFtei1TaWduZWRIZWFkZXJzPWhvc3QifQ.khIpUskKPKc75SFUyLM1w7z8i4vNPfoeucNnGwTqohw)
Here is V8
Math.Max
implementation: https://github.com/v8/v8/blob/cd81dd6d740ff82a1abbc68615e8769bd467f91e/src/js/math.js#L77-L102 (old)https://github.com/v8/v8/blob/04f51bc70a38fbea743588e41290bea40830a486/src/builtins/math.tq#L135 (new)
It seems it uses more code paths than we actually need:
In our case both args already numbers and we could take 1 step instead of 5
If we assume every step is an opcode and there is no caching, for case where we have 10 tags + 10 subtags, new comparison should took 100 opcodes, and old (with
Math.max
) - 500. This numbers quite relative to numbers we see in micro-bench.Likely need to check macro-bench to verify