From 9b057896d4d28360d4ff950d01cda0fdba19d2a3 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Sun, 18 Jan 2015 18:23:58 +0000 Subject: Replace std::shared_ptr with homemade, intrusive, ref_ptr That saves a lot of memory as sizeof(SharedString) is now one pointer less. --- src/shared_string.hh | 32 +++++++++++++++++++++++--------- 1 file changed, 23 insertions(+), 9 deletions(-) (limited to 'src/shared_string.hh') 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 - namespace Kakoune { class SharedString : public StringView { public: - using Storage = std::basic_string, - Allocator>; + struct Storage + { + int refcount = 0; + Vector 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(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) + SharedString(StringView str, ref_ptr storage) : StringView{str}, m_storage(std::move(storage)) {} friend class StringRegistry; - std::shared_ptr m_storage; + ref_ptr m_storage; }; inline size_t hash_value(const SharedString& str) @@ -59,7 +73,7 @@ public: void purge_unused(); private: - UnorderedMap, MemoryDomain::SharedString> m_strings; + UnorderedMap, MemoryDomain::SharedString> m_strings; }; inline SharedString intern(StringView str) -- cgit v1.2.3