String::isEmpty() optimization


#1

Hi Julian,

 

My project massively calls the String::isEmpty() function.

It seems that this function can be optimized when the underlying character string is UTF8 or UTF16 .

Because of the complexity of CharPointer_UTF8::operator[] and CharPointer_UTF16::operator[], I replaced the following code of juce_String.h :

 

inline bool isEmpty() const noexcept { return text[0] == 0; }

inline bool isNotEmpty() const noexcept { return text[0] != 0; }

 

by this one:

 

inline bool isEmpty() const noexcept { return text.isEmpty(); }

inline bool isNotEmpty() const noexcept { return !text.isEmpty(); }

 

With my project built with Visual Studio 2010 SP1 in RelWithDebInfo x64 mode,

and running on Windows 7 SP1 64 Pro with Intel Core i7 960 3200Mhz.

For those kind of juce::CharPointer I obtain a performance gain of 33%.

 

Thanks for your concern about this.

 


#2

What a great optimisation! Thanks, I'll certainly change that!

I think the text[0] code must be left behind from the old days when the class contained a raw pointer rather than a CharPointer_UTF8 method. I doubt whether many other people will see as big a performance increase as you did, but every little helps..


#3

I fully agree with you.

Thanks again for your awesome reactivity !