Skip to content

Commit d1807a5

Browse files
committed
Add ValueType bit mask to improve type check performance
1 parent 04819a3 commit d1807a5

File tree

5 files changed

+54
-213
lines changed

5 files changed

+54
-213
lines changed

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ public static MessageFormat valueOf(final byte b) {
9595
* @return
9696
*/
9797
@VisibleForTesting
98-
static MessageFormat toMessageFormat(final byte b) {
98+
public static MessageFormat toMessageFormat(final byte b) {
9999
if (Code.isPosFixInt(b)) {
100100
return POSFIXINT;
101101
}

msgpack-core/src/main/java/org/msgpack/value/ValueFactory.java

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -103,6 +103,11 @@ public static ImmutableArrayValue newArray(Value[] array) {
103103
return new ImmutableArrayValueImpl(Arrays.copyOf(array, array.length));
104104
}
105105

106+
public static ImmutableArrayValue newArrayOf(Value... elem) {
107+
return newArray(elem);
108+
}
109+
110+
106111
public static ImmutableArrayValue emptyArray() {
107112
return ImmutableArrayValueImpl.empty();
108113
}
@@ -130,6 +135,11 @@ public static ImmutableMapValue newMap(Value[] kvs) {
130135
return new ImmutableMapValueImpl(Arrays.copyOf(kvs, kvs.length));
131136
}
132137

138+
public static ImmutableMapValue emptyMap() {
139+
return ImmutableMapValueImpl.empty();
140+
}
141+
142+
133143
public static class MapEntry {
134144
public final Value key;
135145
public final Value value;

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

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,8 @@
1515
//
1616
package org.msgpack.value;
1717

18+
import org.msgpack.core.MessageFormat;
19+
1820
/**
1921
* MessageTypeFamily is a group of {@link org.msgpack.core.MessageFormat}s
2022
*/
@@ -32,10 +34,30 @@ public enum ValueType {
3234

3335
private final boolean numberType;
3436
private final boolean rawType;
37+
private final int bitMask;
3538

3639
private ValueType(boolean numberType, boolean rawType) {
3740
this.numberType = numberType;
3841
this.rawType = rawType;
42+
this.bitMask = 1 << this.ordinal();
43+
}
44+
45+
/**
46+
* Returns a bit mask representing this value type for quickly cheking
47+
* this value type
48+
* @return bit mask representing this value type
49+
*/
50+
public int getBitMask() {
51+
return bitMask;
52+
}
53+
54+
/**
55+
* Check whether the given bit mask represents this value type
56+
* @param bitMask
57+
* @return
58+
*/
59+
public boolean isTypeOf(int bitMask) {
60+
return (this.bitMask & bitMask) != 0;
3961
}
4062

4163
public boolean isNilType() {
@@ -81,4 +103,12 @@ public boolean isMapType() {
81103
public boolean isExtensionType() {
82104
return this == EXTENSION;
83105
}
106+
107+
public static ValueType valueOf(byte b) {
108+
return MessageFormat.valueOf(b).getValueType();
109+
}
110+
111+
public String toTypeName() {
112+
return this.name().substring(0, 1) + this.name().substring(1).toLowerCase();
113+
}
84114
}

msgpack-core/src/test/scala/org/msgpack/value/CursorTest.scala

Lines changed: 0 additions & 199 deletions
This file was deleted.

msgpack-core/src/test/scala/org/msgpack/value/ValueFactoryTest.scala

Lines changed: 13 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -21,25 +21,25 @@ class ValueFactoryTest extends MessagePackSpec {
2121
isRaw : Boolean = false,
2222
isNumber : Boolean = false
2323
) {
24-
v.isNil shouldBe isNil
25-
v.isBoolean shouldBe isBoolean
26-
v.isInteger shouldBe isInteger
27-
v.isFloat shouldBe isFloat
28-
v.isString shouldBe isString
29-
v.isBinary shouldBe isBinary
30-
v.isArray shouldBe isArray
31-
v.isMap shouldBe isMap
32-
v.isExtension shouldBe isExtension
33-
v.isRaw shouldBe isRaw
34-
v.isNumber shouldBe isNumber
24+
v.isNilValue shouldBe isNil
25+
v.isBooleanValue shouldBe isBoolean
26+
v.isIntegerValue shouldBe isInteger
27+
v.isFloatValue shouldBe isFloat
28+
v.isStringValue shouldBe isString
29+
v.isBinaryValue shouldBe isBinary
30+
v.isArrayValue shouldBe isArray
31+
v.isMapValue shouldBe isMap
32+
v.isExtensionValue shouldBe isExtension
33+
v.isRawValue shouldBe isRaw
34+
v.isNumberValue shouldBe isNumber
3535
}
3636

3737
"ValueFactory" should {
3838

3939
"create valid type values" in {
40-
isValid(ValueFactory.nilValue(), expected=ValueType.NIL, isNil = true)
40+
isValid(ValueFactory.nil(), expected=ValueType.NIL, isNil = true)
4141
forAll{(v:Boolean) => isValid(ValueFactory.newBoolean(v), expected=ValueType.BOOLEAN, isBoolean = true)}
42-
forAll{(v:Int) => isValid(ValueFactory.newInt(v), expected=ValueType.INTEGER, isInteger = true, isNumber = true)}
42+
forAll{(v:Int) => isValid(ValueFactory.newInteger(v), expected=ValueType.INTEGER, isInteger = true, isNumber = true)}
4343
forAll{(v:Float) => isValid(ValueFactory.newFloat(v), expected=ValueType.FLOAT, isFloat = true, isNumber = true)}
4444
forAll{(v:String) => isValid(ValueFactory.newString(v), expected=ValueType.STRING, isString = true, isRaw = true)}
4545
forAll{(v:Array[Byte]) => isValid(ValueFactory.newBinary(v), expected=ValueType.BINARY, isBinary = true, isRaw = true)}

0 commit comments

Comments
 (0)