diff options
author | 2023-08-10 13:32:04 -0700 | |
---|---|---|
committer | 2023-08-10 13:32:04 -0700 | |
commit | e25833d00946fdd400abd3c25e8aa1c3e3d2e355 (patch) | |
tree | 71f6f046b75f6dbafd90e96443b1a3e8a29ad07d /src/bun.js/bindings/wtf-bindings.cpp | |
parent | e65535cc054f8bfff98648f0605537a9d734e225 (diff) | |
download | bun-e25833d00946fdd400abd3c25e8aa1c3e3d2e355.tar.gz bun-e25833d00946fdd400abd3c25e8aa1c3e3d2e355.tar.zst bun-e25833d00946fdd400abd3c25e8aa1c3e3d2e355.zip |
Fixes #4062 (#4106)
* Fixes #4062
* Update encoding.zig
* Use faster C++ impl
* Update wtf-bindings.cpp
* undo
* Fixup
---------
Co-authored-by: Jarred Sumner <709451+Jarred-Sumner@users.noreply.github.com>
Diffstat (limited to 'src/bun.js/bindings/wtf-bindings.cpp')
-rw-r--r-- | src/bun.js/bindings/wtf-bindings.cpp | 53 |
1 files changed, 45 insertions, 8 deletions
diff --git a/src/bun.js/bindings/wtf-bindings.cpp b/src/bun.js/bindings/wtf-bindings.cpp index 5c0e593d7..ccf71f8eb 100644 --- a/src/bun.js/bindings/wtf-bindings.cpp +++ b/src/bun.js/bindings/wtf-bindings.cpp @@ -1,5 +1,4 @@ #include "wtf-bindings.h" -#include "wtf/text/Base64.h" #include "wtf/StackTrace.h" #include "wtf/dtoa.h" @@ -14,13 +13,6 @@ extern "C" void WTF__copyLCharsFromUCharSource(LChar* destination, const UChar* WTF::StringImpl::copyCharacters(destination, source, length); } -extern "C" JSC::EncodedJSValue WTF__toBase64URLStringValue(const uint8_t* bytes, size_t length, JSC::JSGlobalObject* globalObject) -{ - WTF::String string = WTF::base64URLEncodeToString(reinterpret_cast<const LChar*>(bytes), static_cast<unsigned int>(length)); - string.impl()->ref(); - return JSC::JSValue::encode(JSC::jsString(globalObject->vm(), string)); -} - extern "C" void Bun__crashReportWrite(void* ctx, const char* message, size_t length); extern "C" void Bun__crashReportDumpStackTrace(void* ctx) { @@ -51,4 +43,49 @@ extern "C" void Bun__crashReportDumpStackTrace(void* ctx) auto str = out.toCString(); Bun__crashReportWrite(ctx, str.data(), str.length()); } +} + +// For whatever reason +// Doing this in C++/C is 2x faster than doing it in Zig. +// However, it's still slower than it should be. +static constexpr size_t encodeMapSize = 64; +static constexpr char base64URLEncMap[encodeMapSize] = { + 0x41, 0x42, 0x43, 0x44, 0x45, 0x46, 0x47, 0x48, 0x49, 0x4A, 0x4B, + 0x4C, 0x4D, 0x4E, 0x4F, 0x50, 0x51, 0x52, 0x53, 0x54, 0x55, 0x56, + 0x57, 0x58, 0x59, 0x5A, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x67, + 0x68, 0x69, 0x6A, 0x6B, 0x6C, 0x6D, 0x6E, 0x6F, 0x70, 0x71, 0x72, + 0x73, 0x74, 0x75, 0x76, 0x77, 0x78, 0x79, 0x7A, 0x30, 0x31, 0x32, + 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x2D, 0x5F +}; + +extern "C" size_t WTF__base64URLEncode(const unsigned char* __restrict inputDataBuffer, size_t inputDataBufferSize, + unsigned char* __restrict destinationDataBuffer, + size_t destinationDataBufferSize) +{ + size_t sidx = 0; + size_t didx = 0; + + if (inputDataBufferSize > 1) { + while (sidx < inputDataBufferSize - 2) { + destinationDataBuffer[didx++] = base64URLEncMap[(inputDataBuffer[sidx] >> 2) & 077]; + destinationDataBuffer[didx++] = base64URLEncMap[((inputDataBuffer[sidx + 1] >> 4) & 017) | ((inputDataBuffer[sidx] << 4) & 077)]; + destinationDataBuffer[didx++] = base64URLEncMap[((inputDataBuffer[sidx + 2] >> 6) & 003) | ((inputDataBuffer[sidx + 1] << 2) & 077)]; + destinationDataBuffer[didx++] = base64URLEncMap[inputDataBuffer[sidx + 2] & 077]; + sidx += 3; + } + } + + if (sidx < inputDataBufferSize) { + destinationDataBuffer[didx++] = base64URLEncMap[(inputDataBuffer[sidx] >> 2) & 077]; + if (sidx < inputDataBufferSize - 1) { + destinationDataBuffer[didx++] = base64URLEncMap[((inputDataBuffer[sidx + 1] >> 4) & 017) | ((inputDataBuffer[sidx] << 4) & 077)]; + destinationDataBuffer[didx++] = base64URLEncMap[(inputDataBuffer[sidx + 1] << 2) & 077]; + } else + destinationDataBuffer[didx++] = base64URLEncMap[(inputDataBuffer[sidx] << 4) & 077]; + } + + while (didx < destinationDataBufferSize) + destinationDataBuffer[didx++] = '='; + + return destinationDataBufferSize; }
\ No newline at end of file |