1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-17 14:11:06 +00:00

Optimize String::get_data, length and is_empty by making better assumptions and inlining.

This commit is contained in:
Lukas Tenbrink
2025-05-30 09:25:21 +02:00
parent b89c47bb85
commit 70672ef008
2 changed files with 8 additions and 22 deletions

View File

@@ -745,11 +745,6 @@ signed char String::filenocasecmp_to(const String &p_str) const {
return naturalnocasecmp_to_base(this_str, that_str); 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 { String String::_separate_compound_words() const {
if (length() == 0) { if (length() == 0) {
return *this; return *this;

View File

@@ -177,7 +177,11 @@ class [[nodiscard]] CharStringT {
public: public:
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); } _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 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<T>() const { return Span(ptr(), length()); } _FORCE_INLINE_ operator Span<T>() const { return Span(ptr(), length()); }
_FORCE_INLINE_ Span<T> span() const { return Span(ptr(), length()); } _FORCE_INLINE_ Span<T> span() const { return Span(ptr(), length()); }
@@ -226,14 +230,6 @@ public:
return *this; 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: protected:
void copy_from(const T *p_cstr) { void copy_from(const T *p_cstr) {
if (!p_cstr) { if (!p_cstr) {
@@ -313,7 +309,11 @@ public:
_FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); } _FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); }
_FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); } _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 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<char32_t>() const { return Span(ptr(), length()); } _FORCE_INLINE_ operator Span<char32_t>() const { return Span(ptr(), length()); }
_FORCE_INLINE_ Span<char32_t> span() const { return Span(ptr(), length()); } _FORCE_INLINE_ Span<char32_t> span() const { return Span(ptr(), length()); }
@@ -377,14 +377,6 @@ public:
signed char filecasecmp_to(const String &p_str) const; signed char filecasecmp_to(const String &p_str) const;
signed char filenocasecmp_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; bool is_valid_string() const;
/* debug, error messages */ /* debug, error messages */
@@ -587,7 +579,6 @@ public:
Vector<uint8_t> sha1_buffer() const; Vector<uint8_t> sha1_buffer() const;
Vector<uint8_t> sha256_buffer() const; Vector<uint8_t> 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 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(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; } _FORCE_INLINE_ bool contains_char(char32_t p_chr) const { return find_char(p_chr) != -1; }