Search code examples
javastringperformancesax

Effective way of creating a String from char[],start,length in Java


We are using Java SAX to parser on really big XML files. Our characters implementation looks like following:

@Override
public void characters(char ch[], int start, int length) throws SAXException {
    String value = String.copyValueOf(ch, start, length);
    ...
}

(ch[] arrays passed by SAX tend to be pretty long)

But we are recently getting some performance issues and the profiler shows us that over 20% of our CPU usage is above invocation of String.copyValueOf (which invoked new String(ch,start,length) under the hood).

Is there any more effective way to obtain a String from array of characters, start index and length than String.copyValueOf(ch, start, length) or new String(ch,start,length)?


Solution

  • Good question, but I'm sure, that answer is no.

    This is because any String object construction uses arrays copy method. It can not be constructed directly on exist array, because String object must be immutable and its internal string array representation is encapsulated from outer changes.

    Furthermore, in your case you have a deal with a fragment of some array. It is impossible to build String object on the fragment of another array in any way.