Re: Java Indexing- Historical question
- From: Tom Anderson <twic@xxxxxxxxxxxxxxx>
- Date: Wed, 28 Jan 2009 21:37:09 +0000
On Wed, 28 Jan 2009, Mark Thornton wrote:
Tom Anderson wrote:On Tue, 27 Jan 2009, Tom McGlynn wrote:
On Jan 27, 4:49 pm, Mark Thornton <mthorn...@xxxxxxxxxxxx> wrote:Wojtek wrote:Languages that allow indice ranges were born before collections were
easy to use and had good speed (I think...). And trying to retrieve a
Arrays are still a lot faster where the key is a dense range of
integers. The early languages were mostly targeted at applications where
performance was (and remains) critical.
And when the array is of primitives there should be a dramatic benefit in memory usage as well. I often manipulate 16K x 16k or larger images. I think it will be a while before one of these babies is going to fit nicely into a collection. Though I confess I've never actualy tried... Perhaps someday I'll be surprised.
It would be simple enought to write an ArrayMap<T> (or RestrictedRangeIntegerKeyedMap<T> if you prefer) implements Map<Integer, T> that only accepted keys in a given range, and stored its elements in an array. Like EnumMap does. It would be as memory-efficient as an array, and as fast apart from the boxing (which we all hope future JVMs will be better at optimising away).
Boxing of primitives costs memory too.
True. I was really responding to your original comment about situations where the key is a dense range of integers, rather than Mr McGlynn's comment about collections where the values are also primitives. I'm not sure why i responded to his post rather than yours - apology for any confusion.
Anyway, in the integer-key case, there's no memory cost: the boxed integers wouldn't be stored, they'd only exist to get the indices/keys between the calling code and the map innards. They'd live their brief lives and die in eden.
To expand on my description above, i'm thinking of something like:
public class ArrayMap<V> implements SortedMap<Integer, V> {
private int base;
private Object[] values; // because you can't declare a V[]
public ArrayMap(int base, int size) {
this.base = base;
values = new Object[size];
}
public V get(Integer key) {
int index = key - base;
@SuppressWarnings("unchecked")
V value (V)values[index];
return value;
}
}
Used like this:
SortedMap<Integer, Foo> foosByYear = new ArrayMap<Foo>(2005, 10);
for (int year: foosByYear.keySet()) {
process(year, foosByYear.get(year);
}
You'd actually write that as a loop over the entrySet, but that serves to illustrate the general use pattern.
If the run-time compiler could work out that the map was definitely an ArrayMap, i think the key operations can all be reduced to some very simple and efficient code. Hopefully.
The ability to eliminate the boxing involved in Integer[] vs int[] is still a long way off
Perhaps. It would certainly be needed for the array-of-pixels type situation.
(and may require a change in the language so that new Integer.valueOf(x) == Integer.valueOf(x) for all x).
Escape analysis means you don't need that to be able to do deboxing in at least some cases, but yes, a change along those lines might help make it more widely possible.
tom
--
It involves police, bailiffs, vampires and a portal to hell under a
tower block in Hackney.
.
- Follow-Ups:
- Re: Java Indexing- Historical question
- From: Mark Thornton
- Re: Java Indexing- Historical question
- References:
- Java Indexing- Historical question
- From: Gilbert
- Re: Java Indexing- Historical question
- From: Ben Kaufman
- Re: Java Indexing- Historical question
- From: Lew
- Re: Java Indexing- Historical question
- From: Tom McGlynn
- Re: Java Indexing- Historical question
- From: Martin Gregorie
- Re: Java Indexing- Historical question
- From: Eric Sosman
- Re: Java Indexing- Historical question
- From: Lew
- Re: Java Indexing- Historical question
- From: Patricia Shanahan
- Re: Java Indexing- Historical question
- From: Wojtek
- Re: Java Indexing- Historical question
- From: Patricia Shanahan
- Re: Java Indexing- Historical question
- From: Wojtek
- Re: Java Indexing- Historical question
- From: Mark Thornton
- Re: Java Indexing- Historical question
- From: Tom McGlynn
- Re: Java Indexing- Historical question
- From: Tom Anderson
- Re: Java Indexing- Historical question
- From: Mark Thornton
- Java Indexing- Historical question
- Prev by Date: Re: setting the CPU affinity for a whole JVM on Linux
- Next by Date: Re: setting the CPU affinity for a whole JVM on Linux
- Previous by thread: Re: Java Indexing- Historical question
- Next by thread: Re: Java Indexing- Historical question
- Index(es):
Relevant Pages
|
Loading