You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@flink.apache.org by al...@apache.org on 2020/08/27 08:41:04 UTC

[flink] branch master updated: [FLINK-19052] [serialization] add constructor cache in PojoSerializer

This is an automated email from the ASF dual-hosted git repository.

aljoscha pushed a commit to branch master
in repository https://gitbox.apache.org/repos/asf/flink.git


The following commit(s) were added to refs/heads/master by this push:
     new 162e3ea  [FLINK-19052] [serialization] add constructor cache in PojoSerializer
162e3ea is described below

commit 162e3ead63e5766cf2116af09922a88537889e53
Author: Grebennikov Roman <gr...@dfdx.me>
AuthorDate: Wed Aug 26 12:53:28 2020 +0200

    [FLINK-19052] [serialization] add constructor cache in PojoSerializer
---
 .../api/java/typeutils/runtime/PojoSerializer.java | 22 ++++++++++++++++++++--
 1 file changed, 20 insertions(+), 2 deletions(-)

diff --git a/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializer.java b/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializer.java
index 5c43d1e..f7b3874 100644
--- a/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializer.java
+++ b/flink-core/src/main/java/org/apache/flink/api/java/typeutils/runtime/PojoSerializer.java
@@ -41,6 +41,9 @@ import org.apache.flink.util.Preconditions;
 import java.io.IOException;
 import java.io.ObjectInputStream;
 import java.io.ObjectOutputStream;
+import java.lang.invoke.MethodHandle;
+import java.lang.invoke.MethodHandles;
+import java.lang.invoke.MethodType;
 import java.lang.reflect.Field;
 import java.lang.reflect.Modifier;
 import java.util.ArrayList;
@@ -72,6 +75,13 @@ public final class PojoSerializer<T> extends TypeSerializer<T> {
 	private final Class<T> clazz;
 
 	/**
+	 * A cached clazz constructor.
+	 * Invoking it using reflection (e.g. clazz.newInstance) has a lot of overhead.
+	 */
+
+	private transient MethodHandle cachedConstructor;
+
+	/**
 	 * Fields of the POJO and their serializers.
 	 *
 	 * <p>The fields are kept as a separate transient member, with their serialization
@@ -149,6 +159,7 @@ public final class PojoSerializer<T> extends TypeSerializer<T> {
 			ExecutionConfig executionConfig) {
 
 		this.clazz = checkNotNull(clazz);
+
 		this.fields = checkNotNull(fields);
 		this.numFields = fields.length;
 		this.fieldSerializers = checkNotNull(fieldSerializers);
@@ -193,11 +204,17 @@ public final class PojoSerializer<T> extends TypeSerializer<T> {
 			return null;
 		}
 		try {
-			T t = clazz.newInstance();
+			if (cachedConstructor == null) {
+				// constructor metahandle is not serializable, so we cannot populate this cache while
+				// building the serializer, as the whole serializer needs to be serializable
+				cachedConstructor = MethodHandles.lookup()
+					.findConstructor(clazz, MethodType.methodType(void.class));
+			}
+			T t = (T) cachedConstructor.invoke();
 			initializeFields(t);
 			return t;
 		}
-		catch (Exception e) {
+		catch (Throwable e) {
 			throw new RuntimeException("Cannot instantiate class.", e);
 		}
 	}
@@ -1126,4 +1143,5 @@ public final class PojoSerializer<T> extends TypeSerializer<T> {
 				subclassRegistry,
 				nonRegisteredSubclassSerializerCache);
 	}
+
 }