Explorar o código

Merge pull request #102369 from Ivorforce/string-get-data-fast

Optimize `String::get_data` by avoiding a dereference of ptr, and inlining the function.
Thaddeus Crews hai 1 mes
pai
achega
3c43899e24
Modificáronse 2 ficheiros con 8 adicións e 22 borrados
  1. 0 5
      core/string/ustring.cpp
  2. 8 17
      core/string/ustring.h

+ 0 - 5
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);
 	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;

+ 8 - 17
core/string/ustring.h

@@ -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; }