-
-
Notifications
You must be signed in to change notification settings - Fork 1.1k
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(core): concat strings before computing hash #2773
Conversation
Just wondering, but could berry/packages/yarnpkg-core/sources/hashUtils.ts Lines 5 to 12 in 9ff0615
Seems like it would go at the root of the problem. |
I tested that before (and again now) but it's actually slower |
That's weird, seems it would be equivalent to the changes in the PR. What could be the difference? How did the implementation look? |
Well the changes in this PR gets rid of two iterations (the spread and the loop in makeHash), applying the attached diff on top of this PR makes the time spent in diff --git a/packages/yarnpkg-core/sources/hashUtils.ts b/packages/yarnpkg-core/sources/hashUtils.ts
index 247d826fc..989bdc39d 100644
--- a/packages/yarnpkg-core/sources/hashUtils.ts
+++ b/packages/yarnpkg-core/sources/hashUtils.ts
@@ -5,8 +5,18 @@ import globby from 'globby';
export function makeHash<T extends string = string>(...args: Array<BinaryLike | null>): T {
const hash = createHash(`sha512`);
- for (const arg of args)
- hash.update(arg ? arg : ``);
+ const acc: Array<Buffer> = [];
+ let totalLength = 0;
+ for (const arg of args) {
+ if (arg) {
+ // @ts-expect-error
+ const buffer = Buffer.from(arg);
+ totalLength += buffer.length;
+ acc.push(buffer);
+ }
+ }
+
+ hash.update(Buffer.concat(acc, totalLength));
return hash.digest(`hex`) as T;
}
|
I think that's possibly due to the
|
I'm not a fan of "leaking" this optimization in the caller (because then it would suggest we should do that everywhere export function makeHash<T extends string = string>(...args: Array<BinaryLike | null>): T {
const hash = createHash(`sha512`);
for (let t = 0, T = args.length; t < T; ++t) {
let acc = args[t];
while (typeof args[t + 1] === `string`)
acc += args[++t];
hash.update(acc);
}
return hash.digest(`hex`) as T;
} |
ea1a3ee
to
248d353
Compare
Moved the logic to |
What's the problem this PR addresses?
hashUtils.makeHash
callsHash.update
for every argumentHow did you fix it?
Concat all string arguments in a row before calling update
Results
Testing on the repro provided in #973 (comment)
Checklist