Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]


Groups > comp.lang.java.machine > #47

Re: new String ( char[] )

From Andreas Leitgeb <avl@auth.logic.tuwien.ac.at>
Newsgroups comp.lang.java.machine
Subject Re: new String ( char[] )
Date 2014-04-27 16:50 +0000
Organization A noiseless patient Spider
Message-ID <slrnllqdal.bi9.avl@login.logic.tuwien.ac.at> (permalink)
References <s55gl9lqu164g1gufg2mji0q8b2lcj7ac2@4ax.com>

Show all headers | View raw


Roedy Green <see_website@mindprod.com.invalid> wrote:
> For some reason it bothers me deeply that new String ( char[] )
> requires making a copy of the char array and then setting a pointer in
> the String object to it.
>
> Why not just point to the existing char[]?  I don't need it any more. 
>
> The problem is the JVM does not know for sure than that there are no
> other references to that char[].  The referencers would then have
> access to modify the char[] inside an immutable String object, a
> strong no no.

Often these topics are quickly turned down with the killer phrase
  "pre-mature optimization"
but just recently I was able to improve performance by a factor of
five by no longer relying on the magic powers of JIT, but instead
doing some lowlevel optimizations regarding StringBuilder's capacity
and avoiding a lot of toString()'s when putting characters from
various sources into a target StringBuilder.
Maybe, it wasn't pre-mature in my case, although some people claim
that it is always "pre-mature" regardless of the circumstances.

> There must be a way of getting the same effect without that whacking
> huge copy. It might be megabytes long.

Make your own library, that accepts StringBuilders/CharSequences
instead of Strings.  To your own classes add a method toCharSeq()
that will bypass creation of String and its inevitable copying,
if all you needed the String for was having it appened to a StringBuilder.

Also, if you're dealing with long strings and concatenating them, then
its likely to be worth the effort predetermining the total size and
using StringBuilder's ensureCapacity() beforehand, but I guess you 
already do that with your Fast StringBuilder that you mentioned in
comp.lang.java.programmer a while ago.

> Can anyone think of a JVM implementation that could avoid that copy?

I for myself wouldn't bet on that horse.

Back to comp.lang.java.machine | Previous | NextPrevious in thread | Next in thread | Find similar


Thread

new String ( char[] ) Roedy Green <see_website@mindprod.com.invalid> - 2014-04-23 12:42 -0700
  Re: new String ( char[] ) Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-04-27 16:50 +0000
    Re: new String ( char[] ) Roedy Green <see_website@mindprod.com.invalid> - 2014-04-27 12:34 -0700
      Re: new String ( char[] ) Andreas Leitgeb <avl@auth.logic.tuwien.ac.at> - 2014-05-05 08:41 +0000

csiph-web