-
Notifications
You must be signed in to change notification settings - Fork 819
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
Faster trace id generation #824
Faster trace id generation #824
Conversation
70b2ac3
to
f7b2e93
Compare
Codecov Report
@@ Coverage Diff @@
## master #824 +/- ##
==========================================
+ Coverage 94.11% 94.13% +0.01%
==========================================
Files 247 240 -7
Lines 10762 10142 -620
Branches 1049 963 -86
==========================================
- Hits 10129 9547 -582
+ Misses 633 595 -38
|
It would be nice to have some benchmarks (numbers) |
UUID only generate 16 byte random values, introduces a dependency, and returns strings containing dashes which need to be stripped, but I have included it here for completeness.
|
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 think the function for browser is still slow and this might be improved
I have did some research and I have compared this solution with 3 others
here is jsperf
https://jsperf.com/convert-numeric-array-to-hex-string/11
the function toHex2
seems to be the fastest (for our purposes) and it is really visible difference ( more then 7 times faster)
function toHex2 (byteArray) {
const chars = new Uint8Array(byteArray.length * 2);
const alpha = 'a'.charCodeAt(0) - 10;
const digit = '0'.charCodeAt(0);
let p = 0;
for (let i = 0; i < byteArray.length; i++) {
let nibble = (byteArray[i] >>> 4) & 0xF;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
nibble = byteArray[i] & 0xF;
chars[p++] = nibble > 9 ? nibble + alpha : nibble + digit;
}
return String.fromCharCode.apply(null, chars);
}
the mentioned jsperf is modification and the origin comes from this discussion
https://stackoverflow.com/questions/34309988/byte-array-to-hex-string-conversion-in-javascript
Wow @obecny that is a big difference. edit: just realized i'm being stupid. We're generating the characters of course we can make them lowercase only. i'll apply this today |
…trace-oss-contrib/opentelemetry-js into trace-id-fast
@obecny applied your change |
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.
lgtm, if you can please add just simple unit test for function toHex.
That would require us to export the toHex function, but I don't think we should as it is an internal detail. As long as this test passes, we know ids are being generated correctly: https://github.com/open-telemetry/opentelemetry-js/blob/cf93906a9b736bd15d1f094ec803eae67c17bdde/packages/opentelemetry-core/test/platform/id.test.ts I expanded it in f539c99 in order to make sure the ids we generate are robust, which you can see here: https://github.com/open-telemetry/opentelemetry-js/blob/f539c9939d19d67a494fe62a81872c9572179838/packages/opentelemetry-core/test/platform/id.test.ts |
Replaces #786
While working on #786 we found that most of the performance benefit came from a more efficient method of generating trace ids. This PR implements the more efficient method while allowing us to continue using the simpler string implementation for trace and span ids.
/cc @Flarna