-
Notifications
You must be signed in to change notification settings - Fork 896
feat(api): improve isValidSpanId, isValidTraceId performance #5714
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
Changes from 6 commits
1ecdb32
a473a43
5caa81b
b222bfb
7754f6e
373d930
62c3be0
6a24229
0c13141
3351e18
d9e9365
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -18,21 +18,45 @@ import { NonRecordingSpan } from './NonRecordingSpan'; | |
import { Span } from './span'; | ||
import { SpanContext } from './span_context'; | ||
|
||
const VALID_TRACEID_REGEX = /^([0-9a-f]{32})$/i; | ||
const VALID_SPANID_REGEX = /^[0-9a-f]{16}$/i; | ||
// Valid characters (0-9, a-f, A-F) are marked as 1. | ||
const isHex = [ | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, | ||
1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, 0, 0, 0, 0, 0, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 1, 1, 1, 1, 1, 1, 0, | ||
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, | ||
] as const; | ||
pichlermarc marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
||
function isValidHex(id: string, length: number): boolean { | ||
// As of 1.9.0 the id was allowed to be a non-string value, | ||
// even though it was not possible in the types. | ||
if (typeof id !== 'string' || id.length !== length) return false; | ||
|
||
let r = 0; | ||
for (let i = 0; i < id.length; i += 4) { | ||
// Mask by 0x7f (127) to avoid LUT overflow. | ||
r += | ||
isHex[id.charCodeAt(i) & 0x7f] + | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. maybe also a comment about masking so that this only lowest 7 bits are retained. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Added an explanation for the bitwise and There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It is possible using There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This comment got me thinking that this implementation had an error, where an UTF-16 value like I changed the implementation to As for I've profiled different implementations now, here's the data: The implementation in the PR is You can try it out yourself: https://gist.github.com/seemk/f00c432d8779a03dbe1a28236f9dd198 |
||
isHex[id.charCodeAt(i + 1) & 0x7f] + | ||
isHex[id.charCodeAt(i + 2) & 0x7f] + | ||
isHex[id.charCodeAt(i + 3) & 0x7f]; | ||
} | ||
|
||
return r === length; | ||
} | ||
|
||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export function isValidTraceId(traceId: string): boolean { | ||
return VALID_TRACEID_REGEX.test(traceId) && traceId !== INVALID_TRACEID; | ||
return isValidHex(traceId, 32) && traceId !== INVALID_TRACEID; | ||
} | ||
|
||
/** | ||
* @since 1.0.0 | ||
*/ | ||
export function isValidSpanId(spanId: string): boolean { | ||
return VALID_SPANID_REGEX.test(spanId) && spanId !== INVALID_SPANID; | ||
return isValidHex(spanId, 16) && spanId !== INVALID_SPANID; | ||
} | ||
|
||
/** | ||
|
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.
FWIW, Node core implements this with an
Int8Array
, which might be more efficient.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.
Seems to be about the same, with the Int8Array based version being a few percent slower (no idea why, could be noise):
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 benchmarked this with jsbench.me and changing this to be a
Uint8Array
sped it up by about 12% for meThere 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.
It seems to be slightly faster on x64, see the results here: #5714 (comment) (there's a link to the benchmark script if you want to run it yourself).
I changed the implementation to use
Uint8Array
.