libluna: Add the clear_data() method to Vector and use it to optimize Base64::decode
All checks were successful
continuous-integration/drone/push Build is passing

This method clears the Vector's data without deallocating the
backing buffer, so that it can be reused without reallocation.
This commit is contained in:
apio 2023-06-18 19:24:21 +02:00
parent d45e9e2a8c
commit 148c1c7341
Signed by: apio
GPG Key ID: B8A7D06E42258954
2 changed files with 9 additions and 1 deletions

View File

@ -201,6 +201,13 @@ template <typename T> class Vector
m_data = nullptr; m_data = nullptr;
} }
void clear_data()
{
for (usize i = 0; i < m_size; i++) { m_data[i].~T(); }
m_size = 0;
}
Result<Vector<T>> shallow_copy() Result<Vector<T>> shallow_copy()
{ {
Vector<T> other; Vector<T> other;

View File

@ -118,6 +118,7 @@ namespace Base64
} }
Vector<char> chars_read; Vector<char> chars_read;
TRY(chars_read.try_reserve(4));
for (const auto& c : data) for (const auto& c : data)
{ {
@ -134,7 +135,7 @@ namespace Base64
if (chars_read.size() == 4) if (chars_read.size() == 4)
{ {
TRY(decode_base64_buffer(chars_read, buf)); TRY(decode_base64_buffer(chars_read, buf));
chars_read.clear(); chars_read.clear_data();
} }
} }