Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use ICU for text search #15858

Merged
merged 22 commits into from
Aug 24, 2023
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 2 additions & 4 deletions src/buffer/out/Row.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,14 +1034,12 @@ std::wstring_view ROW::GetText(til::CoordType columnBegin, til::CoordType column

til::CoordType ROW::GetLeadingColumnAtCharOffset(const ptrdiff_t offset) const noexcept
{
auto mapper = _createCharToColumnMapper(offset);
return mapper.GetLeadingColumnAt(offset);
return _createCharToColumnMapper(offset).GetLeadingColumnAt(offset);
}

til::CoordType ROW::GetTrailingColumnAtCharOffset(const ptrdiff_t offset) const noexcept
{
auto mapper = _createCharToColumnMapper(offset);
return mapper.GetTrailingColumnAt(offset);
return _createCharToColumnMapper(offset).GetTrailingColumnAt(offset);
}

DelimiterClass ROW::DelimiterClassAt(til::CoordType column, const std::wstring_view& wordDelimiters) const noexcept
Expand Down
90 changes: 48 additions & 42 deletions src/cascadia/TerminalCore/Terminal.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1345,9 +1345,49 @@ void Terminal::_updateUrlDetection()
}
}

// Interns URegularExpression instances so that they can be reused. This method is thread-safe.
static ICU::unique_uregex internURegularExpression(const std::wstring_view& pattern)
struct URegularExpressionInterner
{
// Interns (caches) URegularExpression instances so that they can be reused. This method is thread-safe.
// uregex_open is not terribly expensive at ~10us/op, but it's also much more expensive than uregex_clone
// at ~400ns/op and would effectively double the time it takes to scan the viewport for patterns.
//
// An alternative approach would be to not make this method thread-safe and give each
// Terminal instance its own cache. I'm not sure which approach would've been better.
ICU::unique_uregex Intern(const std::wstring_view& pattern)
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

FYI suppress whitespace changes. The conversion to a class barely changed anything.

{
UErrorCode status = U_ZERO_ERROR;

{
const auto guard = _lock.lock_shared();
if (const auto it = _cache.find(pattern); it != _cache.end())
{
return ICU::unique_uregex{ uregex_clone(it->second.re.get(), &status) };
}
}

// Even if the URegularExpression creation failed, we'll insert it into the cache, because there's no point in retrying.
// (Apart from OOM but in that case this application will crash anyways in 3.. 2.. 1..)
auto re = ICU::CreateRegex(pattern, 0, &status);
ICU::unique_uregex clone{ uregex_clone(re.get(), &status) };
std::wstring key{ pattern };

const auto guard = _lock.lock_exclusive();

_cache.insert_or_assign(std::move(key), CacheValue{ std::move(re), _totalInsertions });
_totalInsertions++;

// If the cache is full remove the oldest element (oldest = lowest generation, just like with humans).
if (_cache.size() > cacheSizeLimit)
{
_cache.erase(std::min_element(_cache.begin(), _cache.end(), [](const auto& it, const auto& smallest) {
return it.second.generation < smallest.second.generation;
}));
}

return clone;
}

private:
struct CacheValue
{
ICU::unique_uregex re;
Expand All @@ -1364,47 +1404,13 @@ static ICU::unique_uregex internURegularExpression(const std::wstring_view& patt
}
};

struct SharedState
{
wil::srwlock lock;
std::unordered_map<std::wstring, CacheValue, CacheKeyHasher, std::equal_to<>> set;
size_t totalInsertions = 0;
};

static SharedState shared;
static constexpr size_t cacheSizeLimit = 128;
wil::srwlock _lock;
std::unordered_map<std::wstring, CacheValue, CacheKeyHasher, std::equal_to<>> _cache;
size_t _totalInsertions = 0;
};

UErrorCode status = U_ZERO_ERROR;

{
const auto guard = shared.lock.lock_shared();
if (const auto it = shared.set.find(pattern); it != shared.set.end())
{
return ICU::unique_uregex{ uregex_clone(it->second.re.get(), &status) };
}
}

// Even if the URegularExpression creation failed, we'll insert it into the cache, because there's no point in retrying.
// (Apart from OOM but in that case this application will crash anyways in 3.. 2.. 1..)
auto re = ICU::CreateRegex(pattern, 0, &status);
ICU::unique_uregex clone{ uregex_clone(re.get(), &status) };
std::wstring key{ pattern };

const auto guard = shared.lock.lock_exclusive();

shared.set.insert_or_assign(std::move(key), CacheValue{ std::move(re), shared.totalInsertions });
shared.totalInsertions++;

// If the cache is full remove the oldest element (oldest = lowest generation, just like with humans).
if (shared.set.size() > cacheSizeLimit)
{
shared.set.erase(std::min_element(shared.set.begin(), shared.set.end(), [](const auto& it, const auto& smallest) {
return it.second.generation < smallest.second.generation;
}));
}

return clone;
}
static URegularExpressionInterner uregexInterner;

PointTree Terminal::_getPatterns(til::CoordType beg, til::CoordType end) const
{
Expand All @@ -1418,7 +1424,7 @@ PointTree Terminal::_getPatterns(til::CoordType beg, til::CoordType end) const

for (size_t i = 0; i < patterns.size(); ++i)
{
const auto re = internURegularExpression(patterns[i]);
const auto re = uregexInterner.Intern(patterns[i]);
uregex_setUText(re.get(), &text, &status);

if (uregex_find(re.get(), -1, &status))
Expand Down