Skip to content

Commit 5921983

Browse files
authored
Improve the performance of jackson-dataformat-msgpack (#866)
* Improve the perf of msgpack-jackson * Refactoring
1 parent 56c0e5a commit 5921983

13 files changed

+946
-490
lines changed

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/ExtensionTypeCustomDeserializers.java

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -35,15 +35,7 @@ public ExtensionTypeCustomDeserializers(ExtensionTypeCustomDeserializers src)
3535

3636
public void addCustomDeser(byte type, final Deser deser)
3737
{
38-
deserTable.put(type, new Deser()
39-
{
40-
@Override
41-
public Object deserialize(byte[] data)
42-
throws IOException
43-
{
44-
return deser.deserialize(data);
45-
}
46-
});
38+
deserTable.put(type, deser);
4739
}
4840

4941
public Deser getDeser(byte type)
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
//
2+
// MessagePack for Java
3+
//
4+
// Licensed under the Apache License, Version 2.0 (the "License");
5+
// you may not use this file except in compliance with the License.
6+
// You may obtain a copy of the License at
7+
//
8+
// http://www.apache.org/licenses/LICENSE-2.0
9+
//
10+
// Unless required by applicable law or agreed to in writing, software
11+
// distributed under the License is distributed on an "AS IS" BASIS,
12+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
// See the License for the specific language governing permissions and
14+
// limitations under the License.
15+
//
16+
package org.msgpack.jackson.dataformat;
17+
18+
import java.lang.reflect.Field;
19+
import java.util.function.Supplier;
20+
21+
public final class JavaInfo
22+
{
23+
static final Supplier<Boolean> STRING_VALUE_FIELD_IS_CHARS;
24+
static {
25+
boolean stringValueFieldIsChars = false;
26+
try {
27+
Field stringValueField = String.class.getDeclaredField("value");
28+
stringValueFieldIsChars = stringValueField.getType() == char[].class;
29+
}
30+
catch (NoSuchFieldException ignored) {
31+
}
32+
if (stringValueFieldIsChars) {
33+
STRING_VALUE_FIELD_IS_CHARS = () -> true;
34+
}
35+
else {
36+
STRING_VALUE_FIELD_IS_CHARS = () -> false;
37+
}
38+
}
39+
40+
private JavaInfo() {}
41+
}

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackExtensionType.java

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
package org.msgpack.jackson.dataformat;
22

33
import com.fasterxml.jackson.core.JsonGenerator;
4-
import com.fasterxml.jackson.core.JsonProcessingException;
54
import com.fasterxml.jackson.databind.JsonSerializer;
65
import com.fasterxml.jackson.databind.SerializerProvider;
76
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
@@ -52,7 +51,7 @@ public boolean equals(Object o)
5251
@Override
5352
public int hashCode()
5453
{
55-
int result = (int) type;
54+
int result = type;
5655
result = 31 * result + Arrays.hashCode(data);
5756
return result;
5857
}
@@ -61,7 +60,7 @@ public static class Serializer extends JsonSerializer<MessagePackExtensionType>
6160
{
6261
@Override
6362
public void serialize(MessagePackExtensionType value, JsonGenerator gen, SerializerProvider serializers)
64-
throws IOException, JsonProcessingException
63+
throws IOException
6564
{
6665
if (gen instanceof MessagePackGenerator) {
6766
MessagePackGenerator msgpackGenerator = (MessagePackGenerator) gen;

msgpack-jackson/src/main/java/org/msgpack/jackson/dataformat/MessagePackFactory.java

Lines changed: 9 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import com.fasterxml.jackson.core.JsonEncoding;
1919
import com.fasterxml.jackson.core.JsonFactory;
2020
import com.fasterxml.jackson.core.JsonGenerator;
21-
import com.fasterxml.jackson.core.JsonParseException;
2221
import com.fasterxml.jackson.core.JsonParser;
2322
import com.fasterxml.jackson.core.io.IOContext;
2423
import org.msgpack.core.MessagePack;
@@ -97,22 +96,21 @@ public JsonGenerator createGenerator(File f, JsonEncoding enc)
9796

9897
@Override
9998
public JsonGenerator createGenerator(Writer w)
100-
throws IOException
10199
{
102100
throw new UnsupportedOperationException();
103101
}
104102

105103
@Override
106104
public JsonParser createParser(byte[] data)
107-
throws IOException, JsonParseException
105+
throws IOException
108106
{
109107
IOContext ioContext = _createContext(data, false);
110108
return _createParser(data, 0, data.length, ioContext);
111109
}
112110

113111
@Override
114112
public JsonParser createParser(InputStream in)
115-
throws IOException, JsonParseException
113+
throws IOException
116114
{
117115
IOContext ioContext = _createContext(in, false);
118116
return _createParser(in, ioContext);
@@ -131,7 +129,7 @@ protected MessagePackParser _createParser(InputStream in, IOContext ctxt)
131129

132130
@Override
133131
protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
134-
throws IOException, JsonParseException
132+
throws IOException
135133
{
136134
if (offset != 0 || len != data.length) {
137135
data = Arrays.copyOfRange(data, offset, offset + len);
@@ -155,12 +153,6 @@ MessagePack.PackerConfig getPackerConfig()
155153
return packerConfig;
156154
}
157155

158-
@VisibleForTesting
159-
boolean isReuseResourceInGenerator()
160-
{
161-
return reuseResourceInGenerator;
162-
}
163-
164156
@VisibleForTesting
165157
boolean isReuseResourceInParser()
166158
{
@@ -178,4 +170,10 @@ public String getFormatName()
178170
{
179171
return "msgpack";
180172
}
173+
174+
@Override
175+
public boolean canHandleBinaryNatively()
176+
{
177+
return true;
178+
}
181179
}

0 commit comments

Comments
 (0)