Immutable Arrays?

M

Mark Space

Just cruising around the Java API, I found the Segment class:

<http://java.sun.com/javase/6/docs/api/javax/swing/text/Segment.html>


"A segment of a character array representing a fragment of text. It
should be treated as immutable even though the array is directly
accessible. This gives fast access to fragments of text without the
overhead of copying around characters. This is effectively an
unprotected String.

"The Segment implements the java.text.CharacterIterator interface to
support use with the i18n support without copying text into a string. "


Would having an actual immutable array type make this better? It seems
to me that it comes up rather often (not very often, but often enough)
that mutable arrays are a problem.

For example, making of defensive copy of an internal array in an
immutable type. Just returning an immutable array could be a big
performance gain in many instances.

I'm not sure how this could work. Maybe just a trick of the language
that makes assignment a runtime exception.


char [] c1 = new char[10];

// Fill c1...

char [] c2 = Arrays.immutable( c1 );

Now an attempt to write to c2 would generate a runtime error. The
reflection API will need a new type, maybe derived from
java.lang.reflect.Array, for the immutable type.


Any thoughts? I'm I off my rocker here? A lone voice crying in the
wilderness?
 
M

Mark Thornton

Mark said:
Just cruising around the Java API, I found the Segment class:

<http://java.sun.com/javase/6/docs/api/javax/swing/text/Segment.html>


"A segment of a character array representing a fragment of text. It
should be treated as immutable even though the array is directly
accessible. This gives fast access to fragments of text without the
overhead of copying around characters. This is effectively an
unprotected String.

"The Segment implements the java.text.CharacterIterator interface to
support use with the i18n support without copying text into a string. "


Would having an actual immutable array type make this better? It seems
to me that it comes up rather often (not very often, but often enough)
that mutable arrays are a problem.

For example, making of defensive copy of an internal array in an
immutable type. Just returning an immutable array could be a big
performance gain in many instances.

public class ImmutableArray<T> {
private final T[] array;

public static <V> ImmutableArray of(V[] array) {
return new ImmutableArray<V>(array);
}

private ImmutableArray(T[] array) {this.array = array}

public int length() {return array.length;}
public T get(int index) {return array[index];}
}



That is almost as efficient as a built in immutable array type would be.
Add a set of implementations for the primitive types and you are done
without needing any extensions to the language.

Mark Thornton
 
M

Mark Space

Mark said:
That is almost as efficient as a built in immutable array type would be.
Add a set of implementations for the primitive types and you are done
without needing any extensions to the language.

Well I can't pass that to other implementations requiring an array. And
even the language designers saw fit to implement the Segment class with
an array, and then say in the documentation "but please don't change the
array, it's really supposed to be immutable."

Just trying to reason this out, not be argumentative. It seems like an
easy and obvious change in theory. Dunno about practice.
 
M

Mark Thornton

Mark said:
Well I can't pass that to other implementations requiring an array. And
even the language designers saw fit to implement the Segment class with
an array, and then say in the documentation "but please don't change the
array, it's really supposed to be immutable."
Modern JITs are better than they were when the Segment class was
designed, and almost always see through thin facades like my ImmutableArray.
 
M

Mark Thornton

Lew said:
I'll bet it's just as efficient. The JVM is reputed to be very good at
inlining.
The inlining is fine, its the extra casting hidden by generics that
might add a modest extra cost.
 
R

Roedy Green

Would having an actual immutable array type make this better?

You can turn a Collection into an immutable Collection with methods
like Collections.unmodifiableCollection, Collections.unmodifiableList,
Collections.unmodifiableMap, Collections.unmodifiableSet,
Collections.unmodifiableSortedMap or
Collections.unmodifiableSortedSet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,774
Messages
2,569,599
Members
45,174
Latest member
BlissKetoACV
Top