Skip to content

Commit ff6eacf

Browse files
committed
dont intern SharedStrings but StringDataPtr
1 parent a8eddd0 commit ff6eacf

File tree

4 files changed

+19
-19
lines changed

4 files changed

+19
-19
lines changed

src/buffer.cc

+7-7
Original file line numberDiff line numberDiff line change
@@ -199,9 +199,9 @@ struct Buffer::Modification
199199

200200
Type type;
201201
ByteCoord coord;
202-
SharedString content;
202+
StringDataPtr content;
203203

204-
Modification(Type type, ByteCoord coord, SharedString content)
204+
Modification(Type type, ByteCoord coord, StringDataPtr content)
205205
: type(type), coord(coord), content(std::move(content)) {}
206206

207207
Modification inverse() const
@@ -248,7 +248,7 @@ void Buffer::reload(StringView data, timespec fs_timestamp)
248248
for (LineCount line = 0; line < d.len; ++line)
249249
m_current_undo_group.emplace_back(
250250
Modification::Insert, cur_line + line,
251-
SharedString{parsed_lines.lines[(int)(d.posB + line)]});
251+
parsed_lines.lines[(int)(d.posB + line)]);
252252

253253
m_changes.push_back({ Change::Insert, it == m_lines.end(), cur_line, cur_line + d.len });
254254
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)
261261
for (LineCount line = d.len-1; line >= 0; --line)
262262
m_current_undo_group.emplace_back(
263263
Modification::Erase, cur_line + line,
264-
SharedString{m_lines.get_storage(cur_line + line)});
264+
m_lines.get_storage(cur_line + line));
265265

266266
it = m_lines.erase(it, it + d.len);
267267
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)
433433

434434
void Buffer::apply_modification(const Modification& modification)
435435
{
436-
StringView content = modification.content;
436+
StringView content = modification.content->strview();
437437
ByteCoord coord = modification.coord;
438438

439439
kak_assert(is_valid(coord));
@@ -467,7 +467,7 @@ BufferIterator Buffer::insert(const BufferIterator& pos, StringView content)
467467
if (content.empty())
468468
return pos;
469469

470-
SharedString real_content;
470+
StringDataPtr real_content;
471471
if (pos == end() and content.back() != '\n')
472472
real_content = intern(content + "\n");
473473
else
@@ -478,7 +478,7 @@ BufferIterator Buffer::insert(const BufferIterator& pos, StringView content)
478478
auto coord = pos == end() ? ByteCoord{line_count()} : pos.coord();
479479
if (not (m_flags & Flags::NoUndo))
480480
m_current_undo_group.emplace_back(Modification::Insert, coord, real_content);
481-
return {*this, do_insert(pos.coord(), real_content)};
481+
return {*this, do_insert(pos.coord(), real_content->strview())};
482482
}
483483

484484
BufferIterator Buffer::erase(BufferIterator begin, BufferIterator end)

src/shared_string.cc

+8-8
Original file line numberDiff line numberDiff line change
@@ -4,22 +4,22 @@
44
namespace Kakoune
55
{
66

7-
SharedString StringRegistry::intern(StringView str)
7+
StringDataPtr StringRegistry::intern(StringView str)
88
{
9-
auto it = m_strings.find({str, SharedString::NoCopy{}});
9+
auto it = m_strings.find(str);
1010
if (it == m_strings.end())
1111
{
12-
SharedString shared_str = str;
13-
it = m_strings.emplace(shared_str).first;
12+
auto data = StringData::create(str);
13+
it = m_strings.emplace(data->strview(), data).first;
1414
}
15-
return *it;
15+
return it->second;
1616
}
1717

1818
void StringRegistry::purge_unused()
1919
{
2020
for (auto it = m_strings.begin(); it != m_strings.end(); )
2121
{
22-
if (it->m_storage->refcount == 1)
22+
if (it->second->refcount == 1)
2323
it = m_strings.erase(it);
2424
else
2525
++it;
@@ -34,8 +34,8 @@ void StringRegistry::debug_stats() const
3434
size_t count = m_strings.size();
3535
for (auto& st : m_strings)
3636
{
37-
total_refcount += st.m_storage->refcount - 1;
38-
total_size += (int)st.m_storage->length;
37+
total_refcount += st.second->refcount - 1;
38+
total_size += (int)st.second->length;
3939
}
4040
write_to_debug_buffer(format(" data size: {}, mean: {}", total_size, (float)total_size/count));
4141
write_to_debug_buffer(format(" refcounts: {}, mean: {}", total_refcount, (float)total_refcount/count));

src/shared_string.hh

+3-3
Original file line numberDiff line numberDiff line change
@@ -102,14 +102,14 @@ class StringRegistry : public Singleton<StringRegistry>
102102
{
103103
public:
104104
void debug_stats() const;
105-
SharedString intern(StringView str);
105+
StringDataPtr intern(StringView str);
106106
void purge_unused();
107107

108108
private:
109-
UnorderedSet<SharedString, MemoryDomain::SharedString> m_strings;
109+
UnorderedMap<StringView, StringDataPtr, MemoryDomain::SharedString> m_strings;
110110
};
111111

112-
inline SharedString intern(StringView str)
112+
inline StringDataPtr intern(StringView str)
113113
{
114114
return StringRegistry::instance().intern(str);
115115
}

src/word_db.cc

+1-1
Original file line numberDiff line numberDiff line change
@@ -67,7 +67,7 @@ void WordDB::add_words(const SharedString& line)
6767
{
6868
for (auto& w : get_words(line))
6969
{
70-
WordDB::WordInfo& info = m_words[intern(w)];
70+
WordDB::WordInfo& info = m_words[SharedString{intern(w)}];
7171
++info.refcount;
7272
if (info.letters.none())
7373
info.letters = used_letters(w);

0 commit comments

Comments
 (0)