|
88 | 88 | import org.apache.fury.collection.IdentityMap;
|
89 | 89 | import org.apache.fury.collection.IdentityObjectIntMap;
|
90 | 90 | import org.apache.fury.collection.LongMap;
|
| 91 | +import org.apache.fury.collection.ObjectArray; |
91 | 92 | import org.apache.fury.collection.ObjectMap;
|
92 | 93 | import org.apache.fury.collection.Tuple2;
|
93 | 94 | import org.apache.fury.config.CompatibleMode;
|
@@ -204,7 +205,9 @@ public class ClassResolver {
|
204 | 205 | private static final float loadFactor = 0.25f;
|
205 | 206 | private static final float furyMapLoadFactor = 0.25f;
|
206 | 207 | private static final int estimatedNumRegistered = 150;
|
207 |
| - private static final String META_SHARE_FIELDS_INFO_KEY = "shareFieldsInfo"; |
| 208 | + private static final String SET_META__CONTEXT_MSG = |
| 209 | + "Meta context must be set before serialization, " |
| 210 | + + "please set meta context by SerializationContext.setMetaContext"; |
208 | 211 | private static final ClassInfo NIL_CLASS_INFO =
|
209 | 212 | new ClassInfo(null, null, null, null, false, null, null, ClassResolver.NO_CLASS_ID);
|
210 | 213 |
|
@@ -1285,10 +1288,7 @@ public void writeClassWithMetaShare(MemoryBuffer buffer, ClassInfo classInfo) {
|
1285 | 1288 | return;
|
1286 | 1289 | }
|
1287 | 1290 | MetaContext metaContext = fury.getSerializationContext().getMetaContext();
|
1288 |
| - Preconditions.checkNotNull( |
1289 |
| - metaContext, |
1290 |
| - "Meta context must be set before serialization, " |
1291 |
| - + "please set meta context by SerializationContext.setMetaContext"); |
| 1291 | + assert metaContext != null : SET_META__CONTEXT_MSG; |
1292 | 1292 | IdentityObjectIntMap<Class<?>> classMap = metaContext.classMap;
|
1293 | 1293 | int newId = classMap.size;
|
1294 | 1294 | int id = classMap.putOrGet(classInfo.cls, newId);
|
@@ -1332,19 +1332,16 @@ boolean needToWriteClassDef(Serializer serializer) {
|
1332 | 1332 | }
|
1333 | 1333 |
|
1334 | 1334 | private ClassInfo readClassInfoWithMetaShare(MemoryBuffer buffer, MetaContext metaContext) {
|
1335 |
| - Preconditions.checkNotNull( |
1336 |
| - metaContext, |
1337 |
| - "Meta context must be set before serialization," |
1338 |
| - + " please set meta context by SerializationContext.setMetaContext"); |
| 1335 | + assert metaContext != null : SET_META__CONTEXT_MSG; |
1339 | 1336 | int header = buffer.readVarUint32Small14();
|
1340 | 1337 | int id = header >>> 1;
|
1341 | 1338 | if ((header & 0b1) == 0) {
|
1342 | 1339 | return getOrUpdateClassInfo((short) id);
|
1343 | 1340 | }
|
1344 |
| - List<ClassInfo> readClassInfos = metaContext.readClassInfos; |
| 1341 | + ObjectArray<ClassInfo> readClassInfos = metaContext.readClassInfos; |
1345 | 1342 | ClassInfo classInfo = readClassInfos.get(id);
|
1346 | 1343 | if (classInfo == null) {
|
1347 |
| - List<ClassDef> readClassDefs = metaContext.readClassDefs; |
| 1344 | + ObjectArray<ClassDef> readClassDefs = metaContext.readClassDefs; |
1348 | 1345 | ClassDef classDef = readClassDefs.get(id);
|
1349 | 1346 | Tuple2<ClassDef, ClassInfo> classDefTuple = extRegistry.classIdToDef.get(classDef.getId());
|
1350 | 1347 | if (classDefTuple == null || classDefTuple.f1 == null) {
|
@@ -1433,11 +1430,31 @@ private ClassInfo getMetaSharedClassInfo(ClassDef classDef, Class<?> clz) {
|
1433 | 1430 | */
|
1434 | 1431 | public void writeClassDefs(MemoryBuffer buffer) {
|
1435 | 1432 | MetaContext metaContext = fury.getSerializationContext().getMetaContext();
|
1436 |
| - buffer.writeVarUint32Small7(metaContext.writingClassDefs.size()); |
1437 |
| - for (ClassDef classDef : metaContext.writingClassDefs) { |
1438 |
| - classDef.writeClassDef(buffer); |
| 1433 | + ObjectArray<ClassDef> writingClassDefs = metaContext.writingClassDefs; |
| 1434 | + final int size = writingClassDefs.size; |
| 1435 | + buffer.writeVarUint32Small7(size); |
| 1436 | + if (buffer.isHeapFullyWriteable()) { |
| 1437 | + writeClassDefs(buffer, writingClassDefs, size); |
| 1438 | + } else { |
| 1439 | + for (int i = 0; i < size; i++) { |
| 1440 | + writingClassDefs.get(i).writeClassDef(buffer); |
| 1441 | + } |
| 1442 | + } |
| 1443 | + metaContext.writingClassDefs.size = 0; |
| 1444 | + } |
| 1445 | + |
| 1446 | + private void writeClassDefs( |
| 1447 | + MemoryBuffer buffer, ObjectArray<ClassDef> writingClassDefs, int size) { |
| 1448 | + int writerIndex = buffer.writerIndex(); |
| 1449 | + for (int i = 0; i < size; i++) { |
| 1450 | + byte[] encoded = writingClassDefs.get(i).getEncoded(); |
| 1451 | + int bytesLen = encoded.length; |
| 1452 | + buffer.ensure(writerIndex + bytesLen); |
| 1453 | + final byte[] targetArray = buffer.getHeapMemory(); |
| 1454 | + System.arraycopy(encoded, 0, targetArray, writerIndex, bytesLen); |
| 1455 | + writerIndex += bytesLen; |
1439 | 1456 | }
|
1440 |
| - metaContext.writingClassDefs.clear(); |
| 1457 | + buffer.writerIndex(writerIndex); |
1441 | 1458 | }
|
1442 | 1459 |
|
1443 | 1460 | /**
|
|
0 commit comments