diff --git a/src/buffer.cc b/src/buffer.cc index cd014574..e140f333 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -199,9 +199,9 @@ struct Buffer::Modification Type type; ByteCoord coord; - SharedString content; + StringDataPtr content; - Modification(Type type, ByteCoord coord, SharedString content) + Modification(Type type, ByteCoord coord, StringDataPtr content) : type(type), coord(coord), content(std::move(content)) {} Modification inverse() const @@ -248,7 +248,7 @@ void Buffer::reload(StringView data, timespec fs_timestamp) for (LineCount line = 0; line < d.len; ++line) m_current_undo_group.emplace_back( Modification::Insert, cur_line + line, - SharedString{parsed_lines.lines[(int)(d.posB + line)]}); + parsed_lines.lines[(int)(d.posB + line)]); m_changes.push_back({ Change::Insert, it == m_lines.end(), cur_line, cur_line + d.len }); m_lines.insert(it, &parsed_lines.lines[d.posB], &parsed_lines.lines[d.posB + d.len]); @@ -261,7 +261,7 @@ void Buffer::reload(StringView data, timespec fs_timestamp) for (LineCount line = d.len-1; line >= 0; --line) m_current_undo_group.emplace_back( Modification::Erase, cur_line + line, - SharedString{m_lines.get_storage(cur_line + line)}); + m_lines.get_storage(cur_line + line)); it = m_lines.erase(it, it + d.len); m_changes.push_back({ Change::Erase, it == m_lines.end(), cur_line, cur_line + d.len }); @@ -433,7 +433,7 @@ ByteCoord Buffer::do_erase(ByteCoord begin, ByteCoord end) void Buffer::apply_modification(const Modification& modification) { - StringView content = modification.content; + StringView content = modification.content->strview(); ByteCoord coord = modification.coord; kak_assert(is_valid(coord)); @@ -467,7 +467,7 @@ BufferIterator Buffer::insert(const BufferIterator& pos, StringView content) if (content.empty()) return pos; - SharedString real_content; + StringDataPtr real_content; if (pos == end() and content.back() != '\n') real_content = intern(content + "\n"); else @@ -478,7 +478,7 @@ BufferIterator Buffer::insert(const BufferIterator& pos, StringView content) auto coord = pos == end() ? ByteCoord{line_count()} : pos.coord(); if (not (m_flags & Flags::NoUndo)) m_current_undo_group.emplace_back(Modification::Insert, coord, real_content); - return {*this, do_insert(pos.coord(), real_content)}; + return {*this, do_insert(pos.coord(), real_content->strview())}; } BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end) diff --git a/src/shared_string.cc b/src/shared_string.cc index b594a56f..9d816a58 100644 --- a/src/shared_string.cc +++ b/src/shared_string.cc @@ -4,22 +4,22 @@ namespace Kakoune { -SharedString StringRegistry::intern(StringView str) +StringDataPtr StringRegistry::intern(StringView str) { - auto it = m_strings.find({str, SharedString::NoCopy{}}); + auto it = m_strings.find(str); if (it == m_strings.end()) { - SharedString shared_str = str; - it = m_strings.emplace(shared_str).first; + auto data = StringData::create(str); + it = m_strings.emplace(data->strview(), data).first; } - return *it; + return it->second; } void StringRegistry::purge_unused() { for (auto it = m_strings.begin(); it != m_strings.end(); ) { - if (it->m_storage->refcount == 1) + if (it->second->refcount == 1) it = m_strings.erase(it); else ++it; @@ -34,8 +34,8 @@ void StringRegistry::debug_stats() const size_t count = m_strings.size(); for (auto& st : m_strings) { - total_refcount += st.m_storage->refcount - 1; - total_size += (int)st.m_storage->length; + total_refcount += st.second->refcount - 1; + total_size += (int)st.second->length; } write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count)); write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count)); diff --git a/src/shared_string.hh b/src/shared_string.hh index 1aadafd0..2dfc3d38 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -102,14 +102,14 @@ class StringRegistry : public Singleton { public: void debug_stats() const; - SharedString intern(StringView str); + StringDataPtr intern(StringView str); void purge_unused(); private: - UnorderedSet m_strings; + UnorderedMap m_strings; }; -inline SharedString intern(StringView str) +inline StringDataPtr intern(StringView str) { return StringRegistry::instance().intern(str); } diff --git a/src/word_db.cc b/src/word_db.cc index 147287b5..51af7a3f 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -67,7 +67,7 @@ void WordDB::add_words(const SharedString& line) { for (auto& w : get_words(line)) { - WordDB::WordInfo& info = m_words[intern(w)]; + WordDB::WordInfo& info = m_words[SharedString{intern(w)}]; ++info.refcount; if (info.letters.none()) info.letters = used_letters(w);