简化部分代码

master
linbin 2024-09-14 14:02:09 +08:00
parent eaae34d491
commit dabed0519b
15 changed files with 58 additions and 74 deletions

View File

@ -15,19 +15,9 @@ public interface JfireSE
byte CONTENT_TRACK = 8;
byte CONTENT_UN_TRACK = 9;
static JfireSEConfig supportRefTracking(boolean support)
static JfireSEConfig config()
{
return new JfireSEConfig().setRefTracking(support);
}
static JfireSEConfig staticRegisterClass(Class<?> clazz)
{
return new JfireSEConfig().staticRegisterClass(clazz);
}
static JfireSE build()
{
return new JfireSEConfig().build();
return new JfireSEConfig();
}
byte[] serialize(Object instance);

View File

@ -168,12 +168,8 @@ public class JfireSEImpl implements JfireSE
ClassInfo[] tmp = new ClassInfo[newLen];
System.arraycopy(deSerializedClassInfos, 0, tmp, 0, deSerializedClassInfos.length);
deSerializedClassInfos = tmp;
deSerializedClassInfos[classId] = classInfo;
}
else
{
deSerializedClassInfos[classId] = classInfo;
}
deSerializedClassInfos[classId] = classInfo;
return classInfo;
}

View File

@ -15,11 +15,11 @@ public abstract class ClassInfo implements RefTracking
protected final Class<?> clazz;
protected final boolean refTrack;
protected Object[] tracking;
protected int refTrackingIndex = 0;
protected int refTrackingIndex = 0;
protected Serializer serializer;
protected JfireSE jfireSE;
protected boolean needClean = false;
protected static final Unsafe UNSAFE = Unsafe.getUnsafe();
protected boolean firstSerializedOrAddTracked = true;
protected static final Unsafe UNSAFE = Unsafe.getUnsafe();
public ClassInfo(short classId, Class<?> clazz, boolean refTrack)
{
@ -64,7 +64,7 @@ public abstract class ClassInfo implements RefTracking
}
refTrackingIndex = 0;
}
needClean = false;
firstSerializedOrAddTracked = true;
}
/**
@ -114,9 +114,9 @@ public abstract class ClassInfo implements RefTracking
public Object readWithTrack(ByteArray byteArray)
{
Object result = serializer.read(byteArray, this);
if (!needClean)
if (firstSerializedOrAddTracked)
{
needClean = true;
firstSerializedOrAddTracked = false;
jfireSE.scheduleForClean(this);
}
return result;

View File

@ -22,8 +22,7 @@ public class DynamicClassInfo extends ClassInfo
@Override
public void write(ByteArray byteArray, Object instance)
{
//如果needClean 为 false意味着这个 classInfo 是首次输出。后续必然需要清理。
if (!needClean)
if (firstSerializedOrAddTracked)
{
if (refTrack)
{
@ -33,7 +32,7 @@ public class DynamicClassInfo extends ClassInfo
byteArray.writeString(classNameStringBytes, classNameStringCoder);
byteArray.writePositiveVarInt(classId);
serializer.writeBytes(byteArray, instance);
needClean = true;
firstSerializedOrAddTracked = false;
jfireSE.scheduleForClean(this);
}
else

View File

@ -31,9 +31,9 @@ public class RegisterClasInfo extends ClassInfo
byteArray.writePositiveVarInt(classId);
byteArray.writePositiveVarInt(i);
}
if (!needClean)
if (firstSerializedOrAddTracked)
{
needClean = true;
firstSerializedOrAddTracked = false;
jfireSE.scheduleForClean(this);
}
}

View File

@ -167,8 +167,8 @@ public class ObjectSerializer implements Serializer
{
MethodModel writeMethod = new MethodModel(Serializer.class.getDeclaredMethod("writeBytes", ByteArray.class, Object.class), classModel);
writeMethod.setParamterNames("byteArray", "instance");
StringBuilder writeBody = new StringBuilder();
MethodModel readMethod = new MethodModel(Serializer.class.getDeclaredMethod("read", ByteArray.class, RefTracking.class), classModel);
StringBuilder writeBody = new StringBuilder();
MethodModel readMethod = new MethodModel(Serializer.class.getDeclaredMethod("read", ByteArray.class, RefTracking.class), classModel);
readMethod.setParamterNames("byteArray", "refTracking");
StringBuilder readBody = new StringBuilder();
int fieldIndex = 0;
@ -456,10 +456,10 @@ public class ObjectSerializer implements Serializer
}
else if (fieldInfo instanceof VariableFieldInfo)
{
String classInfoProperty = "classInfo_$_" + fieldIndex;
String firstClassInfoProperty = "firstClassInfo_$_" + fieldIndex;
FieldModel classInfoModel = new FieldModel(classInfoProperty, ClassInfo.class, classModel);
FieldModel firstClassInfoModel = new FieldModel(firstClassInfoProperty, ClassInfo.class, classModel);
String classInfoProperty = "classInfo_$_" + fieldIndex;
String firstClassInfoProperty = "firstClassInfo_$_" + fieldIndex;
FieldModel classInfoModel = new FieldModel(classInfoProperty, ClassInfo.class, classModel);
FieldModel firstClassInfoModel = new FieldModel(firstClassInfoProperty, ClassInfo.class, classModel);
classModel.addField(classInfoModel, firstClassInfoModel);
initBody.append(classInfoProperty).append("=jfireSE.getOrCreateClassInfo(((FieldInfo)list_serializer_compile.get(").append(fieldIndex).append(")).getField().getType());\r\n");
initBody.append(" if( ((FieldInfo)list_serializer_compile.get(").append(fieldIndex).append(")).getField().getType().isInterface()) {");
@ -472,14 +472,15 @@ public class ObjectSerializer implements Serializer
writeBody.append("else{\r\n");
String objClassName = "objClass_$_" + fieldIndex;
writeBody.append("Class ").append(objClassName).append(" = ").append(objName).append(".getClass();\r\n");
writeBody.append("classInfo = classInfo.getClazz() == objClass ? classInfo : jfireSE.getOrCreateClassInfo(objClass);".replace("objClass", objClassName) //
.replace("classInfo", classInfoProperty));
writeBody.append("if(").append(classInfoProperty).append("==").append(firstClassInfoProperty).append("){\r\n");
writeBody.append(classInfoProperty).append(".writeKnownClazz(byteArray,").append(objName).append(");\r\n");
writeBody.append("}\r\n");
writeBody.append("else{\r\n");
writeBody.append(classInfoProperty).append(".write(byteArray,").append(objName).append(");\r\n");
writeBody.append("}\r\n");
writeBody.append("""
if (firstClassInfo.getClazz() == objClass)
{
firstClassInfo.writeKnownClazz(byteArray, $obj$);
}
else
{
jfireSE.getOrCreateClassInfo(objClass).write(byteArray, $obj$);
}""".replace("firstClassInfo", firstClassInfoProperty).replace("objClass", objClassName).replace("$obj$", objName));
writeBody.append("}\r\n");
String flagName = "flag_$_" + fieldIndex;
readBody.append("byte ").append(flagName).append(" = byteArray.get();\r\n");
@ -498,8 +499,8 @@ public class ObjectSerializer implements Serializer
}
else if (fieldInfo instanceof FinalFieldInfo)
{
String classInfoProperty = "classInfo_$_" + fieldIndex;
FieldModel classInfoModel = new FieldModel(classInfoProperty, ClassInfo.class, classModel);
String classInfoProperty = "classInfo_$_" + fieldIndex;
FieldModel classInfoModel = new FieldModel(classInfoProperty, ClassInfo.class, classModel);
classModel.addField(classInfoModel);
initBody.append(classInfoProperty + "=jfireSE.getOrCreateClassInfo(((FieldInfo)list_serializer_compile.get(" + fieldIndex + ")).getField().getType());\r\n");
writeBody.append("""
@ -586,9 +587,9 @@ public class ObjectSerializer implements Serializer
MethodModel initModel = new MethodModel(Serializer.class.getDeclaredMethod("init"), classModel);
initModel.setBody(initBody.toString());
classModel.putMethodModel(initModel);
CompileHelper compiler = new CompileHelper(Thread.currentThread().getContextClassLoader());
Class<?> compile = compiler.compile(classModel);
Serializer compiledObjectSerializer = (Serializer) compile.getDeclaredConstructor(Class.class, JfireSE.class, List.class).newInstance(clazz, jfireSE, parse);
CompileHelper compiler = new CompileHelper(Thread.currentThread().getContextClassLoader());
Class<?> compile = compiler.compile(classModel);
Serializer compiledObjectSerializer = (Serializer) compile.getDeclaredConstructor(Class.class, JfireSE.class, List.class).newInstance(clazz, jfireSE, parse);
return compiledObjectSerializer;
}
catch (NoSuchMethodException | IOException | ClassNotFoundException | InstantiationException | IllegalAccessException | InvocationTargetException e)

View File

@ -43,14 +43,13 @@ public class VariableFieldInfo extends FieldInfo
else
{
Class<?> objClass = obj.getClass();
classInfo = classInfo.getClazz() == objClass ? classInfo : jfireSE.getOrCreateClassInfo(objClass);
if (classInfo == firstClassInfo)
if (firstClassInfo.getClazz() == objClass)
{
classInfo.writeKnownClazz(byteArray, obj);
firstClassInfo.writeKnownClazz(byteArray, obj);
}
else
{
classInfo.write(byteArray, obj);
jfireSE.getOrCreateClassInfo(objClass).write(byteArray, obj);
}
}
}

View File

@ -24,7 +24,7 @@ public class BenchMarkRead
Fury fury = Fury.builder().withLanguage(Language.JAVA)//
.requireClassRegistration(false)//
.withRefTracking(true).build();
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
TestData data = new TestData().setTestDataSm(new TestDataSm()).setTestDataSm2(new TestDataSm2());
byte[] serialize = jfireSE.serialize(data);
byte[] serialize2 = fury.serialize(data);

View File

@ -30,7 +30,7 @@ public class BenchMarkWrite
Fury fury = Fury.builder().withLanguage(Language.JAVA)//
.requireClassRegistration(false)//
.withRefTracking(true).build();
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
public void testFSENoCompile()
{

View File

@ -36,7 +36,7 @@ public class FunctionTest
TestDataSm[] sms = new TestDataSm[2];
sms[0] = new TestDataSm().setC("xx");
testData.setSms(sms);
JfireSE jfireSE = JfireSE.build();
JfireSE jfireSE = JfireSE.config().build();
byte[] bytes = jfireSE.serialize(testData);
Assert.assertEquals(testData, jfireSE.deSerialize(bytes));
TestData read = (TestData) jfireSE.deSerialize(bytes);
@ -53,7 +53,7 @@ public class FunctionTest
home.setPerson(person);
person.setHome(home);
fury.serialize(home);
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
jfireSE.serialize(home);
}
}

View File

@ -7,7 +7,7 @@ import org.junit.Test;
public class Profile
{
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
TestData data = new TestData().setTestDataSm(new TestDataSm()).setTestDataSm2(new TestDataSm2());
byte[] serialize = jfireSE.serialize(data);
Object unuse = jfireSE.deSerialize(serialize);
@ -15,7 +15,7 @@ public class Profile
@Test
public void test()
{
for (int i = 0; i < 10000000; i++)
for (int i = 0; i < 30000000; i++)
{
jfireSE.deSerialize(serialize);
}

View File

@ -9,7 +9,7 @@ public class BaseTest
@Test
public void test()
{
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
User user = new User();
user.setAge(123);
user.setName("aaa");

View File

@ -23,7 +23,7 @@ public class LongTest
kryo.writeClassAndObject(output, new LongData());
byte[] bb = output.toBytes();
System.out.println("LongData序列化kryo基础数据长度" + bb.length);
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(new LongData());
System.out.println("LongData序列化jfirese基础数据长度" + serialize.length);
System.out.println("序列化长度减少" + (bb.length - serialize.length));
@ -35,5 +35,4 @@ public class LongTest
log.info("basedata序列化jfirese基础数据长度" + serialize1.length);
log.info("序列化长度减少{}", (bb.length - serialize1.length));
}
}

View File

@ -20,7 +20,7 @@ public class MapTest
{
MapDemo demo = new MapDemo();
demo.getMap().put(1, new BaseData());
JfireSE jfireSE = JfireSE.staticRegisterClass(MapDemo.class).refTracking().build();
JfireSE jfireSE = JfireSE.config().staticRegisterClass(MapDemo.class).refTracking().build();
byte[] serialize = jfireSE.serialize(demo);
jfireSE.deSerialize(serialize);
}

View File

@ -20,7 +20,7 @@ public class RightTest
{
// 创建需要序列化的对象
BaseData baseData = new BaseData(1);
JfireSE jfireSE = JfireSE.supportRefTracking(true).staticRegisterClass(baseData.getClass()).build();
JfireSE jfireSE = JfireSE.config().refTracking().staticRegisterClass(baseData.getClass()).build();
byte[] serialize = jfireSE.serialize(baseData);
// 传入二进制buffer对象读取其中的 数据并且反序列化成对象
BaseData result = (BaseData) jfireSE.deSerialize(serialize);
@ -76,7 +76,7 @@ public class RightTest
public void wrapTest() throws IllegalArgumentException, IllegalAccessException, UnsupportedEncodingException, ClassNotFoundException, InstantiationException
{
WrapData wrapData = new WrapData();
JfireSE jfireSE = JfireSE.supportRefTracking(true).staticRegisterClass(WrapData.class).build();
JfireSE jfireSE = JfireSE.config().refTracking().staticRegisterClass(WrapData.class).build();
byte[] serialize = jfireSE.serialize(wrapData);
WrapData result = (WrapData) jfireSE.deSerialize(serialize);
assertEquals(result.getA(), wrapData.getA());
@ -150,7 +150,7 @@ public class RightTest
Person tPerson = new Person("zhangshi[in", 30);
person.setLeader(tPerson);
tPerson.setLeader(person);
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(person);
Person result = (Person) jfireSE.deSerialize(serialize);
assertEquals("zhangshi[in", result.getLeader().getName());
@ -159,7 +159,7 @@ public class RightTest
@Test
public void objectTest() throws IllegalArgumentException, IllegalAccessException, ClassNotFoundException, InstantiationException
{
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
Calendar calendar = Calendar.getInstance();
byte[] serialize = jfireSE.serialize(calendar);
Calendar reCalendar = (Calendar) jfireSE.deSerialize(serialize);
@ -175,7 +175,7 @@ public class RightTest
{
list.add(new BaseData(i));
}
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(list);
ArrayList<BaseData> result = (ArrayList<BaseData>) jfireSE.deSerialize(serialize);
Assert.assertTrue(list.equals(result));
@ -184,7 +184,7 @@ public class RightTest
@Test
public void baseDataTest()
{
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
BaseData baseData = new BaseData();
byte[] serialize = jfireSE.serialize(baseData);
BaseData result = (BaseData) jfireSE.deSerialize(serialize);
@ -199,7 +199,7 @@ public class RightTest
array[1] = new BaseData();
array[2] = new LongData();
array[3] = new WrapData();
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(array);
Object[] result = (Object[]) jfireSE.deSerialize(serialize);
Assert.assertTrue(((Person) result[0]).equals(array[0]));
@ -209,7 +209,7 @@ public class RightTest
public void byteArrayTest()
{
byte[] array = new byte[]{1, 2, 5, 6, 8, 9};
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(array);
byte[] result = (byte[]) jfireSE.deSerialize(serialize);
for (int i = 0; i < array.length; i++)
@ -222,7 +222,7 @@ public class RightTest
public void booleanArrayTest()
{
boolean[] array = new boolean[]{true, false, false, true, true, true};
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(array);
boolean[] result = (boolean[]) jfireSE.deSerialize(serialize);
for (int i = 0; i < array.length; i++)
@ -234,7 +234,7 @@ public class RightTest
@Test
public void arrayDataTest()
{
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(new ArrayData());
jfireSE.deSerialize(serialize);
}
@ -246,7 +246,7 @@ public class RightTest
byte[] key = new byte[16];
random.nextBytes(key);
Object[] data = new Object[]{Integer.valueOf(14), new BaseData[]{new BaseData(), new BaseData()}};
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(data);
Object[] result = (Object[]) jfireSE.deSerialize(serialize);
assertEquals(14, result[0]);
@ -261,7 +261,7 @@ public class RightTest
public void arryaNotRegisterClassSeri()
{
ArrayRefenceHolder holder = new ArrayRefenceHolder();
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(holder);
ArrayRefenceHolder result = (ArrayRefenceHolder) jfireSE.deSerialize(serialize);
assertArrayEquals(new int[]{1, 2}, result.getA()[0]);
@ -274,7 +274,7 @@ public class RightTest
public void methodObjectTest() throws NoSuchMethodException
{
Method methodObjectTest = this.getClass().getDeclaredMethod("methodObjectTest");
JfireSE jfireSE = JfireSE.supportRefTracking(true).build();
JfireSE jfireSE = JfireSE.config().refTracking().build();
byte[] serialize = jfireSE.serialize(methodObjectTest);
Method method = (Method) jfireSE.deSerialize(serialize);
System.out.println(method.equals(methodObjectTest));