mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2025-12-07 21:59:54 +00:00
LibJS: Avoid redundant ByteBuffer in Uint8Array.fromBase64()
Since we already have a ByteBuffer from decoding the Base64 data, we can pass that when creating a new ArrayBuffer. This avoids a buffer allocation + memory clear + memory copy.
This commit is contained in:
parent
c9c98a150d
commit
b2761b5640
Notes:
github-actions[bot]
2025-11-29 14:41:13 +00:00
Author: https://github.com/awesomekling
Commit: b2761b5640
Pull-request: https://github.com/LadybirdBrowser/ladybird/pull/6975
Reviewed-by: https://github.com/trflynn89
1 changed files with 4 additions and 8 deletions
|
|
@ -115,18 +115,14 @@ JS_DEFINE_NATIVE_FUNCTION(Uint8ArrayConstructorHelpers::from_base64)
|
|||
auto result_length = result.bytes.size();
|
||||
|
||||
// 12. Let ta be ? AllocateTypedArray("Uint8Array", %Uint8Array%, "%Uint8Array.prototype%", resultLength).
|
||||
auto typed_array = TRY(Uint8Array::create(realm, result_length));
|
||||
// 14. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
|
||||
// index of result.[[Bytes]].
|
||||
auto array_buffer = ArrayBuffer::create(realm, move(result.bytes));
|
||||
auto typed_array = Uint8Array::create(realm, result_length, array_buffer);
|
||||
|
||||
// 13. Assert: ta.[[ViewedArrayBuffer]].[[ArrayBufferByteLength]] is the number of elements in result.[[Bytes]].
|
||||
VERIFY(typed_array->viewed_array_buffer()->byte_length() == result_length);
|
||||
|
||||
// 14. Set the value at each index of ta.[[ViewedArrayBuffer]].[[ArrayBufferData]] to the value at the corresponding
|
||||
// index of result.[[Bytes]].
|
||||
auto& array_buffer_data = typed_array->viewed_array_buffer()->buffer();
|
||||
|
||||
for (size_t index = 0; index < result_length; ++index)
|
||||
array_buffer_data[index] = result.bytes[index];
|
||||
|
||||
// 15. Return ta.
|
||||
return typed_array;
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue