From 148c1c7341fb3393eaa0e94e24b86a4955972af4 Mon Sep 17 00:00:00 2001 From: apio Date: Sun, 18 Jun 2023 19:24:21 +0200 Subject: [PATCH] libluna: Add the clear_data() method to Vector and use it to optimize Base64::decode This method clears the Vector's data without deallocating the backing buffer, so that it can be reused without reallocation. --- libluna/include/luna/Vector.h | 7 +++++++ libluna/src/Base64.cpp | 3 ++- 2 files changed, 9 insertions(+), 1 deletion(-) diff --git a/libluna/include/luna/Vector.h b/libluna/include/luna/Vector.h index c73d3989..65c73f19 100644 --- a/libluna/include/luna/Vector.h +++ b/libluna/include/luna/Vector.h @@ -201,6 +201,13 @@ template class Vector m_data = nullptr; } + void clear_data() + { + for (usize i = 0; i < m_size; i++) { m_data[i].~T(); } + + m_size = 0; + } + Result> shallow_copy() { Vector other; diff --git a/libluna/src/Base64.cpp b/libluna/src/Base64.cpp index a40add49..25cdaf8e 100644 --- a/libluna/src/Base64.cpp +++ b/libluna/src/Base64.cpp @@ -118,6 +118,7 @@ namespace Base64 } Vector chars_read; + TRY(chars_read.try_reserve(4)); for (const auto& c : data) { @@ -134,7 +135,7 @@ namespace Base64 if (chars_read.size() == 4) { TRY(decode_base64_buffer(chars_read, buf)); - chars_read.clear(); + chars_read.clear_data(); } }