V
Senior Newbie 
|
 |
«
Posted
2006-01-14 11:00:14 » |
|
hey there  The game I'm working on heavily depends on non-incremental numbers associated with objects (for many different purposes), and I can't find a good type of collection for this. ArrayList, Vector and LinkedList all depend on incremental indices, all other collections associate Objects with Objects. Right now I'm using HashMap<Integer, Object>, but I guess that's far from being the optimal solution. Is there any int -> Object collection implementation which I simply overlooked? If that's not the case, is HashMap<Integer, Object> a good enough solution or should I write my own collection for this purpose?
|
|
|
|
|
Riven
|
 |
«
Reply #1 - Posted
2006-01-14 12:00:15 » |
|
You might be interested in the IntHashMap class in one of the Apache projects.
org.apache.commons.lang.IntHashMap
|
|
|
|
krausest
Junior Member  
I love YaBB 1G - SP1!
|
 |
«
Reply #2 - Posted
2006-01-14 13:23:51 » |
|
Or you might take a look at the pcj ( http://pcj.sourceforge.net/ ) library and the IntKeyMap subclasses, which offer what you need. Alternatively the apache commons primitive ( http://jakarta.apache.org/commons/primitives/) might help. But I found that it's always lacking the specific collection I needed (in your case there seems to be no HashMap). @Riven: I couldn't find IntHashMap in the current apache.commons.lang API docs. Can you post a link?
|
|
|
|
|
Games published by our own members! Check 'em out!
|
|
tom
|
 |
«
Reply #3 - Posted
2006-01-14 13:34:40 » |
|
I think HashMap<Integer, Object> is good enough. I think HashMap use Object.hashCode() as its key internally. The Integer class just returns its integer value in its hashCode() function. So you pretty much got exactly what you want. Although there is the overhead of wrapping the int in an Integer.
|
|
|
|
V
Senior Newbie 
|
 |
«
Reply #4 - Posted
2006-01-15 03:46:22 » |
|
http://trove4j.sourceforge.net/updated in 2005, while the pcj's last update was 2003 (prior to java 1.5). thank you very much for your help 
|
|
|
|
|
kappa
|
 |
«
Reply #5 - Posted
2006-01-15 23:53:05 » |
|
from what i've seen ArrayLists are pretty fast for general stuff and random access. However LinkedLists are a little faster when having a problem that require you to scroll through the list from begining to end. You may also want to look at FastList at http://javolution.org/
|
|
|
|
|
V
Senior Newbie 
|
 |
«
Reply #6 - Posted
2006-01-29 08:34:43 » |
|
As I said in my first post, I can't use arraylist or the like because the indices aren't incremental. anyways, thanks for the javolution link, I didn't know about that before and it seems pretty nifty  that whole page seems to be an advertisement for those libs, though. any experiences? o_O
|
|
|
|
|
Jeff
|
 |
«
Reply #7 - Posted
2006-01-29 23:41:16 » |
|
Use Map.
Yes, thats an interface not a class.
The right thing to do whenevr using collections is to design your code to use the interfaces. Then you can tune later as needed by swaping different implementations (eg HashMap, TreeMap, etc) or even writing your own.
Rule #1 of writing fast code: First write clean, clear and well encapsulated code. Second profile. Thrid go back and fix bottlenecks.
|
|
|
|
V
Senior Newbie 
|
 |
«
Reply #8 - Posted
2006-01-30 02:49:50 » |
|
Yeash, but I couldn't find an interface for which there was any implementation for int->object association, that's why I asked in the first place 
|
|
|
|
|
Jeff
|
 |
«
Reply #9 - Posted
2006-01-30 03:44:06 » |
|
Map<Integer,Object>.
Map associates any two objects
With Java5 autoboxing you can treat int like an object if you so desire.
|
|
|
|
Games published by our own members! Check 'em out!
|
|
V
Senior Newbie 
|
 |
«
Reply #10 - Posted
2006-01-30 14:35:51 » |
|
I know, but still using a direct int->object association is faster (saves one boxing and two hash calls per get, and I need tons of gets) than using a boxed int, that's why I asked if there was any specific implementation for that.
|
|
|
|
|
Riven
|
 |
«
Reply #11 - Posted
2006-01-30 14:38:18 » |
|
Again: IntHashMap from Jakarta Commons Here is the java2html page...
|
|
|
|
V
Senior Newbie 
|
 |
«
Reply #12 - Posted
2006-01-30 15:16:18 » |
|
I know, I just told Jeff why I asked for this in the first place. >_<
|
|
|
|
|
Riven
|
 |
«
Reply #13 - Posted
2006-01-30 15:32:28 » |
|
Err, sorry, I confused krausest's response with yours.
|
|
|
|
Jeff
|
 |
«
Reply #14 - Posted
2006-02-02 01:53:08 » |
|
I know, but still using a direct int->object association is faster (saves one boxing and two hash calls per get, and I need tons of gets) than using a boxed int, that's why I asked if there was any specific implementation for that.
Thats *really* your bottleneck? You've proved this with a profiler?? I would be astonished if the rest of your code was so beautifully tight that this was significant.
|
|
|
|
Riven
|
 |
«
Reply #15 - Posted
2006-02-02 10:11:35 » |
|
I would be astonished if the rest of your code was so beautifully tight that this was significant.
The rest of the code normally doesn't matter... only a few lines will be performance-critical. It's very easy to create a bottleneck in accessing Maps with autoboxed ints in tight loops. Very easy. I've done a lot of pathfinding-stuff where the difference between HashMap<Integer, Object> and IntHashMap was significantly astonishing 
|
|
|
|
rreyelts
Junior Member  
There is nothing Nu under the sun
|
 |
«
Reply #16 - Posted
2006-02-02 16:49:10 » |
|
Thats *really* your bottleneck? You've proved this with a profiler?? I would be astonished if the rest of your code was so beautifully tight that this was significant.
I wrote a spatial index where autoboxing was, at one point in time, a bottleneck in the index. (About ~50 million objects in the index, with query times running in the low milliseconds). I ended up creating my own special "GeometrySet" interface which enabled me to avoid most boxing/autoboxing at the expense of more complexity. Of course, I wouldn't randomly go around getting rid of all boxing, but it can be a bottleneck in tight loops that deal with _many_ objects.
|
|
|
|
princec
|
 |
«
Reply #17 - Posted
2006-02-02 18:54:24 » |
|
50million objects = 1.5 gigabytees of object overhead and unnecessary garbage collection trawling too... autoboxing bad! Evil! Cas 
|
|
|
|
Riven
|
 |
«
Reply #18 - Posted
2006-02-02 19:06:25 » |
|
30 byte per object?!  Really?
|
|
|
|
Raghar
Junior Member  
Ue ni taete 'ru hitomi ni kono mi wa dou utsuru
|
 |
«
Reply #19 - Posted
2006-02-02 19:30:45 » |
|
You talk about minimum 2-3+ pointers + 1-0 identifier. One pointer is on 32 bit computer 4 bytes, it's 8 bytes on 64 bit CPU. You could look at my old post about my memory problems (with something like patricia trie). 12 - 24 bytes + some aditional object info looks like real object overhead. Test it.
Dipper is superior to any autoboxing (with exception of computationally expensive types), and it alows to use rather nice (and FAST) abstractions with sliding window.
|
|
|
|
|
princec
|
 |
«
Reply #20 - Posted
2006-02-02 19:45:11 » |
|
30 byte per object?!  Really? Actually that might have been a little OTT  But it would not be unreasonable to implement a VM that allocated objects on 32 byte boundaries... Cas 
|
|
|
|
V
Senior Newbie 
|
 |
«
Reply #21 - Posted
2006-02-03 00:25:08 » |
|
I never said this was my bottleneck, it's just that there are about 30000 values stored in hashmaps out of which about ~1000 need to be called per tick (1/60 second) or more, so I thought there might be a better way than boxing. It's not critical at all, but why not optimize it  My bottleneck is - surprise, surprise - java2d 
|
|
|
|
|
Mr_Light
|
 |
«
Reply #22 - Posted
2006-02-03 01:58:14 » |
|
if your asking yourself why not make it efficient, the aswer should be yes. if you truely wondering about optimising it, the answer should be no, not until your have profiled your app.
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
V
Senior Newbie 
|
 |
«
Reply #23 - Posted
2006-02-03 17:17:40 » |
|
when did I ever say I didn't profile it?? I even posted that I know what my bottleneck is in my very last post. >_<
|
|
|
|
|
Mr_Light
|
 |
«
Reply #24 - Posted
2006-02-03 17:56:00 » |
|
ok I was beeing dense then.  but even if you are profiling you pass my assertion, so no worries 
|
It's harder to read code than to write it. - it's even harder to write readable code.
The gospel of brother Riven: "The guarantee that all bugs are in *your* code is worth gold." Amen brother a-m-e-n.
|
|
|
Jeff
|
 |
«
Reply #25 - Posted
2006-02-04 07:02:35 » |
|
I never said this was my bottleneck, it's just that there are about 30000 values stored in hashmaps out of which about ~1000 need to be called per tick (1/60 second) or more, so I thought there might be a better way than boxing. It's not critical at all, but why not optimize it  Because, as the Great Abrash puts it.... "Things that don't matter don't matter." So why waste time and energy on them.
|
|
|
|
|