2

Hello fellow Stackoverflowers,

I have the following problem:

I have a situation, where I get an array of primitive numbers as an input. For example int[] or short[] or even byte[]. Now, I need to iterate over the code and do certain stuff, for example, write the numbers into a list. The problem is, however, that every type of number needs a certain list. No problem, I thought, and tried to use generics:

Object dataSet = provider.getDataArray();
Number[] copy = new Number[Array.getLength(dataSet)];
for(int i= 0; i < Array.getLength(dataSet); i++) {
    copy[i] = (T) Array.get(dataSet, i);
}

This works beautifully. However, the problem is with performance. I know that is cannot be circumvented because Reflection and the occuring boxing of the primitives is costly. I am now searching for a pattern to reduce the amount of code, because writing

Object dataSet = provider.getDataArray();
Class<? extends Number> dataType = provider.getDataType();

Number[] copy = new Number[dataSet.length];
if(dataType == Float.class) 
    float[] dataSetAsFloat = (float[]) dataSet;
    for(int i= 0; i < dataSet.length; i++) 
        copySet[i] = dataSetAsFloat[i];
else if (dataType == Double.class)
    double[] dataSetAsDouble = (double[]) dataSet;
    for(int i= 0; i < dataSet.length; i++) 
        copySet[i] = dataSetAsFloat[i];    
....

is a very bloated solution, because the application in the program I'm writing is not as simple as shown here. Basically, I create several hundred lines of extra code because of this performance problem. Is there a solution to this? Perhaps a pattern I'm not aware of, or some really simple trick I'm not seeing?

I would be immensely grateful for a response.

Thanks.

6
  • 1
    There's no help. You can only reduce the code for each case by using System.arraycopy, which, btw, is singificantly faster than the way you do it. Jul 4, 2012 at 18:19
  • The typical solution is code generation from templates. See fastutil or mahout-collections for examples.
    – bmargulies
    Jul 4, 2012 at 18:21
  • Thank you for your answer. The way I'm doing it here is just to show my problem. In reality, I don't have a copySet array I'm copying into, but a complicated data structure that I have no control over. I feared that it might not be possible, but maybe there is still hope?
    – DrEnquinox
    Jul 4, 2012 at 18:24
  • 1
    Java has specific bytecode instructions for every primitive type so your goal is impossible to reach. You need to determine at runtime the type of your data and, based on it, run type-specific code. No way around that. Jul 4, 2012 at 18:27
  • @MarkoTopolnik Thank you. This is exactly as I feared. Well then, I guess there's no helping the bloat.
    – DrEnquinox
    Jul 4, 2012 at 18:29

2 Answers 2

1

Have you considered a strategy pattern that chooses a conversion strategy based on the data type? While it won't reduce much of the overall total code, it will help to modularize it.

public interface ArrayConversionStrategy<T extends Number> {
    T[] convertArray
}

public class FloatConversionStrategy implements ArrayConversionStrategy<Float>
   float[] convertArray(Object[] dataset) {
     float[] dataSetAsFloat = new float[dataset.length];
      for(int i= 0; i < dataSet.length; i++) 
        dataSetAsFloat [i] = dataset[i];
  }
}

public class DoubleConversionStrategy { ... }
public class LongConversionStrategy { ... }

Then in the calling class have a map of data types to strategies

Map<Class<? extends Number>, ArrayConversionStrategy> map;

Object[] dataSet =    provider.getDataArray();
Class<? extends Number> dataType = provider.getDataType();
ArrayConversionStrategy strategy = map.get(dataType)
return strategy.convertArray(dataSet);

Some of my generic syntax may be off here and may have some boxing/autounboxing that may need to be done, but just as a general strategy this may be useful.

0
0

Instead of unpacking the wrapper, you can use a getLong(int)/putLong for integers and getDouble(int)/putDouble for floating point. This will give you two methods which support all primitives types.

interface Array {
    public long getLong(int idx);
    public double getDouble(int idx);
    public void setLong(int idx, long l);
    public void setDouble(int idx, double d);
}

class ByteProvider implements Array {

}

class IntProvider implement Array {

etc.

4
  • First of all, thanks for your answer! This is a solution I tried as well. Sadly it's around 10 times slower than native array access. This is to be expected from reflection, so I don't really care. The problem is, that I need every second in speed I can get but I want to write as clean a code as possible.
    – DrEnquinox
    Jul 4, 2012 at 18:23
  • What I meant was extend provider to support getLong(int) instead of using reflection. The overhead should be a fraction. Jul 4, 2012 at 18:26
  • Good idea! Sadly it's no as simple as that, since I have no control over the provider. I see, I need to be more specific in my next question.
    – DrEnquinox
    Jul 4, 2012 at 18:32
  • Ok. you can wrap the "provider" in this. Jul 4, 2012 at 18:36

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.