From b1281d225d5baa73dc0763627d18bb45a4dc97b2 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Mon, 19 Jan 2015 19:31:56 +0000 Subject: [PATCH] rename SharedString::Storage to StringStorage and use directly in Buffer --- src/buffer.cc | 30 +++++++++---------- src/buffer.hh | 22 +++++++++++--- src/shared_string.cc | 2 +- src/shared_string.hh | 69 +++++++++++++++++++++++--------------------- src/word_db.cc | 2 +- 5 files changed, 71 insertions(+), 54 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index 32a909a0..c5473054 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -34,7 +34,7 @@ Buffer::Buffer(String name, Flags flags, Vector lines, for (auto& line : lines) { kak_assert(not line.empty() and line.back() == '\n'); - m_lines.emplace_back(std::move(line)); + m_lines.emplace_back(StringStorage::create(line)); } m_changes.push_back({ Change::Insert, {0,0}, line_count(), true }); @@ -107,7 +107,7 @@ ByteCoord Buffer::clamp(ByteCoord coord) const ByteCoord Buffer::offset_coord(ByteCoord coord, CharCount offset) { - auto& line = m_lines[coord.line]; + StringView line = m_lines[coord.line]; auto character = std::max(0_char, std::min(line.char_count_to(coord.column) + offset, line.char_length() - 1)); return {coord.line, line.byte_count_to(character)}; @@ -117,7 +117,7 @@ ByteCoordAndTarget Buffer::offset_coord(ByteCoordAndTarget coord, LineCount offs { auto character = coord.target == -1 ? m_lines[coord.line].char_count_to(coord.column) : coord.target; auto line = Kakoune::clamp(coord.line + offset, 0_line, line_count()-1); - auto& content = m_lines[line]; + StringView content = m_lines[line]; character = std::max(0_char, std::min(character, content.char_length() - 2)); return {line, content.byte_count_to(character), character}; @@ -178,7 +178,7 @@ void Buffer::reload(Vector lines, time_t fs_timestamp) for (auto& line : lines) { kak_assert(not line.empty() and line.back() == '\n'); - m_lines.emplace_back(std::move(line)); + m_lines.emplace_back(StringStorage::create(line)); if (not (m_flags & Flags::NoUndo)) m_current_undo_group.emplace_back( Modification::Insert, line_count()-1, m_lines.back()); @@ -243,8 +243,8 @@ void Buffer::check_invariant() const kak_assert(not m_lines.empty()); for (auto& line : m_lines) { - kak_assert(line.length() > 0); - kak_assert(line.back() == '\n'); + kak_assert(line->strview().length() > 0); + kak_assert(line->strview().back() == '\n'); } #endif } @@ -268,12 +268,12 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content) { if (content[i] == '\n') { - m_lines.push_back(content.substr(start, i + 1 - start)); + m_lines.push_back(StringStorage::create(content.substr(start, i + 1 - start))); start = i + 1; } } if (start != content.length()) - m_lines.push_back(content.substr(start)); + m_lines.push_back(StringStorage::create(content.substr(start))); begin = pos.column == 0 ? pos : ByteCoord{ pos.line + 1, 0 }; end = ByteCoord{ line_count(), 0 }; @@ -284,7 +284,7 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content) StringView prefix = m_lines[pos.line].substr(0, pos.column); StringView suffix = m_lines[pos.line].substr(pos.column); - Vector new_lines; + LineList new_lines; ByteCount start = 0; for (ByteCount i = 0; i < content.length(); ++i) @@ -293,16 +293,16 @@ ByteCoord Buffer::do_insert(ByteCoord pos, StringView content) { StringView line_content = content.substr(start, i + 1 - start); if (start == 0) - new_lines.emplace_back(prefix + line_content); + new_lines.emplace_back(StringStorage::create(prefix + line_content)); else - new_lines.push_back(line_content); + new_lines.push_back(StringStorage::create(line_content)); start = i + 1; } } if (start == 0) - new_lines.emplace_back(prefix + content + suffix); + new_lines.emplace_back(StringStorage::create(prefix + content + suffix)); else if (start != content.length() or not suffix.empty()) - new_lines.emplace_back(content.substr(start) + suffix); + new_lines.emplace_back(StringStorage::create(content.substr(start) + suffix)); LineCount last_line = pos.line + new_lines.size() - 1; @@ -332,7 +332,7 @@ ByteCoord Buffer::do_erase(ByteCoord begin, ByteCoord end) if (new_line.length() != 0) { m_lines.erase(m_lines.begin() + (int)begin.line, m_lines.begin() + (int)end.line); - m_lines[begin.line] = SharedString(new_line); + m_lines.get_storage(begin.line) = StringStorage::create(new_line); next = begin; } else @@ -547,7 +547,7 @@ String Buffer::debug_description() const size_t content_size = 0; for (auto& line : m_lines) - content_size += (int)line.length(); + content_size += (int)line->strview().length(); size_t additional_size = 0; for (auto& undo_group : m_history) diff --git a/src/buffer.hh b/src/buffer.hh index 6121d36f..e36d5580 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -121,9 +121,12 @@ public: BufferIterator end() const; LineCount line_count() const; - const SharedString& operator[](LineCount line) const + StringView operator[](LineCount line) const { return m_lines[line]; } + SharedString shared_line(LineCount line) const + { return m_lines.get_shared(line); } + // returns an iterator at given coordinates. clamp line_and_column BufferIterator iterator_at(ByteCoord coord) const; @@ -166,16 +169,27 @@ private: void on_option_changed(const Option& option) override; - using LineListBase = Vector; + using LineListBase = Vector, MemoryDomain::BufferContent>; struct LineList : LineListBase { [[gnu::always_inline]] - SharedString& operator[](LineCount line) + ref_ptr& get_storage(LineCount line) { return LineListBase::operator[]((int)line); } [[gnu::always_inline]] - const SharedString& operator[](LineCount line) const + const ref_ptr& get_storage(LineCount line) const { return LineListBase::operator[]((int)line); } + + [[gnu::always_inline]] + SharedString get_shared(LineCount line) const + { return SharedString{get_storage(line)}; } + + [[gnu::always_inline]] + StringView operator[](LineCount line) const + { return get_storage(line)->strview(); } + + StringView front() const { return LineListBase::front()->strview(); } + StringView back() const { return LineListBase::back()->strview(); } }; LineList m_lines; diff --git a/src/shared_string.cc b/src/shared_string.cc index 52605342..a02dfcda 100644 --- a/src/shared_string.cc +++ b/src/shared_string.cc @@ -12,7 +12,7 @@ SharedString StringRegistry::intern(StringView str) SharedString shared_str = str; it = m_strings.emplace(shared_str, shared_str.m_storage).first; } - return {it->second->strview(), it->second}; + return SharedString{it->second}; } void StringRegistry::purge_unused() diff --git a/src/shared_string.hh b/src/shared_string.hh index a0a4a8e1..489d9326 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -9,44 +9,44 @@ namespace Kakoune { +struct StringStorage : UseMemoryDomain +{ + int refcount; + int length; + char data[1]; + + StringView strview() const { return {data, length}; } + + static StringStorage* create(StringView str) + { + const int len = (int)str.length(); + void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); + StringStorage* res = reinterpret_cast(ptr); + memcpy(res->data, str.data(), len); + res->refcount = 0; + res->length = len; + res->data[len] = 0; + return res; + } + + static void destroy(StringStorage* s) + { + StringStorage::operator delete(s, sizeof(StringStorage) + s->length); + } + + friend void inc_ref_count(StringStorage* s) { ++s->refcount; } + friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); } +}; + class SharedString : public StringView { public: - struct Storage : UseMemoryDomain - { - int refcount; - int length; - char data[1]; - - StringView strview() const { return {data, length}; } - - static Storage* create(StringView str) - { - const int len = (int)str.length(); - void* ptr = Storage::operator new(sizeof(Storage) + len); - Storage* res = reinterpret_cast(ptr); - memcpy(res->data, str.data(), len); - res->refcount = 0; - res->length = len; - res->data[len] = 0; - return res; - } - - static void destroy(Storage* s) - { - Storage::operator delete(s, sizeof(Storage) + s->length); - } - - friend void inc_ref_count(Storage* s) { ++s->refcount; } - friend void dec_ref_count(Storage* s) { if (--s->refcount == 0) Storage::destroy(s); } - }; - SharedString() = default; SharedString(StringView str) { if (not str.empty()) { - m_storage = Storage::create(str); + m_storage = StringStorage::create(str); StringView::operator=(m_storage->strview()); } } @@ -64,12 +64,15 @@ public: return SharedString{StringView::substr(from, length), m_storage}; } + explicit SharedString(ref_ptr storage) + : StringView{storage->strview()}, m_storage(std::move(storage)) {} + private: - SharedString(StringView str, ref_ptr storage) + SharedString(StringView str, ref_ptr storage) : StringView{str}, m_storage(std::move(storage)) {} friend class StringRegistry; - ref_ptr m_storage; + ref_ptr m_storage; }; inline size_t hash_value(const SharedString& str) @@ -85,7 +88,7 @@ public: void purge_unused(); private: - UnorderedMap, MemoryDomain::SharedString> m_strings; + UnorderedMap, MemoryDomain::SharedString> m_strings; }; inline SharedString intern(StringView str) diff --git a/src/word_db.cc b/src/word_db.cc index 844aa196..1332dd3c 100644 --- a/src/word_db.cc +++ b/src/word_db.cc @@ -119,7 +119,7 @@ void WordDB::update_db() if (modif.new_line + l >= buffer.line_count()) break; - new_lines.push_back(buffer[modif.new_line + l]); + new_lines.push_back(buffer.shared_line(modif.new_line + l)); add_words(get_words(new_lines.back())); } }