You are viewing a plain text version of this content. The canonical link for it is here.
Posted to users@tomcat.apache.org by Christopher Schultz <ch...@christopherschultz.net> on 2009/05/13 15:04:07 UTC

Re: [OT] Performance with many small requests

-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1

Chuck,

On 5/12/2009 5:03 PM, Caldarale, Charles R wrote:
>> From: David kerber [mailto:dckerber@verizon.net] Subject: Re:
>> Performance with many small requests
>> 
>> When (what java version) did those string operation optimizations 
>> happen?  Sun's web page that talks about this (and explicitly says 
>> that string buffers are usually faster than direct string
>> operations) doesn't mention a specific java version.
> 
> Don't confuse a StringBuffer (the recommended type) with a byte array
> (what Chris was talking about).

Right. People used to write code like this:

String s = ...;
char[] c = s.toCharArray();

for(int i=0; i<c.length; ++i)
   if(c[i] == '\n')
      ...;

Instead of just:

String s = ...;
for(int i=0; i<s.length(); ++i)
  if(s.charAt(i) == '\n')
    ...;

Final method calls got a whole lot quicker at some point (like 1.2 or
1.3) and so the conversion to a char array (or even a byte array) merely
wastes time (for the constructor, memory allocation, etc.) /plus/ the
memory required to duplicate the String's character array. This
"optimization" quickly became a de-optimization.

My position has always been that you should write the code how it ought
to look. Only if it's really causing you performance pain should you
bother to do pinhole optimizations. The JVM authors are more likely to
have an effect on the performance of your code (through optimizations of
things like method calls and monitor lock acquisition) than pinhole
optimizations like the one shown above.

> Since a String object is immutable, one should always use a
> StringBuffer (preferably a StringBuilder, these days) when you are
> constructing strings in a piecemeal fashion, then convert to String
> when complete.

This advice is good when constructing a long string in a loop or across
a long series of statements. If you are just concatenating a bunch of
string together to make a big one in a single statement, go ahead and
use the + operator: the compiler is smart enough to convert the entire
thing into a StringBuilder (a non-synchronized replacement for
StringBuffer) expression that gets good performance without making your
code look like crap.

- -chris
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.4.9 (MingW32)
Comment: Using GnuPG with Mozilla - http://enigmail.mozdev.org/

iEYEARECAAYFAkoKxUcACgkQ9CaO5/Lv0PCo+gCgrlE7HabUgDG+zcba+GFPwZlP
TTEAn2l+hTciWmHGvHH5GSiybnxZfTbi
=nuGH
-----END PGP SIGNATURE-----

---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Performance with many small requests

Posted by Andre-John Mas <aj...@sympatico.ca>.
On 13-May-2009, at 09:16, David kerber wrote:

> Christopher Schultz wrote:
>
> ...
>>> Since a String object is immutable, one should always use a
>>> StringBuffer (preferably a StringBuilder, these days) when you are
>>> constructing strings in a piecemeal fashion, then convert to String
>>> when complete.
>>>
>>
>> This advice is good when constructing a long string in a loop or  
>> across
>> a long series of statements. If you are just concatenating a bunch of
>> string together to make a big one in a single statement, go ahead and
>> use the + operator: the compiler is smart enough to convert the  
>> entire
>> thing into a StringBuilder (a non-synchronized replacement for
>> StringBuffer) expression that gets good performance without making  
>> your
>> code look like crap.
>>
> I was wondering about that.  It certainly seemed like a good place  
> for a smart compiler to fix things up, but didn't know if it  
> actually did or not.  I don't do a lot of that, but enough of it  
> that it becomes a style issue.

If in doubt write a small test case and repeat it for a period of time  
and see which one had the most completions. The one with the most  
completions is likely to be the most optimal.

André-John
---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org


Re: [OT] Performance with many small requests

Posted by David kerber <dc...@verizon.net>.
Christopher Schultz wrote:

...
>> Since a String object is immutable, one should always use a
>> StringBuffer (preferably a StringBuilder, these days) when you are
>> constructing strings in a piecemeal fashion, then convert to String
>> when complete.
>>     
>
> This advice is good when constructing a long string in a loop or across
> a long series of statements. If you are just concatenating a bunch of
> string together to make a big one in a single statement, go ahead and
> use the + operator: the compiler is smart enough to convert the entire
> thing into a StringBuilder (a non-synchronized replacement for
> StringBuffer) expression that gets good performance without making your
> code look like crap.
>   
I was wondering about that.  It certainly seemed like a good place for a 
smart compiler to fix things up, but didn't know if it actually did or 
not.  I don't do a lot of that, but enough of it that it becomes a style 
issue.

D





---------------------------------------------------------------------
To unsubscribe, e-mail: users-unsubscribe@tomcat.apache.org
For additional commands, e-mail: users-help@tomcat.apache.org