5

I'm using a LinkedHashMap<Integer, Integer> to store values of layers on a tile in a 2D game. Higher numbers are drawn over the lower numbers.

In my draw function, I iterate through the value set and draw each one. This means I'm unboxing values (width * height * numLayers) times. I'm planning to port to Android so I want to be as efficient as possible, and I'm thinking this is too much?

The reason I'm using a Map is because the layer number (key) matters: keys above 4 are drawn over players, etc.. So I'll frequently need to skip over a bunch of keys.

I could probably just use an int[10] since I won't need that many layers, but then all of the unused layers would each be taking up 32 bits over nothing, compared to my current HashMap which can have keys 0, 9 and only take up 64 bits.

shruti1810
  • 3,920
  • 2
  • 16
  • 28
Audiocrow
  • 65
  • 5
  • 1
    *Unboxing* is very efficient - all it's doing is getting a number out of an object. It doesn't create a new object or anything. Do you have any evidence that this is actually causing you a problem? – Jon Skeet May 23 '15 at 09:28
  • I'm doing a fair amount of boxing too, since I keep chunks of 2000 tiles loaded at any given time. That's more of a memory issue than a performance issue though. – Audiocrow May 23 '15 at 09:57

1 Answers1

3

Efficient alternative to Map ?

SparseIntArrays is more efficient than HashMap<Integer,Integer>. According to documentation

SparseIntArrays map integers to integers. Unlike a normal array of integers, there can be gaps in the indices. It is intended to be more memory efficient than using a HashMap to map Integers to Integers, both because it avoids auto-boxing keys and values and its data structure doesn't rely on an extra entry object for each mapping. For containers holding up to hundreds of items, the performance difference is not significant, less than 50%.

for more reference click here

For Non-Android languages :

  • Write your own Hash-based map class (not implementing collections.Map). Relatively simple using a 'linear probe' in an array of cells -- the other technique is a linked-list, which (again) will be as large as the 'direct array' option.
  • GNU Trove has primitive maps that will do what you want. But if you are not trying to eke out every byte of memory, I'd second Thomas's suggestion to just use an array.
Kartheek
  • 7,104
  • 3
  • 30
  • 44
  • Thank you for the response, that sounds perfect. This application is for desktop too, will I still be able to use it? – Audiocrow May 23 '15 at 09:40
  • No this class is available in android.util so it only available for android – Kartheek May 23 '15 at 09:42
  • It's no longer perfect since I can't use this class on desktop, but I found the source code and I'll use it as a reference to make my own class. The source is here: https://android.googlesource.com/platform/frameworks/base/+/0b285499db739ba50f2f839d633e763c70e67f96/core/java/android/util/SparseIntArray.java – Audiocrow May 23 '15 at 09:56