From cb197f57ba77ae16ed90d0e470f79d3245e16a70 Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Thu, 22 Jan 2015 13:39:29 +0000 Subject: [PATCH] Avoid temporary strings on buffer load/reload Pass directly a Vector> to the buffer --- src/buffer.cc | 21 +++++++++------------ src/buffer.hh | 17 +++++++++-------- src/buffer_utils.cc | 16 +++------------- src/shared_string.hh | 13 ++++++++++--- src/unit_tests.cc | 16 ++++++++-------- 5 files changed, 39 insertions(+), 44 deletions(-) diff --git a/src/buffer.cc b/src/buffer.cc index c5473054..629842e2 100644 --- a/src/buffer.cc +++ b/src/buffer.cc @@ -15,7 +15,7 @@ namespace Kakoune { -Buffer::Buffer(String name, Flags flags, Vector lines, +Buffer::Buffer(String name, Flags flags, BufferLines lines, time_t fs_timestamp) : Scope(GlobalScope::instance()), m_name(flags & Flags::File ? real_path(parse_filename(name)) : std::move(name)), @@ -28,14 +28,13 @@ Buffer::Buffer(String name, Flags flags, Vector lines, options().register_watcher(*this); if (lines.empty()) - lines.emplace_back("\n"); + lines.emplace_back(StringStorage::create("\n")); - m_lines.reserve(lines.size()); for (auto& line : lines) { - kak_assert(not line.empty() and line.back() == '\n'); - m_lines.emplace_back(StringStorage::create(line)); + kak_assert(not line->length == 0 and line->data[line->length-1] == '\n'); } + static_cast(m_lines) = std::move(lines); m_changes.push_back({ Change::Insert, {0,0}, line_count(), true }); @@ -157,7 +156,7 @@ struct Buffer::Modification } }; -void Buffer::reload(Vector lines, time_t fs_timestamp) +void Buffer::reload(BufferLines lines, time_t fs_timestamp) { m_changes.push_back({ Change::Erase, {0,0}, back_coord(), true }); @@ -169,20 +168,18 @@ void Buffer::reload(Vector lines, time_t fs_timestamp) Modification::Erase, line, m_lines[line]); } - m_lines.clear(); - if (lines.empty()) - lines.emplace_back("\n"); + lines.emplace_back(StringStorage::create("\n")); - m_lines.reserve(lines.size()); for (auto& line : lines) { - kak_assert(not line.empty() and line.back() == '\n'); - m_lines.emplace_back(StringStorage::create(line)); + kak_assert(not line->length == 0 and line->data[line->length-1] == '\n'); if (not (m_flags & Flags::NoUndo)) m_current_undo_group.emplace_back( Modification::Insert, line_count()-1, m_lines.back()); } + static_cast(m_lines) = std::move(lines); + commit_undo_group(); m_last_save_undo_index = m_history_cursor - m_history.begin(); diff --git a/src/buffer.hh b/src/buffer.hh index e36d5580..9963b7cc 100644 --- a/src/buffer.hh +++ b/src/buffer.hh @@ -59,6 +59,8 @@ private: ByteCoord m_coord; }; +using BufferLines = Vector, MemoryDomain::BufferContent>; + // A Buffer is a in-memory representation of a file // // The Buffer class permits to read and mutate this file @@ -76,7 +78,7 @@ public: NoUndo = 8, }; - Buffer(String name, Flags flags, Vector lines = { "\n" }, + Buffer(String name, Flags flags, BufferLines lines = {}, time_t fs_timestamp = InvalidTime); Buffer(const Buffer&) = delete; Buffer& operator= (const Buffer&) = delete; @@ -150,7 +152,7 @@ public: void run_hook_in_own_context(const String& hook_name, StringView param); - void reload(Vector lines, time_t fs_timestamp = InvalidTime); + void reload(BufferLines lines, time_t fs_timestamp = InvalidTime); void check_invariant() const; @@ -169,16 +171,15 @@ private: void on_option_changed(const Option& option) override; - using LineListBase = Vector, MemoryDomain::BufferContent>; - struct LineList : LineListBase + struct LineList : BufferLines { [[gnu::always_inline]] ref_ptr& get_storage(LineCount line) - { return LineListBase::operator[]((int)line); } + { return BufferLines::operator[]((int)line); } [[gnu::always_inline]] const ref_ptr& get_storage(LineCount line) const - { return LineListBase::operator[]((int)line); } + { return BufferLines::operator[]((int)line); } [[gnu::always_inline]] SharedString get_shared(LineCount line) const @@ -188,8 +189,8 @@ private: StringView operator[](LineCount line) const { return get_storage(line)->strview(); } - StringView front() const { return LineListBase::front()->strview(); } - StringView back() const { return LineListBase::back()->strview(); } + StringView front() const { return BufferLines::front()->strview(); } + StringView back() const { return BufferLines::back()->strview(); } }; LineList m_lines; diff --git a/src/buffer_utils.cc b/src/buffer_utils.cc index b3b3e936..0b5941b9 100644 --- a/src/buffer_utils.cc +++ b/src/buffer_utils.cc @@ -39,24 +39,14 @@ Buffer* create_buffer_from_data(StringView data, StringView name, pos = data.begin() + 3; } - Vector lines; + BufferLines lines; while (pos < data.end()) { const char* line_end = pos; while (line_end < data.end() and *line_end != '\r' and *line_end != '\n') ++line_end; - // this should happen only when opening a file which has no - // end of line as last character. - if (line_end == data.end()) - { - lines.emplace_back(pos, line_end); - lines.back() += '\n'; - break; - } - - lines.emplace_back(pos, line_end + 1); - lines.back().back() = '\n'; + lines.emplace_back(StringStorage::create({pos, line_end}, '\n')); if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\n') { @@ -88,7 +78,7 @@ Buffer* create_fifo_buffer(String name, int fd, bool scroll) if (buffer) { buffer->flags() |= Buffer::Flags::NoUndo; - buffer->reload(Vector({"\n"_str}), 0); + buffer->reload({"\n"_ss}, 0); } else buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo); diff --git a/src/shared_string.hh b/src/shared_string.hh index 489d9326..b8db3c89 100644 --- a/src/shared_string.hh +++ b/src/shared_string.hh @@ -17,14 +17,16 @@ struct StringStorage : UseMemoryDomain StringView strview() const { return {data, length}; } - static StringStorage* create(StringView str) + static StringStorage* create(StringView str, char back = 0) { - const int len = (int)str.length(); + const int len = (int)str.length() + (back != 0 ? 1 : 0); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len); StringStorage* res = reinterpret_cast(ptr); - memcpy(res->data, str.data(), len); + memcpy(res->data, str.data(), (int)str.length()); res->refcount = 0; res->length = len; + if (back != 0) + res->data[len-1] = back; res->data[len] = 0; return res; } @@ -38,6 +40,11 @@ struct StringStorage : UseMemoryDomain friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); } }; +inline ref_ptr operator""_ss(const char* ptr, size_t len) +{ + return StringStorage::create({ptr, (int)len}); +} + class SharedString : public StringView { public: diff --git a/src/unit_tests.cc b/src/unit_tests.cc index 4b8dcbda..504c9246 100644 --- a/src/unit_tests.cc +++ b/src/unit_tests.cc @@ -10,7 +10,7 @@ void test_buffer() { Buffer empty_buffer("empty", Buffer::Flags::None, {}); - Buffer buffer("test", Buffer::Flags::None, { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" }); + Buffer buffer("test", Buffer::Flags::None, { "allo ?\n"_ss, "mais que fais la police\n"_ss, " hein ?\n"_ss, " youpi\n"_ss }); kak_assert(buffer.line_count() == 4); BufferIterator pos = buffer.begin(); @@ -53,7 +53,7 @@ void test_buffer() void test_undo_group_optimizer() { - Vector lines = { "allo ?\n", "mais que fais la police\n", " hein ?\n", " youpi\n" }; + BufferLines lines = { "allo ?\n"_ss, "mais que fais la police\n"_ss, " hein ?\n"_ss, " youpi\n"_ss }; Buffer buffer("test", Buffer::Flags::None, lines); auto pos = buffer.insert(buffer.end(), "kanaky\n"); buffer.erase(pos, buffer.end()); @@ -68,17 +68,17 @@ void test_undo_group_optimizer() kak_assert((int)buffer.line_count() == lines.size()); for (size_t i = 0; i < lines.size(); ++i) - kak_assert(lines[i] == buffer[LineCount((int)i)]); + kak_assert(SharedString{lines[i]} == buffer[LineCount((int)i)]); } void test_word_db() { Buffer buffer("test", Buffer::Flags::None, - { "tchou mutch\n", - "tchou kanaky tchou\n", - "\n", - "tchaa tchaa\n", - "allo\n"}); + { "tchou mutch\n"_ss, + "tchou kanaky tchou\n"_ss, + "\n"_ss, + "tchaa tchaa\n"_ss, + "allo\n"_ss}); WordDB word_db(buffer); auto res = word_db.find_matching("", prefix_match); std::sort(res.begin(), res.end());