Skip to content

Commit a7052a2

Browse files
committed
Merge pull request godotengine#101033 from Ivorforce/string-count-avoid-copy
Optimize `_count` by replacing a full copy with a CoW copy for the full-string count case.
2 parents 892f77d + b2d881a commit a7052a2

File tree

1 file changed

+2
-4
lines changed

1 file changed

+2
-4
lines changed

core/string/ustring.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3707,8 +3707,7 @@ int String::_count(const String &p_string, int p_from, int p_to, bool p_case_ins
37073707
return 0;
37083708
}
37093709
if (p_from == 0 && p_to == len) {
3710-
str = String();
3711-
str.copy_from_unchecked(&get_data()[0], len);
3710+
str = *this;
37123711
} else {
37133712
str = substr(p_from, p_to - p_from);
37143713
}
@@ -3744,8 +3743,7 @@ int String::_count(const char *p_string, int p_from, int p_to, bool p_case_insen
37443743
return 0;
37453744
}
37463745
if (p_from == 0 && search_limit == source_length) {
3747-
str = String();
3748-
str.copy_from_unchecked(&get_data()[0], source_length);
3746+
str = *this;
37493747
} else {
37503748
str = substr(p_from, search_limit - p_from);
37513749
}

0 commit comments

Comments
 (0)