From 70672ef0085c5e7c448e4e687c857190e66f637b Mon Sep 17 00:00:00 2001 From: Lukas Tenbrink Date: Fri, 30 May 2025 09:25:21 +0200 Subject: [PATCH] Optimize `String::get_data`, `length` and `is_empty` by making better assumptions and inlining. --- core/string/ustring.cpp | 5 ----- core/string/ustring.h | 25 ++++++++----------------- 2 files changed, 8 insertions(+), 22 deletions(-) diff --git a/core/string/ustring.cpp b/core/string/ustring.cpp index 9d3fe51a2ca..d893d93e7db 100644 --- a/core/string/ustring.cpp +++ b/core/string/ustring.cpp @@ -745,11 +745,6 @@ signed char String::filenocasecmp_to(const String &p_str) const { return naturalnocasecmp_to_base(this_str, that_str); } -const char32_t *String::get_data() const { - static const char32_t zero = 0; - return size() ? &operator[](0) : &zero; -} - String String::_separate_compound_words() const { if (length() == 0) { return *this; diff --git a/core/string/ustring.h b/core/string/ustring.h index 88b44578be2..d19fa1b462c 100644 --- a/core/string/ustring.h +++ b/core/string/ustring.h @@ -177,7 +177,11 @@ class [[nodiscard]] CharStringT { public: _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } + _FORCE_INLINE_ const T *get_data() const { return ptr() ? ptr() : &_null; } + _FORCE_INLINE_ int size() const { return _cowdata.size(); } + _FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; } + _FORCE_INLINE_ bool is_empty() const { return length() == 0; } _FORCE_INLINE_ operator Span() const { return Span(ptr(), length()); } _FORCE_INLINE_ Span span() const { return Span(ptr(), length()); } @@ -226,14 +230,6 @@ public: return *this; } - _FORCE_INLINE_ int length() const { return size() ? size() - 1 : 0; } - _FORCE_INLINE_ const T *get_data() const { - if (size()) { - return &operator[](0); - } - return &_null; - } - protected: void copy_from(const T *p_cstr) { if (!p_cstr) { @@ -313,7 +309,11 @@ public: _FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); } + _FORCE_INLINE_ const char32_t *get_data() const { return ptr() ? ptr() : &_null; } + _FORCE_INLINE_ int size() const { return _cowdata.size(); } + _FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; } + _FORCE_INLINE_ bool is_empty() const { return length() == 0; } _FORCE_INLINE_ operator Span() const { return Span(ptr(), length()); } _FORCE_INLINE_ Span span() const { return Span(ptr(), length()); } @@ -377,14 +377,6 @@ public: signed char filecasecmp_to(const String &p_str) const; signed char filenocasecmp_to(const String &p_str) const; - const char32_t *get_data() const; - /* standard size stuff */ - - _FORCE_INLINE_ int length() const { - int s = size(); - return s ? (s - 1) : 0; // length does not include zero - } - bool is_valid_string() const; /* debug, error messages */ @@ -587,7 +579,6 @@ public: Vector sha1_buffer() const; Vector sha256_buffer() const; - _FORCE_INLINE_ bool is_empty() const { return length() == 0; } _FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; } _FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; } _FORCE_INLINE_ bool contains_char(char32_t p_chr) const { return find_char(p_chr) != -1; }