Skip to content

Commit

Permalink
AK: Add FlyString, a simple flyweight string class
Browse files Browse the repository at this point in the history
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
  • Loading branch information
awesomekling committed Mar 22, 2020
1 parent 0395b25 commit 4f72f6b
Show file tree
Hide file tree
Showing 15 changed files with 232 additions and 28 deletions.
91 changes: 91 additions & 0 deletions AK/FlyString.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
/*
* Copyright (c) 2020, Andreas Kling <[email protected]>
* 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 changes: 64 additions & 0 deletions AK/FlyString.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,64 @@
/*
* Copyright (c) 2020, Andreas Kling <[email protected]>
* 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;
2 changes: 2 additions & 0 deletions AK/Forward.h
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,7 @@ class StringBuilder;
class StringImpl;
class StringView;
class URL;
class FlyString;
class Utf8View;

template<typename T>
Expand Down Expand Up @@ -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;
3 changes: 3 additions & 0 deletions AK/StringImpl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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>
Expand Down Expand Up @@ -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);
Expand Down
6 changes: 6 additions & 0 deletions AK/StringImpl.h
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@

#pragma once

#include <AK/Badge.h>
#include <AK/RefCounted.h>
#include <AK/RefPtr.h>
#include <AK/Types.h>
Expand Down Expand Up @@ -70,11 +71,15 @@ class StringImpl : public RefCounted<StringImpl> {
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';
}
Expand All @@ -89,6 +94,7 @@ class StringImpl : public RefCounted<StringImpl> {
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];
};

Expand Down
8 changes: 8 additions & 0 deletions AK/StringView.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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())
Expand Down
1 change: 1 addition & 0 deletions AK/StringView.h
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ class StringView {

StringView(const ByteBuffer&);
StringView(const String&);
StringView(const FlyString&);

bool is_null() const { return !m_characters; }
bool is_empty() const { return m_length == 0; }
Expand Down
1 change: 1 addition & 0 deletions AK/Tests/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ SHARED_TEST_SOURCES = \
../LogStream.cpp \
../JsonValue.cpp \
../JsonParser.cpp \
../FlyString.cpp \
../FileSystemPath.cpp \
../URL.cpp \
../Utf8View.cpp
Expand Down
22 changes: 22 additions & 0 deletions AK/Tests/TestString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,9 @@

#include <AK/TestSuite.h>

#include <AK/FlyString.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>

TEST_CASE(construct_empty)
{
Expand Down Expand Up @@ -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)
11 changes: 6 additions & 5 deletions DevTools/FormCompiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ PROGRAM = FormCompiler

OBJS = \
main.o \
../../AK/FlyString.o \
../../AK/JsonParser.o \
../../AK/JsonValue.o \
../../AK/LogStream.o \
../../AK/String.o \
../../AK/StringImpl.o \
../../AK/StringBuilder.o \
../../AK/StringView.o \
../../AK/StringImpl.o \
../../AK/StringUtils.o \
../../AK/JsonValue.o \
../../AK/JsonParser.o \
../../AK/LogStream.o \
../../AK/StringView.o \
../../Libraries/LibCore/IODevice.o \
../../Libraries/LibCore/File.o \
../../Libraries/LibCore/Object.o \
Expand Down
11 changes: 6 additions & 5 deletions DevTools/IPCCompiler/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ PROGRAM = IPCCompiler

OBJS = \
main.o \
../../AK/FlyString.o \
../../AK/JsonParser.o \
../../AK/JsonValue.o \
../../AK/LogStream.o \
../../AK/String.o \
../../AK/StringImpl.o \
../../AK/StringBuilder.o \
../../AK/StringView.o \
../../AK/StringImpl.o \
../../AK/StringUtils.o \
../../AK/JsonValue.o \
../../AK/JsonParser.o \
../../AK/LogStream.o \
../../AK/StringView.o \
../../Libraries/LibCore/IODevice.o \
../../Libraries/LibCore/File.o \
../../Libraries/LibCore/Object.o \
Expand Down
3 changes: 2 additions & 1 deletion Kernel/Makefile
Original file line number Diff line number Diff line change
@@ -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 \
Expand Down
15 changes: 8 additions & 7 deletions Libraries/LibC/Makefile
Original file line number Diff line number Diff line change
@@ -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 = \
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,14 +4,15 @@ PROGRAM = Generate_CSS_PropertyID_cpp

OBJS = \
Generate_CSS_PropertyID_cpp.o \
../../../../AK/FlyString.o \
../../../../AK/JsonParser.o \
../../../../AK/JsonValue.o \
../../../../AK/LogStream.o \
../../../../AK/String.o \
../../../../AK/StringImpl.o \
../../../../AK/StringBuilder.o \
../../../../AK/StringView.o \
../../../../AK/StringImpl.o \
../../../../AK/StringUtils.o \
../../../../AK/JsonValue.o \
../../../../AK/JsonParser.o \
../../../../AK/LogStream.o \
../../../../AK/StringView.o \
../../../../Libraries/LibCore/IODevice.o \
../../../../Libraries/LibCore/File.o \
../../../../Libraries/LibCore/Object.o \
Expand Down
Loading

0 comments on commit 4f72f6b

Please sign in to comment.