namhnguyen / asterixdb

Automatically exported from code.google.com/p/asterixdb
0 stars 0 forks source link

Java Code: can't create a switch() on the ATypeTag.X.serialize() values #858

Closed GoogleCodeExporter closed 9 years ago

GoogleCodeExporter commented 9 years ago
In the Asterix source code, we have an issue of using switch() instead of if() 
on the ATypeTag.X.serialize() values. For example, for the following values, 
when creating a switch statement, Eclipse complains that it is not a constant 
expression and we are forced to use an if() instead of switch(). 

    private static final byte SER_INT32_TYPE_TAG = ATypeTag.INT32.serialize();
    private static final byte SER_STRING_TYPE_TAG = ATypeTag.STRING.serialize();
    private final static byte SER_INT8_TYPE_TAG = ATypeTag.INT8.serialize();
    private final static byte SER_INT16_TYPE_TAG = ATypeTag.INT16.serialize();
    private final static byte SER_INT64_TYPE_TAG = ATypeTag.INT64.serialize();
    private final static byte SER_FLOAT_TYPE_TAG = ATypeTag.FLOAT.serialize();
    private final static byte SER_DOUBLE_TYPE_TAG = ATypeTag.DOUBLE.serialize();

   switch (byte a) {
      case SER_INT_32_TYPE_TAG:
              ...
             break;

      case SER_INT_64_TYPE_TAG:
              ...
             break;
   }

Original issue reported on code.google.com by wangs...@gmail.com on 28 Feb 2015 at 1:55

GoogleCodeExporter commented 9 years ago
One way to address this would be to get the enum for the tag and to switch on 
the enum.

Original comment by westm...@gmail.com on 28 Feb 2015 at 3:30

GoogleCodeExporter commented 9 years ago
Yes. There are some places already does it. But, I'm not sure which way is 
faster (serialize? or deserialize?):  1) a byte to a byte [actual byte value in 
the frame - ATypeTag.X.serialize()]  2) getting enum from a byte 
[EnumDeserializer.ATYPETAGDESERIALIZER.deserialize(byte value) to an enum]. 

Original comment by wangs...@gmail.com on 28 Feb 2015 at 3:36

GoogleCodeExporter commented 9 years ago
There is not need for deserialization. 
To get from a byte to the enum value, we only need to look it up in 
ATypeTag.VALUE_TYPE_MAPPING. 
So the only 2 additional operations (compared to a simple byte comparison) that 
we would do are
a) a cast from byte to an int (as the array index is an int) and
b) the range check when accessing the array.
However, I am not sure how many instructions those really are after the code is 
JIT-compiled.

But I also think that the code is easier to read with a switch than with nested 
ifs.

Original comment by westm...@gmail.com on 28 Feb 2015 at 4:50

GoogleCodeExporter commented 9 years ago
Till, I agree with you. Please check whether I understood your concept 
correctly. The following code is what you expect?

                        ATypeTag argOutTag = ATypeTag.VALUE_TYPE_MAPPING[argOut.getByteArray()[0]];

                        switch (argOutTag) {
                            case INT64:
                                start = (int) LongPointable.getLong(argOut.getByteArray(), 1) - 1;
                            case INT32:
                                start = IntegerPointable.getInteger(argOut.getByteArray(), 1) - 1;
                        }

-- Original

                        private final static byte SER_INT64_TYPE_TAG = ATypeTag.INT64.serialize();
                        private final static byte SER_INT32_TYPE_TAG = ATypeTag.INT32.serialize();

                        if (argOut.getByteArray()[0] == SER_INT64_TYPE_TAG) {
                            start = (int) LongPointable.getLong(argOut.getByteArray(), 1) - 1;
                        } else if (argOut.getByteArray()[0] == SER_INT32_TYPE_TAG) {
                            start = IntegerPointable.getInteger(argOut.getByteArray(), 1) - 1;
                        }

Original comment by wangs...@gmail.com on 28 Feb 2015 at 5:15

GoogleCodeExporter commented 9 years ago
Yes, that's what I thought (with the addition of "break"s in the switch cases).

Original comment by westm...@gmail.com on 28 Feb 2015 at 6:04

GoogleCodeExporter commented 9 years ago
Sure. I will add a break. Let's do it.

Original comment by wangs...@gmail.com on 28 Feb 2015 at 7:10

GoogleCodeExporter commented 9 years ago

Original comment by wangs...@gmail.com on 6 Mar 2015 at 4:43