mirror of
https://github.com/OpenHFT/Chronicle-Map.git
synced 2021-09-13 01:26:12 +00:00
Optimize BytesMarshallable default serialization and add a test for it
This commit is contained in:
+30
-6
@@ -16,9 +16,7 @@
|
||||
|
||||
package net.openhft.chronicle.hash.serialization.impl;
|
||||
|
||||
import net.openhft.chronicle.bytes.Bytes;
|
||||
import net.openhft.chronicle.bytes.BytesMarshallable;
|
||||
import net.openhft.chronicle.bytes.RandomDataInput;
|
||||
import net.openhft.chronicle.bytes.*;
|
||||
import net.openhft.chronicle.hash.Data;
|
||||
import net.openhft.chronicle.hash.serialization.DataAccess;
|
||||
import net.openhft.chronicle.wire.WireIn;
|
||||
@@ -28,8 +26,10 @@ import org.jetbrains.annotations.Nullable;
|
||||
public class BytesMarshallableDataAccess<T extends BytesMarshallable>
|
||||
extends InstanceCreatingMarshaller<T> implements DataAccess<T>, Data<T> {
|
||||
|
||||
/** Cache field */
|
||||
// Cache fields
|
||||
private transient boolean bytesInit;
|
||||
private transient Bytes bytes;
|
||||
private transient VanillaBytes targetBytes;
|
||||
|
||||
/** State field */
|
||||
private transient T instance;
|
||||
@@ -47,13 +47,23 @@ public class BytesMarshallableDataAccess<T extends BytesMarshallable>
|
||||
|
||||
private void initTransients() {
|
||||
bytes = Bytes.allocateElasticDirect(1);
|
||||
targetBytes = VanillaBytes.vanillaBytes();
|
||||
}
|
||||
|
||||
@Override
|
||||
public RandomDataInput bytes() {
|
||||
initBytes();
|
||||
return bytes.bytesStore();
|
||||
}
|
||||
|
||||
private void initBytes() {
|
||||
if (!bytesInit) {
|
||||
bytes.clear();
|
||||
instance.writeMarshallable(bytes);
|
||||
bytesInit = true;
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public long offset() {
|
||||
return 0;
|
||||
@@ -61,9 +71,23 @@ public class BytesMarshallableDataAccess<T extends BytesMarshallable>
|
||||
|
||||
@Override
|
||||
public long size() {
|
||||
initBytes();
|
||||
return bytes.readRemaining();
|
||||
}
|
||||
|
||||
@Override
|
||||
public void writeTo(RandomDataOutput target, long targetOffset) {
|
||||
if (bytesInit) {
|
||||
target.write(targetOffset, bytes(), offset(), size());
|
||||
} else {
|
||||
targetBytes.bytesStore((BytesStore) target, targetOffset,
|
||||
target.capacity() - targetOffset);
|
||||
targetBytes.writePosition(targetOffset);
|
||||
instance.writeMarshallable(targetBytes);
|
||||
targetBytes.bytesStore(NoBytesStore.NO_BYTES_STORE, 0, 0);
|
||||
}
|
||||
}
|
||||
|
||||
@Override
|
||||
public T get() {
|
||||
return instance;
|
||||
@@ -73,6 +97,7 @@ public class BytesMarshallableDataAccess<T extends BytesMarshallable>
|
||||
public T getUsing(@Nullable T using) {
|
||||
if (using == null)
|
||||
using = createInstance();
|
||||
initBytes();
|
||||
using.readMarshallable(bytes);
|
||||
bytes.readPosition(0);
|
||||
return using;
|
||||
@@ -96,8 +121,7 @@ public class BytesMarshallableDataAccess<T extends BytesMarshallable>
|
||||
@Override
|
||||
public Data<T> getData(@NotNull T instance) {
|
||||
this.instance = instance;
|
||||
bytes.clear();
|
||||
instance.writeMarshallable(bytes);
|
||||
bytesInit = false;
|
||||
return this;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user