Avoid temporary strings on buffer load/reload

Pass directly a Vector<ref_ptr<StringStorage>> to the buffer
This commit is contained in:
Maxime Coste 2015-01-22 13:39:29 +00:00
parent 2516c16bb9
commit cb197f57ba
5 changed files with 39 additions and 44 deletions

View File

@ -15,7 +15,7 @@
namespace Kakoune namespace Kakoune
{ {
Buffer::Buffer(String name, Flags flags, Vector<String> lines, Buffer::Buffer(String name, Flags flags, BufferLines lines,
time_t fs_timestamp) time_t fs_timestamp)
: Scope(GlobalScope::instance()), : Scope(GlobalScope::instance()),
m_name(flags & Flags::File ? real_path(parse_filename(name)) : std::move(name)), m_name(flags & Flags::File ? real_path(parse_filename(name)) : std::move(name)),
@ -28,14 +28,13 @@ Buffer::Buffer(String name, Flags flags, Vector<String> lines,
options().register_watcher(*this); options().register_watcher(*this);
if (lines.empty()) if (lines.empty())
lines.emplace_back("\n"); lines.emplace_back(StringStorage::create("\n"));
m_lines.reserve(lines.size());
for (auto& line : lines) for (auto& line : lines)
{ {
kak_assert(not line.empty() and line.back() == '\n'); kak_assert(not line->length == 0 and line->data[line->length-1] == '\n');
m_lines.emplace_back(StringStorage::create(line));
} }
static_cast<BufferLines&>(m_lines) = std::move(lines);
m_changes.push_back({ Change::Insert, {0,0}, line_count(), true }); m_changes.push_back({ Change::Insert, {0,0}, line_count(), true });
@ -157,7 +156,7 @@ struct Buffer::Modification
} }
}; };
void Buffer::reload(Vector<String> 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 }); m_changes.push_back({ Change::Erase, {0,0}, back_coord(), true });
@ -169,20 +168,18 @@ void Buffer::reload(Vector<String> lines, time_t fs_timestamp)
Modification::Erase, line, m_lines[line]); Modification::Erase, line, m_lines[line]);
} }
m_lines.clear();
if (lines.empty()) if (lines.empty())
lines.emplace_back("\n"); lines.emplace_back(StringStorage::create("\n"));
m_lines.reserve(lines.size());
for (auto& line : lines) for (auto& line : lines)
{ {
kak_assert(not line.empty() and line.back() == '\n'); kak_assert(not line->length == 0 and line->data[line->length-1] == '\n');
m_lines.emplace_back(StringStorage::create(line));
if (not (m_flags & Flags::NoUndo)) if (not (m_flags & Flags::NoUndo))
m_current_undo_group.emplace_back( m_current_undo_group.emplace_back(
Modification::Insert, line_count()-1, m_lines.back()); Modification::Insert, line_count()-1, m_lines.back());
} }
static_cast<BufferLines&>(m_lines) = std::move(lines);
commit_undo_group(); commit_undo_group();
m_last_save_undo_index = m_history_cursor - m_history.begin(); m_last_save_undo_index = m_history_cursor - m_history.begin();

View File

@ -59,6 +59,8 @@ private:
ByteCoord m_coord; ByteCoord m_coord;
}; };
using BufferLines = Vector<ref_ptr<StringStorage>, MemoryDomain::BufferContent>;
// A Buffer is a in-memory representation of a file // A Buffer is a in-memory representation of a file
// //
// The Buffer class permits to read and mutate this file // The Buffer class permits to read and mutate this file
@ -76,7 +78,7 @@ public:
NoUndo = 8, NoUndo = 8,
}; };
Buffer(String name, Flags flags, Vector<String> lines = { "\n" }, Buffer(String name, Flags flags, BufferLines lines = {},
time_t fs_timestamp = InvalidTime); time_t fs_timestamp = InvalidTime);
Buffer(const Buffer&) = delete; Buffer(const Buffer&) = delete;
Buffer& operator= (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 run_hook_in_own_context(const String& hook_name, StringView param);
void reload(Vector<String> lines, time_t fs_timestamp = InvalidTime); void reload(BufferLines lines, time_t fs_timestamp = InvalidTime);
void check_invariant() const; void check_invariant() const;
@ -169,16 +171,15 @@ private:
void on_option_changed(const Option& option) override; void on_option_changed(const Option& option) override;
using LineListBase = Vector<ref_ptr<StringStorage>, MemoryDomain::BufferContent>; struct LineList : BufferLines
struct LineList : LineListBase
{ {
[[gnu::always_inline]] [[gnu::always_inline]]
ref_ptr<StringStorage>& get_storage(LineCount line) ref_ptr<StringStorage>& get_storage(LineCount line)
{ return LineListBase::operator[]((int)line); } { return BufferLines::operator[]((int)line); }
[[gnu::always_inline]] [[gnu::always_inline]]
const ref_ptr<StringStorage>& get_storage(LineCount line) const const ref_ptr<StringStorage>& get_storage(LineCount line) const
{ return LineListBase::operator[]((int)line); } { return BufferLines::operator[]((int)line); }
[[gnu::always_inline]] [[gnu::always_inline]]
SharedString get_shared(LineCount line) const SharedString get_shared(LineCount line) const
@ -188,8 +189,8 @@ private:
StringView operator[](LineCount line) const StringView operator[](LineCount line) const
{ return get_storage(line)->strview(); } { return get_storage(line)->strview(); }
StringView front() const { return LineListBase::front()->strview(); } StringView front() const { return BufferLines::front()->strview(); }
StringView back() const { return LineListBase::back()->strview(); } StringView back() const { return BufferLines::back()->strview(); }
}; };
LineList m_lines; LineList m_lines;

View File

@ -39,24 +39,14 @@ Buffer* create_buffer_from_data(StringView data, StringView name,
pos = data.begin() + 3; pos = data.begin() + 3;
} }
Vector<String> lines; BufferLines lines;
while (pos < data.end()) while (pos < data.end())
{ {
const char* line_end = pos; const char* line_end = pos;
while (line_end < data.end() and *line_end != '\r' and *line_end != '\n') while (line_end < data.end() and *line_end != '\r' and *line_end != '\n')
++line_end; ++line_end;
// this should happen only when opening a file which has no lines.emplace_back(StringStorage::create({pos, line_end}, '\n'));
// 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';
if (line_end+1 != data.end() and *line_end == '\r' and *(line_end+1) == '\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) if (buffer)
{ {
buffer->flags() |= Buffer::Flags::NoUndo; buffer->flags() |= Buffer::Flags::NoUndo;
buffer->reload(Vector<String>({"\n"_str}), 0); buffer->reload({"\n"_ss}, 0);
} }
else else
buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo); buffer = new Buffer(std::move(name), Buffer::Flags::Fifo | Buffer::Flags::NoUndo);

View File

@ -17,14 +17,16 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
StringView strview() const { return {data, length}; } 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); void* ptr = StringStorage::operator new(sizeof(StringStorage) + len);
StringStorage* res = reinterpret_cast<StringStorage*>(ptr); StringStorage* res = reinterpret_cast<StringStorage*>(ptr);
memcpy(res->data, str.data(), len); memcpy(res->data, str.data(), (int)str.length());
res->refcount = 0; res->refcount = 0;
res->length = len; res->length = len;
if (back != 0)
res->data[len-1] = back;
res->data[len] = 0; res->data[len] = 0;
return res; return res;
} }
@ -38,6 +40,11 @@ struct StringStorage : UseMemoryDomain<MemoryDomain::SharedString>
friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); } friend void dec_ref_count(StringStorage* s) { if (--s->refcount == 0) StringStorage::destroy(s); }
}; };
inline ref_ptr<StringStorage> operator""_ss(const char* ptr, size_t len)
{
return StringStorage::create({ptr, (int)len});
}
class SharedString : public StringView class SharedString : public StringView
{ {
public: public:

View File

@ -10,7 +10,7 @@ void test_buffer()
{ {
Buffer empty_buffer("empty", Buffer::Flags::None, {}); 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); kak_assert(buffer.line_count() == 4);
BufferIterator pos = buffer.begin(); BufferIterator pos = buffer.begin();
@ -53,7 +53,7 @@ void test_buffer()
void test_undo_group_optimizer() void test_undo_group_optimizer()
{ {
Vector<String> 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); Buffer buffer("test", Buffer::Flags::None, lines);
auto pos = buffer.insert(buffer.end(), "kanaky\n"); auto pos = buffer.insert(buffer.end(), "kanaky\n");
buffer.erase(pos, buffer.end()); buffer.erase(pos, buffer.end());
@ -68,17 +68,17 @@ void test_undo_group_optimizer()
kak_assert((int)buffer.line_count() == lines.size()); kak_assert((int)buffer.line_count() == lines.size());
for (size_t i = 0; i < lines.size(); ++i) 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() void test_word_db()
{ {
Buffer buffer("test", Buffer::Flags::None, Buffer buffer("test", Buffer::Flags::None,
{ "tchou mutch\n", { "tchou mutch\n"_ss,
"tchou kanaky tchou\n", "tchou kanaky tchou\n"_ss,
"\n", "\n"_ss,
"tchaa tchaa\n", "tchaa tchaa\n"_ss,
"allo\n"}); "allo\n"_ss});
WordDB word_db(buffer); WordDB word_db(buffer);
auto res = word_db.find_matching("", prefix_match); auto res = word_db.find_matching("", prefix_match);
std::sort(res.begin(), res.end()); std::sort(res.begin(), res.end());