-
Notifications
You must be signed in to change notification settings - Fork 2.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Upgrade SIMDUTF, 8x faster atob (#11085)
- Loading branch information
1 parent
5389c7a
commit b5dff55
Showing
8 changed files
with
6,799 additions
and
1,074 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
import { bench, run } from "./runner.mjs"; | ||
|
||
function makeBenchmark(size) { | ||
const latin1 = btoa("A".repeat(size)); | ||
|
||
bench(`atob(${size} chars)`, () => { | ||
atob(latin1); | ||
}); | ||
} | ||
|
||
[32, 512, 64 * 1024, 512 * 1024, 1024 * 1024 * 8].forEach(makeBenchmark); | ||
|
||
await run(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
|
||
#include "root.h" | ||
#include "simdutf.h" | ||
|
||
#include "ExceptionOr.h" | ||
|
||
namespace Bun { | ||
|
||
namespace Base64 { | ||
|
||
using namespace WebCore; | ||
|
||
ExceptionOr<String> atob(const String& encodedString) | ||
{ | ||
if (encodedString.isEmpty()) | ||
return String(); | ||
|
||
if (!encodedString.is8Bit()) { | ||
const auto span = encodedString.span16(); | ||
size_t expected_length = simdutf::latin1_length_from_utf16(span.size()); | ||
LChar* ptr; | ||
WTF::String convertedString = WTF::String::createUninitialized(expected_length, ptr); | ||
if (UNLIKELY(convertedString.isNull())) { | ||
return WebCore::Exception { OutOfMemoryError }; | ||
} | ||
|
||
auto result = simdutf::convert_utf16le_to_latin1_with_errors(span.data(), span.size(), reinterpret_cast<char*>(ptr)); | ||
|
||
if (result.error) { | ||
return WebCore::Exception { InvalidCharacterError }; | ||
} | ||
return atob(convertedString); | ||
} | ||
|
||
const auto span = encodedString.span8(); | ||
size_t result_length = simdutf::maximal_binary_length_from_base64(reinterpret_cast<const char*>(span.data()), encodedString.length()); | ||
LChar* ptr; | ||
WTF::String outString = WTF::String::createUninitialized(result_length, ptr); | ||
if (UNLIKELY(outString.isNull())) { | ||
return WebCore::Exception { OutOfMemoryError }; | ||
} | ||
auto result = simdutf::base64_to_binary(reinterpret_cast<const char*>(span.data()), span.size(), reinterpret_cast<char*>(ptr), simdutf::base64_default); | ||
if (result.error != simdutf::error_code::SUCCESS) { | ||
return WebCore::Exception { InvalidCharacterError }; | ||
} | ||
if (result.count != result_length) { | ||
return outString.substringSharingImpl(0, result.count); | ||
} | ||
|
||
return outString; | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,10 @@ | ||
#include "root.h" | ||
|
||
namespace Bun { | ||
|
||
namespace Base64 { | ||
|
||
WebCore::ExceptionOr<WTF::String> atob(const WTF::String& encodedString); | ||
|
||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.