1
Fork 0
mirror of https://github.com/RGBCube/serenity synced 2025-07-27 00:17:46 +00:00

LibLine: Remove duplicate members in CompletionSuggestion

Previously, we stored two representations of the same string in
`CompletionSuggestion` object: one for the bytes and the other for the
code points corresponding to those bytes. To minimize duplication, this
patch combine both representations into a single UTF-8 string, which is
already supported by our new String class.

Following this update, we successfully reduce the size of each object
from 376 bytes to 256 bytes
This commit is contained in:
hanaa12G 2023-12-07 03:49:35 +07:00 committed by Ali Mohammad Pur
parent 89877b3f40
commit 19f137c1e6
7 changed files with 54 additions and 66 deletions

View file

@ -8,14 +8,13 @@
#include <AK/DeprecatedString.h>
#include <AK/Forward.h>
#include <AK/String.h>
#include <AK/Utf32View.h>
#include <AK/Utf8View.h>
#include <LibLine/Style.h>
namespace Line {
// FIXME: These objects are pretty heavy since they store two copies of text
// somehow get rid of one.
struct CompletionSuggestion {
private:
struct ForSearchTag {
@ -30,8 +29,8 @@ public:
{
}
CompletionSuggestion(DeprecatedString const& completion, ForSearchTag)
: text_string(completion)
CompletionSuggestion(StringView completion, ForSearchTag)
: text(MUST(String::from_utf8(completion)))
{
}
@ -44,12 +43,12 @@ public:
bool operator==(CompletionSuggestion const& suggestion) const
{
return suggestion.text_string == text_string;
return suggestion.text == text;
}
Vector<u32> text;
Vector<u32> trailing_trivia;
Vector<u32> display_trivia;
String text;
String trailing_trivia;
String display_trivia;
Style style;
size_t start_index { 0 };
size_t input_offset { 0 };
@ -57,11 +56,11 @@ public:
size_t invariant_offset { 0 };
bool allow_commit_without_listing { true };
Utf32View text_view;
Utf32View trivia_view;
Utf32View display_trivia_view;
DeprecatedString text_string;
DeprecatedString display_trivia_string;
Utf8View text_view() const { return text.code_points(); }
Utf8View trivia_view() const { return trailing_trivia.code_points(); }
Utf8View display_trivia_view() const { return display_trivia.code_points(); }
StringView text_string() const { return text.bytes_as_string_view(); }
StringView display_trivia_string() const { return display_trivia.bytes_as_string_view(); }
bool is_valid { false };
};
@ -101,7 +100,7 @@ public:
// This bit of data will be removed, but restored if the suggestion is rejected.
size_t static_offset_from_cursor { 0 };
Vector<Utf32View> insert {};
Vector<Utf8View> insert {};
Optional<Style> style_to_apply {};