Skip to content

Commit 263ef0b

Browse files
committed
Get rid of SharedString
1 parent 94cbd5a commit 263ef0b

File tree

3 files changed

+23
-57
lines changed

3 files changed

+23
-57
lines changed

src/shared_string.hh

-42
Original file line numberDiff line numberDiff line change
@@ -56,48 +56,6 @@ struct StringData : UseMemoryDomain<MemoryDomain::SharedString>
5656

5757
using StringDataPtr = RefPtr<StringData>;
5858

59-
class SharedString : public StringView
60-
{
61-
public:
62-
SharedString() = default;
63-
SharedString(StringView str)
64-
{
65-
if (not str.empty())
66-
{
67-
m_storage = StringData::create(str);
68-
StringView::operator=(m_storage->strview());
69-
}
70-
}
71-
struct NoCopy{};
72-
SharedString(StringView str, NoCopy) : StringView(str) {}
73-
74-
SharedString(const char* str) : SharedString(StringView{str}) {}
75-
76-
SharedString acquire_substr(ByteCount from, ByteCount length = INT_MAX) const
77-
{
78-
return SharedString{StringView::substr(from, length), m_storage};
79-
}
80-
SharedString acquire_substr(CharCount from, CharCount length = INT_MAX) const
81-
{
82-
return SharedString{StringView::substr(from, length), m_storage};
83-
}
84-
85-
explicit SharedString(StringDataPtr storage)
86-
: StringView{storage->strview()}, m_storage(std::move(storage)) {}
87-
88-
friend size_t hash_value(const SharedString& str)
89-
{
90-
return str.m_storage ? str.m_storage->hash : hash_data(str.data(), (int)str.length());
91-
}
92-
93-
private:
94-
SharedString(StringView str, StringDataPtr storage)
95-
: StringView{str}, m_storage(std::move(storage)) {}
96-
97-
friend class StringRegistry;
98-
StringDataPtr m_storage;
99-
};
100-
10159
class StringRegistry : public Singleton<StringRegistry>
10260
{
10361
public:

src/word_db.cc

+19-12
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ UsedLetters to_lower(UsedLetters letters)
3737
using WordList = Vector<StringView>;
3838

3939

40-
static WordList get_words(const SharedString& content)
40+
static WordList get_words(StringView content)
4141
{
4242
WordList res;
4343
using Utf8It = utf8::iterator<const char*, utf8::InvalidPolicy::Pass>;
@@ -56,29 +56,36 @@ static WordList get_words(const SharedString& content)
5656
{
5757
const ByteCount start = word_start - content.begin();
5858
const ByteCount length = it.base() - word_start;
59-
res.push_back(content.acquire_substr(start, length));
59+
res.push_back(content.substr(start, length));
6060
in_word = false;
6161
}
6262
}
6363
return res;
6464
}
6565

66-
void WordDB::add_words(const SharedString& line)
66+
void WordDB::add_words(StringView line)
6767
{
6868
for (auto& w : get_words(line))
6969
{
70-
WordDB::WordInfo& info = m_words[SharedString{intern(w)}];
71-
++info.refcount;
72-
if (info.letters.none())
70+
auto it = m_words.find(w);
71+
if (it == m_words.end())
72+
{
73+
auto word = intern(w);
74+
WordDB::WordInfo& info = m_words[word->strview()];
75+
info.word = word;
7376
info.letters = used_letters(w);
77+
++info.refcount;
78+
}
79+
else
80+
++ it->second.refcount;
7481
}
7582
}
7683

77-
void WordDB::remove_words(const SharedString& line)
84+
void WordDB::remove_words(StringView line)
7885
{
7986
for (auto& w : get_words(line))
8087
{
81-
auto it = m_words.find({w, SharedString::NoCopy()});
88+
auto it = m_words.find(w);
8289
kak_assert(it != m_words.end() and it->second.refcount > 0);
8390
if (--it->second.refcount == 0)
8491
m_words.erase(it);
@@ -92,7 +99,7 @@ WordDB::WordDB(const Buffer& buffer)
9299
for (auto line = 0_line, end = buffer.line_count(); line < end; ++line)
93100
{
94101
m_lines.push_back(buffer.line_storage(line));
95-
add_words(SharedString{m_lines.back()});
102+
add_words(m_lines.back()->strview());
96103
}
97104
}
98105

@@ -123,13 +130,13 @@ void WordDB::update_db()
123130
while (old_line < modif.old_line + modif.num_removed)
124131
{
125132
kak_assert(old_line < m_lines.size());
126-
remove_words(SharedString{m_lines[(int)old_line++]});
133+
remove_words(m_lines[(int)old_line++]->strview());
127134
}
128135

129136
for (auto l = 0_line; l < modif.num_added; ++l)
130137
{
131138
new_lines.push_back(buffer.line_storage(modif.new_line + l));
132-
add_words(SharedString{new_lines.back()});
139+
add_words(new_lines.back()->strview());
133140
}
134141
}
135142
while (old_line != (int)m_lines.size())
@@ -140,7 +147,7 @@ void WordDB::update_db()
140147

141148
int WordDB::get_word_occurences(StringView word) const
142149
{
143-
auto it = m_words.find({word, SharedString::NoCopy()});
150+
auto it = m_words.find(word);
144151
if (it != m_words.end())
145152
return it->second.refcount;
146153
return 0;

src/word_db.hh

+4-3
Original file line numberDiff line numberDiff line change
@@ -30,15 +30,16 @@ public:
3030
int get_word_occurences(StringView word) const;
3131
private:
3232
void update_db();
33-
void add_words(const SharedString& line);
34-
void remove_words(const SharedString& line);
33+
void add_words(StringView line);
34+
void remove_words(StringView line);
3535

3636
struct WordInfo
3737
{
38+
StringDataPtr word;
3839
UsedLetters letters;
3940
int refcount;
4041
};
41-
using WordToInfo = UnorderedMap<SharedString, WordInfo, MemoryDomain::WordDB>;
42+
using WordToInfo = UnorderedMap<StringView, WordInfo, MemoryDomain::WordDB>;
4243
using Lines = Vector<StringDataPtr, MemoryDomain::WordDB>;
4344

4445
SafePtr<const Buffer> m_buffer;

0 commit comments

Comments
 (0)