1

I have a piece of code which is basically as followed:

long[]   ids;
long[][] values;

The values are filled out of turn i.e. if ids = ['id1','id2',...] the values maybe be
values = [['id2val1','id2val2',..]['id1val1','id2val2',...],..]
The out of turn execution cannot be avoided, the approach I am considering is to use a Map Map<Long,Long[]>,
but due to auto boxing of java it is not possible use the JAVA generics.

I would like to know if it can be done via some optimised data structure library that uses primitives,
to avoid the unnecessary auto boxing, I am looking at libraries like Koloboke & Fastutil.
Looking for a data structure recommendation

gourab ghosh
  • 159
  • 6
  • Please provide some code to explain what _due to auto boxing of java it is not possible use the JAVA generics_ means. – Andrew S Dec 21 '20 at 13:12
  • @AndrewS - The current code stores time series data. so the `id[]` contains list of unique identifiers. For each of those there is an associated long array. Currently it is done via two individual arrays, i.e. ids[],values[][]. However now the results can return out of order so need a way to correlate and access id with value[]. This if done with `Map` takes more time/ processing due to unboxing boxing from long to Long and reverse. So need an optimised data structure, does that clarify the doubt ? – gourab ghosh Dec 21 '20 at 13:26
  • Possible duplicate of https://stackoverflow.com/questions/41726286/map-alternative-for-primitive-values – stridecolossus Dec 21 '20 at 13:50
  • Do you know that you _need_ to optimise this? i.e. is this bottle-necking your software? How large are those data structures? – stridecolossus Dec 21 '20 at 13:52
  • @stridecolossus - provided the answers inline Do you know that you need to optimise this? Yes this needs to be as optimised as possible. Is this bottle-necking your software? Currently, this is not in use, however this is part of an effort to increase scale. It would definitely be considered as a bottle-neck How large are those data structures ? Currently average size of the long[][] of values is [1000000][2000] – gourab ghosh Dec 22 '20 at 05:19

1 Answers1

2

While you have to use boxed Long as the map key, you do not need to box the primitives in value arrays. You can use Map<Long,long[]> rather than Map<Long,Long[]>. This should alleviate most of your performance concerns.

So, before you start introducing third party libraries, see if simple HashMap<Long,long[]> will be good enough for your purposes.

Misha
  • 27,433
  • 6
  • 62
  • 78