Created
July 23, 2012 09:47
-
-
Save akandratovich/3162849 to your computer and use it in GitHub Desktop.
fast serialization
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// https://github.com/kondratovich/machariel/blob/master/src/main/java/org/machariel/core/serialization/Serializer.java#L34 | |
private Key serializecm(Object object, int depth, ClassMap cm) { | |
final int size = Reflection.size(object); | |
final Key ref = allocator.allocate(cm, size - Reflection.MAGIC_SIZE + PTR_OFFSET); // Unsafe.allocateMemory(size) | |
allocator.put(ref, 0, size); // Unsafe.putInt(pointer, offset, value) | |
allocator.put(ref, Reflection.ARRAY_INT_INDEX_SCALE, object.getClass().hashCode()); // Unsafe.putInt(pointer, offset, value) | |
allocator.copy(object, Reflection.MAGIC_SIZE, ref, PTR_OFFSET, size - Reflection.MAGIC_SIZE); // Unsafe.copyMemory(...) | |
for (int j = 0; j < cm.refs().length; j++) { // in case we have Reference fields | |
final int i = cm.refs()[j]; | |
allocator.fill(ref, PTR_OFFSET - Reflection.MAGIC_SIZE + cm.offset(i), Reflection.ADDRESS_SIZE, (byte) 0); | |
if (depth > 0) { | |
Object member = Allocator.getObject(object, cm.offset(i)); | |
if (member != null) ref.member(j, serialize(member, depth - 1)); | |
} | |
} | |
return ref; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Хм, а если преаллоцировать память бОльшими кусками, а не под каждый обьект?..