Skip to content

Commit

Permalink
Improve the performance of jackson-dataformat-msgpack (#866)
Browse files Browse the repository at this point in the history
* Improve the perf of msgpack-jackson

* Refactoring
  • Loading branch information
komamitsu authored Jan 5, 2025
1 parent 56c0e5a commit 5921983
Show file tree
Hide file tree
Showing 13 changed files with 946 additions and 490 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -35,15 +35,7 @@ public ExtensionTypeCustomDeserializers(ExtensionTypeCustomDeserializers src)

public void addCustomDeser(byte type, final Deser deser)
{
deserTable.put(type, new Deser()
{
@Override
public Object deserialize(byte[] data)
throws IOException
{
return deser.deserialize(data);
}
});
deserTable.put(type, deser);
}

public Deser getDeser(byte type)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
//
// MessagePack for Java
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//
package org.msgpack.jackson.dataformat;

import java.lang.reflect.Field;
import java.util.function.Supplier;

public final class JavaInfo
{
static final Supplier<Boolean> STRING_VALUE_FIELD_IS_CHARS;
static {
boolean stringValueFieldIsChars = false;
try {
Field stringValueField = String.class.getDeclaredField("value");
stringValueFieldIsChars = stringValueField.getType() == char[].class;
}
catch (NoSuchFieldException ignored) {
}
if (stringValueFieldIsChars) {
STRING_VALUE_FIELD_IS_CHARS = () -> true;
}
else {
STRING_VALUE_FIELD_IS_CHARS = () -> false;
}
}

private JavaInfo() {}
}
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
package org.msgpack.jackson.dataformat;

import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonSerializer;
import com.fasterxml.jackson.databind.SerializerProvider;
import com.fasterxml.jackson.databind.annotation.JsonSerialize;
Expand Down Expand Up @@ -52,7 +51,7 @@ public boolean equals(Object o)
@Override
public int hashCode()
{
int result = (int) type;
int result = type;
result = 31 * result + Arrays.hashCode(data);
return result;
}
Expand All @@ -61,7 +60,7 @@ public static class Serializer extends JsonSerializer<MessagePackExtensionType>
{
@Override
public void serialize(MessagePackExtensionType value, JsonGenerator gen, SerializerProvider serializers)
throws IOException, JsonProcessingException
throws IOException
{
if (gen instanceof MessagePackGenerator) {
MessagePackGenerator msgpackGenerator = (MessagePackGenerator) gen;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,6 @@
import com.fasterxml.jackson.core.JsonEncoding;
import com.fasterxml.jackson.core.JsonFactory;
import com.fasterxml.jackson.core.JsonGenerator;
import com.fasterxml.jackson.core.JsonParseException;
import com.fasterxml.jackson.core.JsonParser;
import com.fasterxml.jackson.core.io.IOContext;
import org.msgpack.core.MessagePack;
Expand Down Expand Up @@ -97,22 +96,21 @@ public JsonGenerator createGenerator(File f, JsonEncoding enc)

@Override
public JsonGenerator createGenerator(Writer w)
throws IOException
{
throw new UnsupportedOperationException();
}

@Override
public JsonParser createParser(byte[] data)
throws IOException, JsonParseException
throws IOException
{
IOContext ioContext = _createContext(data, false);
return _createParser(data, 0, data.length, ioContext);
}

@Override
public JsonParser createParser(InputStream in)
throws IOException, JsonParseException
throws IOException
{
IOContext ioContext = _createContext(in, false);
return _createParser(in, ioContext);
Expand All @@ -131,7 +129,7 @@ protected MessagePackParser _createParser(InputStream in, IOContext ctxt)

@Override
protected JsonParser _createParser(byte[] data, int offset, int len, IOContext ctxt)
throws IOException, JsonParseException
throws IOException
{
if (offset != 0 || len != data.length) {
data = Arrays.copyOfRange(data, offset, offset + len);
Expand All @@ -155,12 +153,6 @@ MessagePack.PackerConfig getPackerConfig()
return packerConfig;
}

@VisibleForTesting
boolean isReuseResourceInGenerator()
{
return reuseResourceInGenerator;
}

@VisibleForTesting
boolean isReuseResourceInParser()
{
Expand All @@ -178,4 +170,10 @@ public String getFormatName()
{
return "msgpack";
}

@Override
public boolean canHandleBinaryNatively()
{
return true;
}
}
Loading

0 comments on commit 5921983

Please sign in to comment.