Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Java] make sting builder serializer codegen eager #1141

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
39 changes: 15 additions & 24 deletions java/fury-core/src/main/java/io/fury/serializer/Serializers.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
import static io.fury.util.function.Functions.makeGetterFunction;

import io.fury.Fury;
import io.fury.collection.Tuple2;
import io.fury.memory.MemoryBuffer;
import io.fury.resolver.ClassResolver;
import io.fury.type.Type;
Expand Down Expand Up @@ -171,35 +170,27 @@ public T xread(MemoryBuffer buffer) {
}
}

static Tuple2<ToIntFunction, Function> builderCache;
private static final ToIntFunction GET_CODER;
private static final Function GET_VALUE;

private static synchronized Tuple2<ToIntFunction, Function> getBuilderFunc() {
if (builderCache == null) {
Function getValue =
(Function) makeGetterFunction(StringBuilder.class.getSuperclass(), "getValue");
try {
Method getCoderMethod = StringBuilder.class.getSuperclass().getDeclaredMethod("getCoder");
ToIntFunction<CharSequence> getCoder =
(ToIntFunction<CharSequence>) makeGetterFunction(getCoderMethod, int.class);
builderCache = Tuple2.of(getCoder, getValue);
} catch (NoSuchMethodException e) {
builderCache = Tuple2.of(null, getValue);
}
static {
GET_VALUE = (Function) makeGetterFunction(StringBuilder.class.getSuperclass(), "getValue");
ToIntFunction<CharSequence> getCoder;
try {
Method getCoderMethod = StringBuilder.class.getSuperclass().getDeclaredMethod("getCoder");
getCoder = (ToIntFunction<CharSequence>) makeGetterFunction(getCoderMethod, int.class);
} catch (NoSuchMethodException e) {
getCoder = null;
}
return builderCache;
GET_CODER = getCoder;
}

public abstract static class AbstractStringBuilderSerializer<T extends CharSequence>
extends Serializer<T> {
protected final ToIntFunction getCoder;
protected final Function getValue;
protected final StringSerializer stringSerializer;

public AbstractStringBuilderSerializer(Fury fury, Class<T> type) {
super(fury, type);
Tuple2<ToIntFunction, Function> builderFunc = getBuilderFunc();
getCoder = builderFunc.f0;
getValue = builderFunc.f1;
stringSerializer = new StringSerializer(fury);
}

Expand All @@ -215,9 +206,9 @@ public short getXtypeId() {

@Override
public void write(MemoryBuffer buffer, T value) {
if (getCoder != null) {
int coder = getCoder.applyAsInt(value);
byte[] v = (byte[]) getValue.apply(value);
if (GET_CODER != null) {
int coder = GET_CODER.applyAsInt(value);
byte[] v = (byte[]) GET_VALUE.apply(value);
buffer.writeByte(coder);
if (coder == 0) {
buffer.writePrimitiveArrayWithSizeEmbedded(v, Platform.BYTE_ARRAY_OFFSET, value.length());
Expand All @@ -229,7 +220,7 @@ public void write(MemoryBuffer buffer, T value) {
v, Platform.BYTE_ARRAY_OFFSET, value.length() << 1);
}
} else {
char[] v = (char[]) getValue.apply(value);
char[] v = (char[]) GET_VALUE.apply(value);
if (StringSerializer.isLatin(v)) {
stringSerializer.writeCharsLatin(buffer, v, value.length());
} else {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,4 +16,5 @@
# The unsafe offset get on build time may be different from runtime
Args = --initialize-at-build-time=io.fury.util.Platform,io.fury.memory.MemoryBuffer,\
io.fury.serializer.collection.UnmodifiableSerializers$Offset,\
io.fury.serializer.collection.SynchronizedSerializers$Offset
io.fury.serializer.collection.SynchronizedSerializers$Offset,\
io.fury.serializer.Serializers