summaryrefslogtreecommitdiff
path: root/src/shared_string.hh
diff options
context:
space:
mode:
authorMaxime Coste <frrrwww@gmail.com>2015-01-18 18:23:58 +0000
committerMaxime Coste <frrrwww@gmail.com>2015-01-18 18:23:58 +0000
commit9b057896d4d28360d4ff950d01cda0fdba19d2a3 (patch)
treec7892541cabd2ad6b450a30f600e283683560182 /src/shared_string.hh
parentef26b77aa71ef339ac2d67c1f17c0e16990fa320 (diff)
Replace std::shared_ptr with homemade, intrusive, ref_ptr
That saves a lot of memory as sizeof(SharedString) is now one pointer less.
Diffstat (limited to 'src/shared_string.hh')
-rw-r--r--src/shared_string.hh32
1 files changed, 23 insertions, 9 deletions
diff --git a/src/shared_string.hh b/src/shared_string.hh
index 982cb67f..e2240cc9 100644
--- a/src/shared_string.hh
+++ b/src/shared_string.hh
@@ -2,26 +2,40 @@
#define shared_string_hh_INCLUDED
#include "string.hh"
+#include "ref_ptr.hh"
#include "utils.hh"
#include "unordered_map.hh"
-#include <memory>
-
namespace Kakoune
{
class SharedString : public StringView
{
public:
- using Storage = std::basic_string<char, std::char_traits<char>,
- Allocator<char, MemoryDomain::SharedString>>;
+ struct Storage
+ {
+ int refcount = 0;
+ Vector<char, MemoryDomain::SharedString> content;
+
+ Storage(StringView str)
+ {
+ content.reserve((int)str.length() + 1);
+ content.assign(str.begin(), str.end());
+ content.push_back('\0');
+ }
+ StringView strview() const { return {&content.front(), &content.back()}; }
+
+ friend void inc_ref_count(Storage* s) { ++s->refcount; }
+ friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) delete s; }
+ };
+
SharedString() = default;
SharedString(StringView str)
{
if (not str.empty())
{
- m_storage = std::make_shared<Storage>(str.begin(), str.end());
- StringView::operator=(*m_storage);
+ m_storage = new Storage{str};
+ StringView::operator=(m_storage->strview());
}
}
struct NoCopy{};
@@ -39,11 +53,11 @@ public:
}
private:
- SharedString(StringView str, std::shared_ptr<Storage> storage)
+ SharedString(StringView str, ref_ptr<Storage> storage)
: StringView{str}, m_storage(std::move(storage)) {}
friend class StringRegistry;
- std::shared_ptr<Storage> m_storage;
+ ref_ptr<Storage> m_storage;
};
inline size_t hash_value(const SharedString& str)
@@ -59,7 +73,7 @@ public:
void purge_unused();
private:
- UnorderedMap<StringView, std::shared_ptr<SharedString::Storage>, MemoryDomain::SharedString> m_strings;
+ UnorderedMap<StringView, ref_ptr<SharedString::Storage>, MemoryDomain::SharedString> m_strings;
};
inline SharedString intern(StringView str)