[Top][All Lists]
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: An interessting change for shared char[] in String/StringBuffer
From: |
Thomas Zander |
Subject: |
Re: An interessting change for shared char[] in String/StringBuffer |
Date: |
Thu, 8 Apr 2004 15:44:18 +0200 |
User-agent: |
KMail/1.6.1 |
-----BEGIN PGP SIGNED MESSAGE-----
Hash: SHA1
On Thursday 08 April 2004 15:24, Eric Blake wrote:
> Tom Tromey wrote:
> I also wonder if the following implementation would be more efficient.
> In the common case, StringBuffer/StringBuilder is used for appends, and
> then converted to a String just before being discarded. Currently, for
> every append, we adjust the underlying char[] and copy the appended
> String into that array. Would it be better ...
An alternate approach;
some time ago I noticed that the ByteArrayOutputStream copied all data
already present on each add, which is when we came up with the following
approach that would be usefull here as well;
Use a list of byte[] 's (or char[]'s) and optimistically allocate space in
the first array.
At appends copy all the bytes that will fit into the already allocated
byte[], and allocate a second one for the rest. Use some algoritm that
allocates based on previous size (say; 20% of current size) so you will
get increasingly bigger arrays which you copy into.
At the end you will have a list of arrays which you can copy into one big
array on toString() or similar.
We found that this is the most effecient way of accumulating data in one
class with minimum garbage collection / allocations.
Hope thats clear.
- --
Thomas
-----BEGIN PGP SIGNATURE-----
Version: GnuPG v1.2.4 (GNU/Linux)
iD8DBQFAdVc1CojCW6H2z/QRAqsSAKDrtFYdfBLrpSmVgd20zR+uDOespgCgnhBz
Q3bBChRLpbavbuKmP2lePdY=
=x02e
-----END PGP SIGNATURE-----