Optimize Integer, Long and Double default serializers

This commit is contained in:
leventov
2016-03-04 22:44:26 +07:00
parent e677f07d46
commit b98a67211c
7 changed files with 381 additions and 15 deletions
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.algo.hashing.LongHashFunction;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.bytes.RandomDataOutput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.serialization.DataAccess;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class DoubleDataAccess extends AbstractData<Double>
implements DataAccess<Double>, Data<Double> {
// Cache fields
private boolean bsInit;
private transient BytesStore bs;
/** State field */
private transient Double instance;
public DoubleDataAccess() {
initTransients();
}
private void initTransients() {
bs = BytesStore.wrap(new byte[8]);
}
@Override
public RandomDataInput bytes() {
if (!bsInit) {
bsInit = true;
bs.writeDouble(0, instance);
}
return bs;
}
@Override
public long offset() {
return 0;
}
@Override
public long size() {
return 8;
}
@Override
public Double get() {
return instance;
}
@Override
public Double getUsing(@Nullable Double using) {
return instance;
}
@Override
public long hash(LongHashFunction f) {
return f.hashLong(Double.doubleToRawLongBits(instance));
}
@Override
public boolean equivalent(RandomDataInput source, long sourceOffset) {
return source.readLong(sourceOffset) == Double.doubleToRawLongBits(instance);
}
@Override
public void writeTo(RandomDataOutput target, long targetOffset) {
target.writeDouble(targetOffset, instance);
}
@Override
public Data<Double> getData(@NotNull Double instance) {
this.instance = instance;
bsInit = false;
return this;
}
@Override
public void uninit() {
instance = null;
}
@Override
public void readMarshallable(@NotNull WireIn wireIn) {
// no config fields to read
initTransients();
}
@Override
public void writeMarshallable(@NotNull WireOut wireOut) {
// no config fields to write
}
@Override
public DataAccess<Double> copy() {
return new DoubleDataAccess();
}
}
@@ -17,13 +17,15 @@
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizedReader;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import net.openhft.chronicle.hash.serialization.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* As a writer for top-level Chronicle Map's key or value type (Long), {@code DoubleMarshaller} is
* deprecated in favor of {@link DoubleDataAccess}. As reader and element writer for {@link
* ListMarshaller} and similar composite marshallers, {@code DoubleMarshaller} is not deprecated.
*/
public enum DoubleMarshaller
implements SizedReader<Double>, BytesReader<Double>,
SizedWriter<Double>, BytesWriter<Double>, EnumMarshallable<DoubleMarshaller> {
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.algo.hashing.LongHashFunction;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.bytes.RandomDataOutput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.serialization.DataAccess;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class IntegerDataAccess extends AbstractData<Integer>
implements DataAccess<Integer>, Data<Integer> {
// Cache fields
private boolean bsInit;
private transient BytesStore bs;
/** State field */
private transient Integer instance;
public IntegerDataAccess() {
initTransients();
}
private void initTransients() {
bs = BytesStore.wrap(new byte[4]);
}
@Override
public RandomDataInput bytes() {
if (!bsInit) {
bsInit = true;
bs.writeInt(0, instance);
}
return bs;
}
@Override
public long offset() {
return 0;
}
@Override
public long size() {
return 4;
}
@Override
public Integer get() {
return instance;
}
@Override
public Integer getUsing(@Nullable Integer using) {
return instance;
}
@Override
public long hash(LongHashFunction f) {
return f.hashInt(instance);
}
@Override
public boolean equivalent(RandomDataInput source, long sourceOffset) {
return source.readInt(sourceOffset) == instance;
}
@Override
public void writeTo(RandomDataOutput target, long targetOffset) {
target.writeInt(targetOffset, instance);
}
@Override
public Data<Integer> getData(@NotNull Integer instance) {
this.instance = instance;
return this;
}
@Override
public void uninit() {
instance = null;
bsInit = false;
}
@Override
public void readMarshallable(@NotNull WireIn wireIn) {
// no config fields to read
initTransients();
}
@Override
public void writeMarshallable(@NotNull WireOut wireOut) {
// no config fields to write
}
@Override
public DataAccess<Integer> copy() {
return new IntegerDataAccess();
}
}
@@ -17,13 +17,15 @@
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizedReader;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import net.openhft.chronicle.hash.serialization.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* As a writer for top-level Chronicle Map's key or value type (Integer), {@code IntegerMarshaller}
* is deprecated in favor of {@link IntegerDataAccess}. As reader and element writer for {@link
* ListMarshaller} and similar composite marshallers, {@code IntegerMarshaller} is not deprecated.
*/
public enum IntegerMarshaller
implements SizedReader<Integer>, BytesReader<Integer>,
SizedWriter<Integer>, BytesWriter<Integer>, EnumMarshallable<IntegerMarshaller> {
@@ -0,0 +1,120 @@
/*
* Copyright (C) 2015 higherfrequencytrading.com
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Lesser General Public License as published by
* the Free Software Foundation, either version 3 of the License.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.algo.hashing.LongHashFunction;
import net.openhft.chronicle.bytes.BytesStore;
import net.openhft.chronicle.bytes.RandomDataInput;
import net.openhft.chronicle.bytes.RandomDataOutput;
import net.openhft.chronicle.hash.AbstractData;
import net.openhft.chronicle.hash.Data;
import net.openhft.chronicle.hash.serialization.DataAccess;
import net.openhft.chronicle.wire.WireIn;
import net.openhft.chronicle.wire.WireOut;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
public final class LongDataAccess extends AbstractData<Long>
implements DataAccess<Long>, Data<Long> {
// Cache fields
private boolean bsInit;
private transient BytesStore bs;
/** State field */
private transient Long instance;
public LongDataAccess() {
initTransients();
}
private void initTransients() {
bs = BytesStore.wrap(new byte[8]);
}
@Override
public RandomDataInput bytes() {
if (!bsInit) {
bsInit = true;
bs.writeLong(0, instance);
}
return bs;
}
@Override
public long offset() {
return 0;
}
@Override
public long size() {
return 8;
}
@Override
public Long get() {
return instance;
}
@Override
public Long getUsing(@Nullable Long using) {
return instance;
}
@Override
public long hash(LongHashFunction f) {
return f.hashLong(instance);
}
@Override
public boolean equivalent(RandomDataInput source, long sourceOffset) {
return source.readLong(sourceOffset) == instance;
}
@Override
public void writeTo(RandomDataOutput target, long targetOffset) {
target.writeLong(targetOffset, instance);
}
@Override
public Data<Long> getData(@NotNull Long instance) {
this.instance = instance;
bsInit = false;
return this;
}
@Override
public void uninit() {
instance = null;
}
@Override
public void readMarshallable(@NotNull WireIn wireIn) {
// no config fields to read
initTransients();
}
@Override
public void writeMarshallable(@NotNull WireOut wireOut) {
// no config fields to write
}
@Override
public DataAccess<Long> copy() {
return new LongDataAccess();
}
}
@@ -17,13 +17,15 @@
package net.openhft.chronicle.hash.serialization.impl;
import net.openhft.chronicle.bytes.Bytes;
import net.openhft.chronicle.hash.serialization.BytesReader;
import net.openhft.chronicle.hash.serialization.BytesWriter;
import net.openhft.chronicle.hash.serialization.SizedReader;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import net.openhft.chronicle.hash.serialization.*;
import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* As a writer for top-level Chronicle Map's key or value type (Long), {@code LongMarshaller} is
* deprecated in favor of {@link LongDataAccess}. As reader and element writer for {@link
* ListMarshaller} and similar composite marshallers, {@code LongMarshaller} is not deprecated.
*/
public enum LongMarshaller
implements SizedReader<Long>, BytesReader<Long>,
SizedWriter<Long>, BytesWriter<Long>, EnumMarshallable<LongMarshaller> {
@@ -95,15 +95,15 @@ public final class SerializationBuilder<T> implements Cloneable {
sizeMarshaller(constant(1));
} else if (tClass == Long.class) {
reader((SizedReader<T>) LongMarshaller.INSTANCE);
notReusingWriter((SizedWriter<T>) LongMarshaller.INSTANCE);
dataAccess((DataAccess<T>) new LongDataAccess());
sizeMarshaller(constant(8));
} else if (tClass == Double.class) {
reader((SizedReader<T>) DoubleMarshaller.INSTANCE);
notReusingWriter((SizedWriter<T>) DoubleMarshaller.INSTANCE);
dataAccess((DataAccess<T>) new DoubleDataAccess());
sizeMarshaller(constant(8));
} else if (tClass == Integer.class) {
reader((SizedReader<T>) IntegerMarshaller.INSTANCE);
notReusingWriter((SizedWriter<T>) IntegerMarshaller.INSTANCE);
dataAccess((DataAccess<T>) new IntegerDataAccess());
sizeMarshaller(constant(4));
} else if (tClass == byte[].class) {
reader((SizedReader<T>) ByteArraySizedReader.INSTANCE);