From b2d881a73a369e361482719fd4f7d8f1da602ad9 Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Thu, 2 Jan 2025 18:16:06 +0100 Subject: [PATCH] Optimize `_count` by replacing a full copy with a CoW copy for the full-string count case. --- core/string/ustring.cpp | 6 ++---- 1 file changed, 2 insertions(+), 4 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index be50b0b4e91d..48d900d19694 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -3707,8 +3707,7 @@ int String::_count(const String &p_string, int p_from, int p_to, bool p_case_ins return 0; } if (p_from == 0 && p_to == len) { - str = String(); - str.copy_from_unchecked(&get_data()[0], len); + str = *this; } else { str = substr(p_from, p_to - p_from); } @@ -3744,8 +3743,7 @@ int String::_count(const char *p_string, int p_from, int p_to, bool p_case_insen return 0; } if (p_from == 0 && search_limit == source_length) { - str = String(); - str.copy_from_unchecked(&get_data()[0], source_length); + str = *this; } else { str = substr(p_from, search_limit - p_from); }