theigl opened a new pull request #497:
URL: https://github.com/apache/wicket/pull/497


   This PR adds some micro-improvements for `Strings.isEmpty`.
   
   `String.trim` is one of the hottest methods in my application. Most calls 
come from `Strings.isEmpty`, either called directly or via `Args.notEmpty`. It 
is called thousands of times during each request, so even minor improvements 
should make sense.
   
   This PR applies two optimizations:
   
   1. It adds overloaded methods for `Strings.isEmpty` and `Args.notEmpty` that 
take a `String` argument. The compiler seems to better inline the method when 
the exact parameter type is known at compile time. This results in about 5% 
performance improvement in most of my tests.
   2. Before calling `String.trim`, the first character of the string is 
checked for whitespace. If it isn't whitespace, we can avoid calling 
`String.trim`. This optimization makes sense for the new method, but 
*especially* for the existing method with CharSequence parameter. One 
CharSequence frequently passed to `isEmpty` is `AppendingStringBuffer`. Before 
calling `trim` we have to convert it into a string with `toString`. In the case 
of `AppendingStringBuffer`, this *always* allocates a new string with the 
entire contents of the buffer.
   
   ```
   Benchmark                            Mode  Cnt          Score         Error  
Units
   StringsBenchmark.bufferFastIsEmpty  thrpt   15    4706409.686 ±   41865.767  
ops/s
   StringsBenchmark.bufferIsEmpty      thrpt   15    4001127.728 ±   14525.221  
ops/s
   StringsBenchmark.fastIsEmpty        thrpt   15  139232012.995 ± 1757089.132  
ops/s
   StringsBenchmark.isEmpty            thrpt   15  130471674.653 ± 1604850.726  
ops/s
   ```
   
   The new method is about 15% faster for `AppendingStringBuffer` and about 
5-10% faster for `String`.
   
   The only case where these optimization will probably result in minor 
performance loss is for strings that actually start with whitespace. In that 
case we check the first character and then call trim. This shouldn't be a 
problem though because in 99.9% of cases the method is called with a null 
string, an empty string, or a "normal" string.
   
   


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]


Reply via email to