-
-
Notifications
You must be signed in to change notification settings - Fork 902
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: improved bytesToUuid func #434
Conversation
Great optimization, thank you! Interestingly the string concatenation seems to be faster on V8 (Node.js/Chrome), but slower on JavaScriptCore (Safari) and SpiderMonkey (Firefox). Since this is definitely a perf improvement for Node.js, I will still merge it since performance should only ever be a real concern in serverside use cases, not in the browser. For the record: Safari 13.1Master:
Branch:
Firefox 75Master:
This Branch:
|
src/bytesToUuid.js
Outdated
@@ -14,28 +14,28 @@ function bytesToUuid(buf, offset) { | |||
const bth = byteToHex; | |||
|
|||
// join used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Oh, I just saw #267 (comment) which I must have missed while reading the other issue initially.
Seems like .toLowerCase()
is also a workaround for the memory issue? If this turns out to be true (@broofa / @sava-smith can you confirm) then we should at least update the comment here to reflect the new hack?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@ctavan I tried different ways to fix this bug. It still exists in the V8 engine. (Did not check for other engines).
The fastest option was ".toLowerCase()" on V8 engine. It also forms a normal-sized string like .join('')
.
There is still a compromise option (' ' + str).slice(1)
which works even faster, but consumes a little more memory for the result string.
I should correct myself: I'm happy to merge this if we can ensure that the issue discussed in #267 no longer exists. |
This problem still exists. But it can be solved by I am not sure that the results in the browser can be trusted. Every time I get new data. And the results are getting worse with each next run… This is probably due to the way the browser works with JS (throttling execution to save device energy, or something like that) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
267 does a good job capturing what went into the current form of this function. 'Looks like you've taken all that into account.
I agree with @ctavan that v8 (Node) perf on the server trumps browser perf. I wouldn't worry too much about browser perf as long as there aren't glaring regressions.
Please update the comment in bytesToUuid
as follows...
// Note: Be careful editing this code! It's been tuned for performance
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434
I see no reason not to take this PR. (Aside from the fact very few people will notice this change unless something breaks. 😝 )
src/bytesToUuid.js
Outdated
].join(''); | ||
// Note: Be careful editing this code! It's been tuned for performance | ||
// and works in ways you may not expect. See https://github.com/uuidjs/uuid/pull/434 | ||
// `toLowerCase` used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Referring to this PR should suffice. Readers can get from here to 267 and the Chromium issue if they want to dive that deep.
// `toLowerCase` used to fix memory issue caused by concatenation: https://bugs.chromium.org/p/v8/issues/detail?id=3175#c4 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok, as you say…
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
👏 🚀
I replaced the conversion of bytes to uuid with a more efficient way (and without inflating the size of the resulting string).
Here's what I got in terms of performance.
Benchmark master:
Benchmark this branch: