chaokunyang commented on code in PR #2312:
URL: https://github.com/apache/fory/pull/2312#discussion_r2173209105
##########
java/fory-core/src/main/java/org/apache/fory/resolver/XtypeResolver.java:
##########
@@ -232,16 +228,126 @@ private void register(
if (type.isEnum()) {
classInfo.serializer = new EnumSerializer(fory, (Class<Enum>) type);
} else {
- classInfo.serializer =
- new LazySerializer.LazyObjectSerializer(
- fory, type, () -> new ObjectSerializer<>(fory, type));
+ boolean codegen =
+ supportCodegenForJavaSerialization(type) &&
fory.getConfig().isCodeGenEnabled();
+ Class<? extends Serializer> objectSerializerClass =
+ getObjectSerializerClass(
+ type,
+ shareMeta,
+ codegen,
+ new JITContext.SerializerJITCallback<Class<? extends
Serializer>>() {
+ @Override
+ public void onSuccess(Class<? extends Serializer> result) {
+ setSerializer(
+ type,
+ Serializers.newSerializer(fory, type, result),
+ namespace,
+ typeName,
+ xtypeId);
+ if (classInfoCache.classInfo.cls == type) {
+ classInfoCache.classInfo = NIL_CLASS_INFO; // clear
class info cache
+ }
+ Preconditions.checkState(getSerializer(type).getClass() ==
result);
+ }
+
+ @Override
+ public Object id() {
+ return type;
+ }
+ });
+ classInfo.serializer = Serializers.newSerializer(fory, type,
objectSerializerClass);
}
}
classInfoMap.put(type, classInfo);
registeredTypeIds.add(xtypeId);
xtypeIdToClassMap.put(xtypeId, classInfo);
}
+ /**
+ * Set the serializer for <code>cls</code>, overwrite serializer if exists.
Note if class info is
+ * already related with a class, this method should try to reuse that class
info, otherwise jit
+ * callback to update serializer won't take effect in some cases since it
can't change that
+ * classinfo.
+ */
+ public <T> void setSerializer(
+ Class<T> cls, Serializer<T> serializer, String namespace, String
typeName, int xtypeId) {
+ addSerializer(cls, serializer, namespace, typeName, xtypeId);
+ }
+
+ /** Ass serializer for specified class. */
+ private void addSerializer(
+ Class<?> type, Serializer<?> serializer, String namespace, String
typeName, int xtypeId) {
+ Preconditions.checkNotNull(serializer);
+ ClassInfo classInfo = classInfoMap.get(type);
+
+ if (classInfo == null) {
+ classInfo = newClassInfo(type, serializer, namespace, typeName, (short)
xtypeId);
+ classInfoMap.put(type, classInfo);
+ }
+
+ // 2. Set `Serializer` for `ClassInfo`.
+ classInfo.serializer = serializer;
+ }
+
+ private Class<? extends Serializer> getObjectSerializerClass(
+ Class<?> cls,
+ boolean shareMeta,
+ boolean codegen,
+ JITContext.SerializerJITCallback<Class<? extends Serializer>> callback) {
+ if (codegen) {
+ if (classCtx.contains(cls)) {
+ // avoid potential recursive call for seq codec generation.
+ return CodegenSerializer.LazyInitBeanSerializer.class;
+ } else {
+ try {
+ classCtx.add(cls);
+ Class<? extends Serializer> sc;
+ switch (fory.getCompatibleMode()) {
+ case SCHEMA_CONSISTENT:
+ sc =
+ fory.getJITContext()
+ .registerSerializerJITCallback(
+ () -> ObjectSerializer.class,
Review Comment:
I prefer creating serializer lazily, since the hash compute for type meta
will read names of all registered classes. And when class registration is
invoked, if the serializer are created egaerly, some classes don't have a
change to let the registered name/tag being used for hash computing
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]