mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 16:47:36 +00:00
AK: Remove String-from-StringView optimization
We had an unusual optimization in AK::StringView where constructing a StringView from a String would cause it to remember the internal StringImpl pointer of the String. This was used to make constructing a String from a StringView fast and copy-free. I tried removing this optimization and indeed we started seeing a ton of allocation traffic. However, all of it was due to a silly pattern where functions would take a StringView and then go on to create a String from it. I've gone through most of the code and updated those functions to simply take a String directly instead, which now makes this optimization unnecessary, and indeed a source of bloat instead. So, let's get rid of it and make StringView a little smaller. :^)
This commit is contained in:
parent
94b247c5a9
commit
873da38d0e
4 changed files with 5 additions and 17 deletions
|
@ -38,9 +38,6 @@ namespace AK {
|
||||||
|
|
||||||
String::String(const StringView& view)
|
String::String(const StringView& view)
|
||||||
{
|
{
|
||||||
if (view.m_impl)
|
|
||||||
m_impl = *view.m_impl;
|
|
||||||
else
|
|
||||||
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
|
m_impl = StringImpl::create(view.characters_without_null_termination(), view.length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -219,8 +219,6 @@ static inline char to_lowercase(char c)
|
||||||
|
|
||||||
bool equals_ignoring_case(const StringView& a, const StringView& b)
|
bool equals_ignoring_case(const StringView& a, const StringView& b)
|
||||||
{
|
{
|
||||||
if (a.impl() && a.impl() == b.impl())
|
|
||||||
return true;
|
|
||||||
if (a.length() != b.length())
|
if (a.length() != b.length())
|
||||||
return false;
|
return false;
|
||||||
for (size_t i = 0; i < a.length(); ++i) {
|
for (size_t i = 0; i < a.length(); ++i) {
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -35,15 +35,13 @@
|
||||||
namespace AK {
|
namespace AK {
|
||||||
|
|
||||||
StringView::StringView(const String& string)
|
StringView::StringView(const String& string)
|
||||||
: m_impl(string.impl())
|
: m_characters(string.characters())
|
||||||
, m_characters(string.characters())
|
|
||||||
, m_length(string.length())
|
, m_length(string.length())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
|
||||||
StringView::StringView(const FlyString& string)
|
StringView::StringView(const FlyString& string)
|
||||||
: m_impl(string.impl())
|
: m_characters(string.characters())
|
||||||
, m_characters(string.characters())
|
|
||||||
, m_length(string.length())
|
, m_length(string.length())
|
||||||
{
|
{
|
||||||
}
|
}
|
||||||
|
@ -251,8 +249,6 @@ unsigned StringView::hash() const
|
||||||
{
|
{
|
||||||
if (is_empty())
|
if (is_empty())
|
||||||
return 0;
|
return 0;
|
||||||
if (m_impl)
|
|
||||||
return m_impl->hash();
|
|
||||||
return string_hash(characters_without_null_termination(), length());
|
return string_hash(characters_without_null_termination(), length());
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -1,5 +1,5 @@
|
||||||
/*
|
/*
|
||||||
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
* Copyright (c) 2018-2021, Andreas Kling <kling@serenityos.org>
|
||||||
* All rights reserved.
|
* All rights reserved.
|
||||||
*
|
*
|
||||||
* Redistribution and use in source and binary forms, with or without
|
* Redistribution and use in source and binary forms, with or without
|
||||||
|
@ -209,8 +209,6 @@ public:
|
||||||
return m_length < other.m_length;
|
return m_length < other.m_length;
|
||||||
}
|
}
|
||||||
|
|
||||||
const StringImpl* impl() const { return m_impl; }
|
|
||||||
|
|
||||||
[[nodiscard]] String to_string() const;
|
[[nodiscard]] String to_string() const;
|
||||||
|
|
||||||
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
[[nodiscard]] bool is_whitespace() const { return StringUtils::is_whitespace(*this); }
|
||||||
|
@ -227,7 +225,6 @@ private:
|
||||||
[[nodiscard]] bool is_one_of() const { return false; }
|
[[nodiscard]] bool is_one_of() const { return false; }
|
||||||
|
|
||||||
friend class String;
|
friend class String;
|
||||||
const StringImpl* m_impl { nullptr };
|
|
||||||
const char* m_characters { nullptr };
|
const char* m_characters { nullptr };
|
||||||
size_t m_length { 0 };
|
size_t m_length { 0 };
|
||||||
};
|
};
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue