AK: Add FlyString, a simple flyweight string class

FlyString is a flyweight string class that wraps a RefPtr<StringImpl>
known to be unique among the set of FlyStrings. The class is very
unoptimized at the moment.

When to use FlyString:

- When you want O(1) string comparison
- When you want to deduplicate a lot of identical strings

When not to use FlyString:

- For strings that don't need either of the above features
- For strings that are likely to be unique
This commit is contained in:
Andreas Kling 2020-03-22 10:12:55 +01:00
parent 0395b25e3f
commit 4f72f6b886
Notes: sideshowbarker 2024-07-19 08:11:13 +09:00
15 changed files with 236 additions and 32 deletions

91
AK/FlyString.cpp Normal file
View file

@ -0,0 +1,91 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FlyString.h>
#include <AK/HashTable.h>
#include <AK/String.h>
#include <AK/StringUtils.h>
namespace AK {
struct FlyStringImplTraits : public AK::Traits<StringImpl*> {
static unsigned hash(const StringImpl* s) { return s ? s->hash() : 0; }
static bool equals(const StringImpl* a, const StringImpl* b)
{
ASSERT(a);
ASSERT(b);
if (a == b)
return true;
if (a->length() != b->length())
return false;
return !__builtin_memcmp(a->characters(), b->characters(), a->length());
}
};
static HashTable<StringImpl*, FlyStringImplTraits>& fly_impls()
{
static HashTable<StringImpl*, FlyStringImplTraits>* table;
if (!table)
table = new HashTable<StringImpl*, FlyStringImplTraits>;
return *table;
}
void FlyString::did_destroy_impl(Badge<StringImpl>, StringImpl& impl)
{
fly_impls().remove(&impl);
}
FlyString::FlyString(const String& string)
{
if (string.is_null())
return;
auto it = fly_impls().find(const_cast<StringImpl*>(string.impl()));
if (it == fly_impls().end()) {
fly_impls().set(const_cast<StringImpl*>(string.impl()));
string.impl()->set_fly({}, true);
m_impl = string.impl();
} else {
ASSERT((*it)->is_fly());
m_impl = *it;
}
}
FlyString::FlyString(const StringView& string)
: FlyString(static_cast<String>(string))
{
}
FlyString::FlyString(const char* string)
: FlyString(static_cast<String>(string))
{
}
int FlyString::to_int(bool& ok) const
{
return StringUtils::convert_to_int(view(), ok);
}
}

64
AK/FlyString.h Normal file
View file

@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Andreas Kling <kling@serenityos.org>
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#pragma once
#include <AK/String.h>
namespace AK {
class FlyString {
public:
FlyString() {}
FlyString(const String&);
FlyString(const StringView&);
FlyString(const char*);
bool operator==(const FlyString& other) const { return m_impl == other.m_impl; }
bool operator!=(const FlyString& other) const { return m_impl != other.m_impl; }
const StringImpl* impl() const { return m_impl; }
const char* characters() const { return m_impl ? m_impl->characters() : nullptr; }
size_t length() const { return m_impl ? m_impl->length() : 0; }
StringView view() const { return { characters(), length() }; }
int to_int(bool& ok) const;
static void did_destroy_impl(Badge<StringImpl>, StringImpl&);
private:
RefPtr<StringImpl> m_impl;
};
template<>
struct Traits<FlyString> : public GenericTraits<FlyString> {
static unsigned hash(const FlyString& s) { return s.impl() ? s.impl()->hash() : 0; }
};
}
using AK::FlyString;

View file

@ -45,6 +45,7 @@ class StringBuilder;
class StringImpl;
class StringView;
class URL;
class FlyString;
class Utf8View;
template<typename T>
@ -137,5 +138,6 @@ using AK::StringImpl;
using AK::StringView;
using AK::Traits;
using AK::URL;
using AK::FlyString;
using AK::Utf8View;
using AK::Vector;

View file

@ -24,6 +24,7 @@
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#include <AK/FlyString.h>
#include <AK/HashTable.h>
#include <AK/Memory.h>
#include <AK/StdLibExtras.h>
@ -72,6 +73,8 @@ StringImpl::StringImpl(ConstructWithInlineBufferTag, size_t length)
StringImpl::~StringImpl()
{
if (m_fly)
FlyString::did_destroy_impl({}, *this);
#ifdef DEBUG_STRINGIMPL
--g_stringimpl_count;
g_all_live_stringimpls->remove(this);

View file

@ -26,6 +26,7 @@
#pragma once
#include <AK/Badge.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Types.h>
@ -70,11 +71,15 @@ public:
return m_hash;
}
bool is_fly() const { return m_fly; }
void set_fly(Badge<FlyString>, bool fly) const { m_fly = fly; }
private:
enum ConstructTheEmptyStringImplTag {
ConstructTheEmptyStringImpl
};
explicit StringImpl(ConstructTheEmptyStringImplTag)
: m_fly(true)
{
m_inline_buffer[0] = '\0';
}
@ -89,6 +94,7 @@ private:
size_t m_length { 0 };
mutable unsigned m_hash { 0 };
mutable bool m_has_hash { false };
mutable bool m_fly { false };
char m_inline_buffer[0];
};

View file

@ -28,6 +28,7 @@
#include <AK/Memory.h>
#include <AK/String.h>
#include <AK/StringView.h>
#include <AK/FlyString.h>
#include <AK/Vector.h>
namespace AK {
@ -39,6 +40,13 @@ StringView::StringView(const String& string)
{
}
StringView::StringView(const FlyString& string)
: m_impl(string.impl())
, m_characters(string.characters())
, m_length(string.length())
{
}
StringView::StringView(const ByteBuffer& buffer)
: m_characters((const char*)buffer.data())
, m_length(buffer.size())

View file

@ -55,6 +55,7 @@ public:
StringView(const ByteBuffer&);
StringView(const String&);
StringView(const FlyString&);
bool is_null() const { return !m_characters; }
bool is_empty() const { return m_length == 0; }

View file

@ -7,6 +7,7 @@ SHARED_TEST_SOURCES = \
../LogStream.cpp \
../JsonValue.cpp \
../JsonParser.cpp \
../FlyString.cpp \
../FileSystemPath.cpp \
../URL.cpp \
../Utf8View.cpp

View file

@ -26,7 +26,9 @@
#include <AK/TestSuite.h>
#include <AK/FlyString.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
TEST_CASE(construct_empty)
{
@ -137,4 +139,24 @@ TEST_CASE(to_uppercase)
EXPECT(String("AbC").to_uppercase() == "ABC");
}
TEST_CASE(flystring)
{
{
FlyString a("foo");
FlyString b("foo");
EXPECT_EQ(a.impl(), b.impl());
}
{
String a = "foo";
FlyString b = a;
StringBuilder builder;
builder.append('f');
builder.append("oo");
FlyString c = builder.to_string();
EXPECT_EQ(a.impl(), b.impl());
EXPECT_EQ(a.impl(), c.impl());
}
}
TEST_MAIN(String)

View file

@ -4,14 +4,15 @@ PROGRAM = FormCompiler
OBJS = \
main.o \
../../AK/String.o \
../../AK/StringImpl.o \
../../AK/StringBuilder.o \
../../AK/StringView.o \
../../AK/StringUtils.o \
../../AK/JsonValue.o \
../../AK/FlyString.o \
../../AK/JsonParser.o \
../../AK/JsonValue.o \
../../AK/LogStream.o \
../../AK/String.o \
../../AK/StringBuilder.o \
../../AK/StringImpl.o \
../../AK/StringUtils.o \
../../AK/StringView.o \
../../Libraries/LibCore/IODevice.o \
../../Libraries/LibCore/File.o \
../../Libraries/LibCore/Object.o \

View file

@ -4,14 +4,15 @@ PROGRAM = IPCCompiler
OBJS = \
main.o \
../../AK/String.o \
../../AK/StringImpl.o \
../../AK/StringBuilder.o \
../../AK/StringView.o \
../../AK/StringUtils.o \
../../AK/JsonValue.o \
../../AK/FlyString.o \
../../AK/JsonParser.o \
../../AK/JsonValue.o \
../../AK/LogStream.o \
../../AK/String.o \
../../AK/StringBuilder.o \
../../AK/StringImpl.o \
../../AK/StringUtils.o \
../../AK/StringView.o \
../../Libraries/LibCore/IODevice.o \
../../Libraries/LibCore/File.o \
../../Libraries/LibCore/Object.o \

View file

@ -1,13 +1,14 @@
OBJS = \
../AK/FileSystemPath.o \
../AK/FlyString.o \
../AK/JsonParser.o \
../AK/JsonValue.o \
../AK/LogStream.o \
../AK/String.o \
../AK/StringBuilder.o \
../AK/StringImpl.o \
../AK/StringView.o \
../AK/StringUtils.o \
../AK/StringView.o \
../Libraries/LibELF/ELFImage.o \
../Libraries/LibELF/ELFLoader.o \
../Libraries/LibBareMetal/Output/Console.o \

View file

@ -1,16 +1,17 @@
AK_OBJS = \
../../AK/StringImpl.o \
../../AK/String.o \
../../AK/StringView.o \
../../AK/StringBuilder.o \
../../AK/StringUtils.o \
../../AK/FileSystemPath.o \
../../AK/URL.o \
../../AK/JsonValue.o \
../../AK/FlyString.o \
../../AK/JsonParser.o \
../../AK/JsonValue.o \
../../AK/LogStream.o \
../../AK/MappedFile.o \
../../AK/SharedBuffer.o \
../../AK/String.o \
../../AK/StringBuilder.o \
../../AK/StringImpl.o \
../../AK/StringUtils.o \
../../AK/StringView.o \
../../AK/URL.o \
../../AK/Utf8View.o
LIBC_OBJS = \

View file

@ -4,14 +4,15 @@ PROGRAM = Generate_CSS_PropertyID_cpp
OBJS = \
Generate_CSS_PropertyID_cpp.o \
../../../../AK/String.o \
../../../../AK/StringImpl.o \
../../../../AK/StringBuilder.o \
../../../../AK/StringView.o \
../../../../AK/StringUtils.o \
../../../../AK/JsonValue.o \
../../../../AK/FlyString.o \
../../../../AK/JsonParser.o \
../../../../AK/JsonValue.o \
../../../../AK/LogStream.o \
../../../../AK/String.o \
../../../../AK/StringBuilder.o \
../../../../AK/StringImpl.o \
../../../../AK/StringUtils.o \
../../../../AK/StringView.o \
../../../../Libraries/LibCore/IODevice.o \
../../../../Libraries/LibCore/File.o \
../../../../Libraries/LibCore/Object.o \

View file

@ -4,14 +4,15 @@ PROGRAM = Generate_CSS_PropertyID_h
OBJS = \
Generate_CSS_PropertyID_h.o \
../../../../AK/StringUtils.o \
../../../../AK/String.o \
../../../../AK/StringImpl.o \
../../../../AK/StringBuilder.o \
../../../../AK/StringView.o \
../../../../AK/JsonValue.o \
../../../../AK/FlyString.o \
../../../../AK/JsonParser.o \
../../../../AK/JsonValue.o \
../../../../AK/LogStream.o \
../../../../AK/String.o \
../../../../AK/StringBuilder.o \
../../../../AK/StringImpl.o \
../../../../AK/StringUtils.o \
../../../../AK/StringView.o \
../../../../Libraries/LibCore/IODevice.o \
../../../../Libraries/LibCore/File.o \
../../../../Libraries/LibCore/Object.o \