Angehefteter Tweet
Eddie
837 posts

Eddie
@onfe1
Maker, designer, developer • he/him • Engineer @Arm
United Kingdom Beigetreten Mart 2013
160 Folgt2.2K Follower
Eddie retweetet

Eddie retweetet
Eddie retweetet

The fast JavaScript runtime Bun is much faster than Node.js 22 at decoding Base64 inputs. By much faster, I mean *several times* faster. But they both rely on the same underlying library (simdutf) for the actual decoding.
So what gives?
The problem is that Node.js needs to interact with v8, the underlying JavaScript engine (from Google)... and doing so is not trivial.
Before we can start decoding the string, we need to grab the string... so, in this instance, we call String::Value...
In turns, this allocates an array inside Node.js and asks v8 to copy the content to it...
In an ideal world, we would avoid the trouble entirely and just ask v8 to give us direct access to how it stores the string... and we try to do that if we can... but let me come back to it...
How bad can this be, right? Just a copy.
Well. Let us do some profiling...
So you see, the base64 decoding itself is about about 1/5 of the running time, but the copy takes half of it.
What is up with this CopyChars function? Well, it is mostly just a wrapper around the standard high level C++ function std::copy_n as far as I can tell. (see v8/src/utils/memcopy.h)
But we are copying for an 8-bit input to a 16-bit output... why is that? Base64 is pure ASCII... and v8 can store ASCII using 8-bit per character.
We get there before both IsExternalOneByte() and IsOneByte() are false (see node/src/node_buffer.cc)... We have fast paths for these cases. If IsExternalOneByte() is true, we just get the bytes and everything is great. Unfortunately, it does not always work.
So we have a v8 string that is really pure ASCII, but, seemingly, we can't tell that it is the case from Node.js, and so we have to convert it to UTF-16 needlessly, using a function that is maybe not very well optimized... and then we do the base64 decoding of an ASCII string from the UTF-16 input. It is not great.
To be fair, this is just one string, created as 'Buffer.alloc(size, "latin1").toString("base64")', basically the base64 encoded version of the string "latin1latin1latin1...". In actual applications, we might have better luck.
Yet. Yet. I am telling this complicated story for a reason.
The story illustrates why our software is slower than it should be. We have layers of abstractions to fight against. Sometimes you win, sometimes you lose.
These layers are there for a reason, but they are not free.
To make matters worse... these abstraction layers often thicken over time... and the friction goes up.
To be clear, I do not claim that the Node.js code is optimal. In fact, I know it can be better. But it is not trivial to make it go fast.
I sometimes hear people say... "well, it is C++ and C++ is hard". No. The C++ part is easy relatively speaking. The difficulty is at a higher level. It is not a matter of syntax. It is a matter of architecture.



Jarred Sumner@jarredsumner
In the next version of Bun `Buffer.from(str, "base64")` gets 6x - 30x faster on large input, thanks to @lemire's simdutf
English
Eddie retweetet

1. I'm legit shocked by the design of @Meta's new notification informing us they want to use the content we post to train their AI models. It's intentionally designed to be highly awkward in order to minimise the number of users who will object to it. Let me break it down.

English

it's the last day of the pre-order
if you want a real popcat, now's your chance
🔗 youtooz.com/products/popca…
English
Eddie retweetet

@geromegamzing @youtooz I think it looks quite good IRL, but thanks for the feedback - what would you change?
English

We've been working with @youtooz to bring out this popcat.click plushie
It'll be available very soon, for a limited time⏳
English
Eddie retweetet
Eddie retweetet

"The customer has nuclear weapons"
#c4" target="_blank" rel="nofollow noopener">gcc.gnu.org/bugzilla/show_…

English
Eddie retweetet
Eddie retweetet
Eddie retweetet
Eddie retweetet
Eddie retweetet
Eddie retweetet
















