Is all this Invarient **** er... stuff, premature optimisation?

Sean Kelly sean at invisibleduck.org
Mon Apr 28 18:18:15 PDT 2008


== Quote from Robert Fraser (fraserofthenight at gmail.com)'s article
> Walter Bright wrote:
> > Steven Schveighoffer wrote:
> >> Java's String.substring(start, last) works just like slicing...
> >
> > No it doesn't. It makes a copy (I don't know if this is true of *all*
> > versions of Java).
> Java's 6's string.substring method (JDK 1.6.0_04, 64-bit Windows):
> public String substring(int beginIndex, int endIndex) {
> if (beginIndex < 0) {
> 	throw new StringIndexOutOfBoundsException(beginIndex);
> }
> if (endIndex > count) {
> 	throw new StringIndexOutOfBoundsException(endIndex);
> }
> if (beginIndex > endIndex) {
> 	throw new StringIndexOutOfBoundsException(endIndex -beginIndex);
> }
> return ((beginIndex == 0) && (endIndex == count)) ? this :
> 	new String(offset + beginIndex, endIndex - beginIndex, value);
> }
> The important part is new String(offset + beginIndex, endIndex -
> beginIndex, value) which does indeed do a "slice" of sorts (that is, it
> returns a string with the same char array backing it with a new offset
> and length). No copying of data is done.

Right.  The issue in Java is that the String wrapper class is still allocated
on the heap so DMA is still occurring.  D, on the other hand, uses a fat
reference so creating a slice doesn't touch the heap at all.


Sean



More information about the Digitalmars-d mailing list