Skip to content

Commit 70672ef

Browse files
committed
Optimize String::get_data, length and is_empty by making better assumptions and inlining.
1 parent b89c47b commit 70672ef

File tree

2 files changed

+8
-22
lines changed

2 files changed

+8
-22
lines changed

core/string/ustring.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -745,11 +745,6 @@ signed char String::filenocasecmp_to(const String &p_str) const {
745745
return naturalnocasecmp_to_base(this_str, that_str);
746746
}
747747

748-
const char32_t *String::get_data() const {
749-
static const char32_t zero = 0;
750-
return size() ? &operator[](0) : &zero;
751-
}
752-
753748
String String::_separate_compound_words() const {
754749
if (length() == 0) {
755750
return *this;

core/string/ustring.h

Lines changed: 8 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -177,7 +177,11 @@ class [[nodiscard]] CharStringT {
177177
public:
178178
_FORCE_INLINE_ T *ptrw() { return _cowdata.ptrw(); }
179179
_FORCE_INLINE_ const T *ptr() const { return _cowdata.ptr(); }
180+
_FORCE_INLINE_ const T *get_data() const { return ptr() ? ptr() : &_null; }
181+
180182
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
183+
_FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; }
184+
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
181185

182186
_FORCE_INLINE_ operator Span<T>() const { return Span(ptr(), length()); }
183187
_FORCE_INLINE_ Span<T> span() const { return Span(ptr(), length()); }
@@ -226,14 +230,6 @@ class [[nodiscard]] CharStringT {
226230
return *this;
227231
}
228232

229-
_FORCE_INLINE_ int length() const { return size() ? size() - 1 : 0; }
230-
_FORCE_INLINE_ const T *get_data() const {
231-
if (size()) {
232-
return &operator[](0);
233-
}
234-
return &_null;
235-
}
236-
237233
protected:
238234
void copy_from(const T *p_cstr) {
239235
if (!p_cstr) {
@@ -313,7 +309,11 @@ class [[nodiscard]] String {
313309

314310
_FORCE_INLINE_ char32_t *ptrw() { return _cowdata.ptrw(); }
315311
_FORCE_INLINE_ const char32_t *ptr() const { return _cowdata.ptr(); }
312+
_FORCE_INLINE_ const char32_t *get_data() const { return ptr() ? ptr() : &_null; }
313+
316314
_FORCE_INLINE_ int size() const { return _cowdata.size(); }
315+
_FORCE_INLINE_ int length() const { return ptr() ? size() - 1 : 0; }
316+
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
317317

318318
_FORCE_INLINE_ operator Span<char32_t>() const { return Span(ptr(), length()); }
319319
_FORCE_INLINE_ Span<char32_t> span() const { return Span(ptr(), length()); }
@@ -377,14 +377,6 @@ class [[nodiscard]] String {
377377
signed char filecasecmp_to(const String &p_str) const;
378378
signed char filenocasecmp_to(const String &p_str) const;
379379

380-
const char32_t *get_data() const;
381-
/* standard size stuff */
382-
383-
_FORCE_INLINE_ int length() const {
384-
int s = size();
385-
return s ? (s - 1) : 0; // length does not include zero
386-
}
387-
388380
bool is_valid_string() const;
389381

390382
/* debug, error messages */
@@ -587,7 +579,6 @@ class [[nodiscard]] String {
587579
Vector<uint8_t> sha1_buffer() const;
588580
Vector<uint8_t> sha256_buffer() const;
589581

590-
_FORCE_INLINE_ bool is_empty() const { return length() == 0; }
591582
_FORCE_INLINE_ bool contains(const char *p_str) const { return find(p_str) != -1; }
592583
_FORCE_INLINE_ bool contains(const String &p_str) const { return find(p_str) != -1; }
593584
_FORCE_INLINE_ bool contains_char(char32_t p_chr) const { return find_char(p_chr) != -1; }

0 commit comments

Comments
 (0)