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

Optimize String.similarity by avoiding allocation for bigrams. #100041

Merged
merged 1 commit into from
Dec 10, 2024
Merged
Changes from all commits
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
14 changes: 7 additions & 7 deletions core/string/ustring.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -4002,17 +4002,17 @@ float String::similarity(const String &p_string) const {
return 0.0f;
}

Vector<String> src_bigrams = bigrams();
Vector<String> tgt_bigrams = p_string.bigrams();
const int src_size = length() - 1;
const int tgt_size = p_string.length() - 1;

int src_size = src_bigrams.size();
int tgt_size = tgt_bigrams.size();

int sum = src_size + tgt_size;
const int sum = src_size + tgt_size;
int inter = 0;
for (int i = 0; i < src_size; i++) {
const char32_t i0 = get(i);
const char32_t i1 = get(i + 1);

for (int j = 0; j < tgt_size; j++) {
if (src_bigrams[i] == tgt_bigrams[j]) {
if (i0 == p_string.get(j) && i1 == p_string.get(j + 1)) {
inter++;
break;
}
Expand Down