-
-
Notifications
You must be signed in to change notification settings - Fork 6
src: use arithmetic HexEncode instead of lookups #12
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
base: main
Are you sure you want to change the base?
Conversation
|
Excellent. For some analysis, please see this article on my blog: Daniel Lemire, "Converting data to hexadecimal outputs quickly," in Daniel Lemire's blog, February 2, 2026, https://lemire.me/blog/2026/02/02/converting-data-to-hexadecimal-outputs-quickly/. |
|
This is not the fastest way yet though, there should be more optimizations possible, but now I estimate that the most significant part of the slowdown is on the wrapping code. @lemire pls s/Skovorora/Skovoroda/g 🙃 |
|
@ChALkeR I'll update your name. Sorry. What you have here is great and I don't recommend doing more. The better place to get fancier would be simdutf. We do invite contributions. |
| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| -1, -1, -1, -1, -1, -1, -1, -1, -1}; | ||
|
|
||
| inline char nibble(uint8_t x) { return x + '0' + ((x > 9) * 39); } |
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.
| inline char nibble(uint8_t x) { return x + '0' + ((x > 9) * 39); } | |
| inline constexpr char nibble(uint8_t x) { | |
| return x + '0' + ((x > 9) * 39); | |
| } |
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.
@ChALkeR I think that one might get results just as good with something simpler
char nibble(uint8_t x) {
auto v = x + '0';
if(x>9) {
v += 'a'-'0'- 10;
}
return v;
}| -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, | ||
| -1, -1, -1, -1, -1, -1, -1, -1, -1}; | ||
|
|
||
| inline char nibble(uint8_t x) { return x + '0' + ((x > 9) * 39); } |
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.
Can you add a comment to this function and explain what it does?
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 that if you express it as I did above, it does not require an explanation anymore.
See nodejs/node#61609
This improves
Buffer#toString('hex')2-4xIn local tests on a MacBook, this improves
.toString('hex')throughput from ~2.24 GiB/sto ~9.53 GiB/son ~80 KiB chunksFor comparison,
Uint8Array#toHex()shows3.28 GiB/s.Benchmark CI: https://ci.nodejs.org/view/Node.js%20benchmark/job/benchmark-node-micro-benchmarks/1790/console