Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!feeder.erje.net!us.feeder.erje.net!newsfeed.straub-nv.de!eternal-september.org!feeder.eternal-september.org!.POSTED!not-for-mail From: markspace Newsgroups: comp.lang.java.programmer Subject: Re: String.substring in JDK 1.7.0_6+ Date: Sat, 02 Feb 2013 15:31:13 -0800 Organization: A noiseless patient Spider Lines: 30 Message-ID: References: <510c0a6a$0$8985$ba4acef3@reader.news.orange.fr> <510c9cea$0$80106$742ec2ed@news.sonic.net> <510d4249$0$80118$742ec2ed@news.sonic.net> <510d972c$0$80186$742ec2ed@news.sonic.net> Mime-Version: 1.0 Content-Type: text/plain; charset=ISO-8859-1; format=flowed Content-Transfer-Encoding: 7bit Injection-Date: Sat, 2 Feb 2013 23:30:57 +0000 (UTC) Injection-Info: mx05.eternal-september.org; posting-host="5aa64ebf9a53573f97c75abba936b0e2"; logging-data="16362"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1+qCm+GHocC6X+LToiurUaQydtwfRqlAQA=" User-Agent: Mozilla/5.0 (Windows NT 6.1; WOW64; rv:17.0) Gecko/20130107 Thunderbird/17.0.2 In-Reply-To: <510d972c$0$80186$742ec2ed@news.sonic.net> Cancel-Lock: sha1:7daMDVL9oddAi95IWrsjnfXuNJU= Xref: csiph.com comp.lang.java.programmer:22028 On 2/2/2013 2:46 PM, Kevin McMurtrie wrote: > Some future JVMs do have JIT tricks to improve String performance. It's > not clear how that would perform or what the side effects would be. One The main think I'd like to see as a "trick" would be to spot when an array is not accessed after a copy, thus negating the need for a copy. String constructor: public String( char[] chars ) { this.buffer = Arrays.copyOf( chars, chars.length ); } Usage: public String someMethod() { char[] myBuff = ... // a local variable return new String( myBuff ); } Spotting that the copy isn't needed because myBuff is local and can't be accessed after the return is one obvious optimization. If this type of analysis is very hard, I can see the original implementation of StringBuilder would be advantageous. OTOH, it doesn't look hard, and I'd bet there's a lot of situations where checking a "copy-on-write bit" is a bigger performance hit.