Skip to content

Commit eb42579

Browse files
committed
Removed the similar table lookup code. Now ValueType uses MessageFormat.valueOf().getValueType() to translate byte codes. #82 (diff)
1 parent fa1a9eb commit eb42579

File tree

8 files changed

+24
-97
lines changed

8 files changed

+24
-97
lines changed

msgpack-core/src/main/java/org/msgpack/core/MessageFormat.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ int skip(MessageUnpacker unpacker) throws IOException{
291291
}
292292
;
293293

294-
private final ValueType family;
294+
private final ValueType valueType;
295295

296296
private MessageFormat(ValueType family) {
297-
this.family = family;
297+
this.valueType = family;
298298
}
299299

300-
public ValueType getTypeFamily() {
301-
return family;
300+
public ValueType getValueType() {
301+
return valueType;
302302
}
303303

304304
/**
@@ -318,10 +318,11 @@ public ValueType getTypeFamily() {
318318
}
319319
}
320320

321-
public static MessageFormat lookUp(final byte b) {
321+
public static MessageFormat valueOf(final byte b) {
322322
return formatTable[table[b & 0xFF]];
323323
}
324324

325+
325326
static MessageFormat toMessageFormat(final byte b) {
326327
if (Code.isPosFixInt(b)) {
327328
return POSFIXINT;

msgpack-core/src/main/java/org/msgpack/core/MessageUnpacker.java

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -158,7 +158,7 @@ private CharsetDecoder getCharsetDecoder() {
158158

159159

160160
private static ValueType getTypeFromHead(final byte b) throws MessageFormatException {
161-
ValueType vt = ValueType.lookUp(b);
161+
ValueType vt = MessageFormat.valueOf(b).getValueType();
162162
if(vt == ValueType.UNKNOWN)
163163
throw new MessageFormatException(String.format("Invalid format code: %02x", b));
164164
return vt;
@@ -169,15 +169,15 @@ public ValueType getNextType() throws IOException {
169169
if(reachedEOF)
170170
return ValueType.EOF;
171171
else
172-
return ValueType.lookUp(b);
172+
return ValueType.valueOf(b);
173173
}
174174

175175
public MessageFormat getNextFormat() throws IOException {
176176
byte b = lookAhead();
177177
if(b == READ_NEXT || reachedEOF)
178178
return MessageFormat.EOF;
179179
else
180-
return MessageFormat.lookUp(b);
180+
return MessageFormat.valueOf(b);
181181
}
182182

183183
/**

msgpack-core/src/main/java/org/msgpack/core/ValueType.java

Lines changed: 2 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -86,81 +86,8 @@ public boolean isExtendedType() {
8686
return this == EXTENDED;
8787
}
8888

89-
90-
static ValueType toValueType(final byte b) {
91-
if (Code.isPosFixInt(b)) { // positive fixint
92-
return ValueType.INTEGER;
93-
}
94-
if (Code.isNegFixInt(b)) { // negative fixint
95-
return ValueType.INTEGER;
96-
}
97-
if (Code.isFixStr(b)) { // fixstr
98-
return ValueType.STRING;
99-
}
100-
if (Code.isFixedArray(b)) { // fixarray
101-
return ValueType.ARRAY;
102-
}
103-
if (Code.isFixedMap(b)) { // fixmap
104-
return ValueType.MAP;
105-
}
106-
switch (b) {
107-
case Code.NIL: // nil
108-
return ValueType.NIL;
109-
case Code.FALSE: // false
110-
case Code.TRUE: // true
111-
return ValueType.BOOLEAN;
112-
case Code.BIN8: // bin 8
113-
case Code.BIN16: // bin 16
114-
case Code.BIN32: // bin 32
115-
return ValueType.BINARY;
116-
case Code.EXT8: // ext 8
117-
case Code.EXT16: // ext 16
118-
case Code.EXT32: // ext 32
119-
return ValueType.EXTENDED;
120-
case Code.FLOAT32: // float 32
121-
case Code.FLOAT64: // float 64
122-
return ValueType.FLOAT;
123-
case Code.UINT8: // unsigned int 8
124-
case Code.UINT16: // unsigned int 16
125-
case Code.UINT32: // unsigned int 32
126-
case Code.UINT64: // unsigned int 64
127-
case Code.INT8: // signed int 8
128-
case Code.INT16: // signed int 16
129-
case Code.INT32: // signed int 32
130-
case Code.INT64: // signed int 64
131-
return ValueType.INTEGER;
132-
case Code.FIXEXT1: // fixext 1
133-
case Code.FIXEXT2: // fixext 2
134-
case Code.FIXEXT4: // fixext 4
135-
case Code.FIXEXT8: // fixext 8
136-
case Code.FIXEXT16: // fixext 16
137-
return ValueType.EXTENDED;
138-
case Code.STR8: // str 8
139-
case Code.STR16: // str 16
140-
case Code.STR32: // str 32
141-
return ValueType.STRING;
142-
case Code.ARRAY16: // array 16
143-
case Code.ARRAY32: // array 32
144-
return ValueType.ARRAY;
145-
case Code.MAP16: // map 16
146-
case Code.MAP32: // map 32
147-
return ValueType.MAP;
148-
default:
149-
return ValueType.UNKNOWN;
150-
}
151-
}
152-
153-
private static byte[] table = new byte[256];
154-
private static ValueType[] symbolTable = ValueType.values();
155-
static {
156-
// Preparing symbol table (byte value -> ValueType ordinal)
157-
for(int b = 0; b <= 0xFF; ++b) {
158-
table[b] = (byte) toValueType((byte) b).ordinal();
159-
}
160-
}
161-
162-
public static ValueType lookUp(final byte b) {
163-
return symbolTable[table[b & 0xFF]];
89+
public static ValueType valueOf(final byte b) {
90+
return MessageFormat.valueOf(b).getValueType();
16491
}
16592

16693

msgpack-core/src/test/scala/org/msgpack/core/MessageFormatTest.scala

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ class MessageFormatTest extends MessagePackSpec {
1717

1818
def checkV(b:Byte, tpe:ValueType) {
1919
try
20-
MessageFormat.lookUp(b).getTypeFamily shouldBe tpe
20+
MessageFormat.valueOf(b).getValueType shouldBe tpe
2121
catch {
2222
case e:TestFailedException =>
2323
error(f"Failure when looking at byte ${b}%02x")
@@ -26,7 +26,7 @@ class MessageFormatTest extends MessagePackSpec {
2626
}
2727

2828
def checkF(b:Byte, f:MessageFormat) {
29-
MessageFormat.lookUp(b) shouldBe f
29+
MessageFormat.valueOf(b) shouldBe f
3030
}
3131

3232
def check(b:Byte, tpe:ValueType, f:MessageFormat) {
@@ -89,13 +89,13 @@ class MessageFormatTest extends MessagePackSpec {
8989

9090
}
9191

92-
"improve the lookUp performance" in {
92+
"improve the valueOf performance" in {
9393

9494
val N = 1000000
9595
val idx = (0 until N).map(x => Random.nextInt(256).toByte).toArray[Byte]
9696

9797
// Initialize
98-
MessageFormat.lookUp(0)
98+
MessageFormat.valueOf(0.toByte)
9999

100100
time("lookup", repeat = 100, logLevel = LogLevel.INFO) {
101101
block("switch") {
@@ -109,7 +109,7 @@ class MessageFormatTest extends MessagePackSpec {
109109
block("table") {
110110
var i = 0
111111
while(i < N) {
112-
MessageFormat.lookUp(idx(i))
112+
MessageFormat.valueOf(idx(i))
113113
i += 1
114114
}
115115
}

msgpack-core/src/test/scala/org/msgpack/core/MessageUnpackerTest.scala

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ class MessageUnpackerTest extends MessagePackSpec {
6060
var f : MessageFormat = null
6161
do {
6262
f = unpacker.getNextFormat()
63-
f.getTypeFamily match {
63+
f.getValueType match {
6464
case ValueType.ARRAY =>
6565
val arrLen = unpacker.unpackArrayHeader()
6666
debug(s"arr size: $arrLen")
@@ -104,7 +104,7 @@ class MessageUnpackerTest extends MessagePackSpec {
104104
var f : MessageFormat = null
105105
do {
106106
f = unpacker.getNextFormat
107-
f.getTypeFamily match {
107+
f.getValueType match {
108108
case ValueType.INTEGER =>
109109
val i = unpacker.unpackInt()
110110
trace(f"read int: $i%,d")

msgpack-core/src/test/scala/org/msgpack/core/ValueTypeTest.scala

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,8 +15,7 @@ class ValueTypeTest extends MessagePackSpec {
1515
"lookup ValueType from a byte value" taggedAs("code") in {
1616

1717
def check(b:Byte, tpe:ValueType) {
18-
ValueType.lookUp(b) shouldBe tpe
19-
ValueType.toValueType(b) shouldBe tpe
18+
ValueType.valueOf(b) shouldBe tpe
2019
}
2120

2221
for(i <- 0 until 0x7f)
@@ -70,15 +69,15 @@ class ValueTypeTest extends MessagePackSpec {
7069
block("switch") {
7170
var i = 0
7271
while(i < N) {
73-
ValueType.toValueType(idx(i))
72+
MessageFormat.toMessageFormat(idx(i)).getValueType()
7473
i += 1
7574
}
7675
}
7776

7877
block("table") {
7978
var i = 0
8079
while(i < N) {
81-
ValueType.lookUp(idx(i))
80+
ValueType.valueOf(idx(i))
8281
i += 1
8382
}
8483
}

msgpack-value/src/main/java/org/msgpack/value/impl/AbstractUnionMutableValue.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -495,7 +495,7 @@ public BigInteger getBigInteger() throws MessageTypeIntegerOverflowException {
495495
// resetToBinaryValue() uses union.setByteBuffer
496496
// encodeString() uses union.setByteBuffer
497497
//
498-
// union.getTypeFamily() == STRING && decodedStringCache != null never happens
498+
// union.getValueType() == STRING && decodedStringCache != null never happens
499499
// because it sets decodedStringCache when it calls union.setString
500500
//
501501

msgpack-value/src/main/java/org/msgpack/value/impl/LazyMutableValue.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -65,15 +65,15 @@ private void lazyRead() throws IOException {
6565
}
6666
6767
@Override
68-
public ValueType getTypeFamily() {
68+
public ValueType getValueType() {
6969
if (!isSet()) {
7070
try {
7171
lazyRead();
7272
} catch (IOException ex) {
7373
throw new RuntimeException(ex); // TODO RuntimeIOException
7474
}
7575
}
76-
return super.getTypeFamily();
76+
return super.getValueType();
7777
}
7878
7979
public void advance() {

0 commit comments

Comments
 (0)