Maker, designer, developer • he/him • Engineer @Arm

United Kingdom
Joined March 2013
circles were never the real danger
49
3,739
65
67,842
971,570
Eddie retweeted
13
362
7
4,100
231,675
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.
In the next version of Bun `Buffer.from(str, "base64")` gets 6x - 30x faster on large input, thanks to @lemire's simdutf
32
233
35
1,631
557,092
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.
345
2,970
456
11,615
2,775,215
the boy... he is here
This quoted post is unavailable.
1
292
I added this feature!! 🤗 popopop in plushie mode 👏👏
This quoted post is unavailable.
257
16
470
44
5,864
238,779
y'all best be glad my mspaint skills never made the cut for @popcatclick
174
We've been working with @youtooz to bring out this popcat.click plushie It'll be available very soon, for a limited time⏳
This quoted post is unavailable.
1
247
"The customer has nuclear weapons" gcc.gnu.org/bugzilla/show_bu…
39
421
57
4,668
646,441
surely this is the three day weekend that will replenish my will to live
6
425
20
4,879
241,901
he’s making a list,
he’s checking it twice,
he’s gonna find out who's naughty or nice,
santa claus is in contravention of article 4 of the General Data Protection Regulation (EU) 2016/679.
13
605
17
3,357
215,199
70
278
44
2,841
143,259
you know those services I refactored without filling a jira ticket?
80
539
132
5,390
951,698
✨🆕 SHOP.POPCAT.CLICK 🆕✨ 🛍️🙀 POP! POP! POP! POP! 🙀🛍️
29
14
2
35
21,297
kind of a bummer to have been born at the very end of the Fuck Around century just to live the rest of my life in the Find Out century
262
40,609
1,043
245,479
This is missing the coolest one: the actual first Photoshop icon which is a literal… photo shop
Photoshop logo evolution 1990–2023 #WebDesignHistory
23
275
12
3,422
361,966
I really think this is terribly disruptive & inconsiderate to block a road like this. Don’t the people responsible for causing this know that people have to get to work, get to daycare to pick up kids? Someone needs to be held responsible! We must not tolerate this.
241
3,261
194
15,393
1,255,545