Optimize CharSequence, StringBuilder and String Chronicle Map's key or value type serialization

This commit is contained in:
leventov
2016-03-04 21:19:56 +07:00
parent 7fa9d23cd0
commit 08e21547a8
9 changed files with 217 additions and 9 deletions
@@ -0,0 +1,89 @@
/*
* 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.bytes.Bytes;
import net.openhft.chronicle.bytes.BytesUtil;
import net.openhft.chronicle.bytes.RandomDataInput;
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;
abstract class AbstractCharSequenceUtf8DataAccess<T extends CharSequence> extends AbstractData<T>
implements DataAccess<T>, Data<T> {
/** Cache field */
private transient Bytes bytes;
/** State field */
transient T cs;
AbstractCharSequenceUtf8DataAccess() {
initTransients();
}
private void initTransients() {
bytes = Bytes.allocateElasticDirect(1);
}
@Override
public Data<T> getData(@NotNull T cs) {
this.cs = cs;
bytes.clear();
BytesUtil.appendUtf8(bytes, cs);
return this;
}
@Override
public void uninit() {
cs = 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 RandomDataInput bytes() {
return bytes.bytesStore();
}
@Override
public long offset() {
return 0;
}
@Override
public long size() {
return bytes.readRemaining();
}
@Override
public T get() {
return cs;
}
}
@@ -30,8 +30,8 @@ import java.io.IOException;
import java.io.ObjectInputStream;
/**
* {@link BytesReader} implementation for {@code CharSequence}, for the primary ChronicleMap's key
* of value type {@link CharSequenceSizedReader} + {@link CharSequenceSizedWriter} are more
* {@link BytesReader} implementation for {@code CharSequence}. For the primary ChronicleMap's key
* or value type {@link CharSequenceSizedReader} + {@link CharSequenceUtf8DataAccess} are more
* efficient (because don't store the size twice), so this reader is useful in conjunction with
* {@link ListMarshaller} or {@link SetMarshaller}.
*
@@ -23,8 +23,8 @@ import net.openhft.chronicle.hash.serialization.SetMarshaller;
import org.jetbrains.annotations.NotNull;
/**
* {@link BytesWriter} implementation for {@link CharSequence}, for the primary ChronicleMap's
* key or value type {@link CharSequenceSizedWriter} + {@link StringSizedReader} are more
* {@link BytesWriter} implementation for {@link CharSequence}, for the primary ChronicleMap's key
* or value type {@link CharSequenceUtf8DataAccess} + {@link CharSequenceSizedReader} are more
* efficient (because don't store the size twice), so this writer is useful in conjunction with
* {@link ListMarshaller} or {@link SetMarshaller}.
*
@@ -21,6 +21,10 @@ import net.openhft.chronicle.bytes.BytesUtil;
import net.openhft.chronicle.hash.serialization.SizedWriter;
import org.jetbrains.annotations.NotNull;
/**
* @deprecated use one of {@link AbstractCharSequenceUtf8DataAccess} subclasses instead
*/
@Deprecated
public enum CharSequenceSizedWriter
implements SizedWriter<CharSequence>, EnumMarshallable<CharSequenceSizedWriter> {
INSTANCE;
@@ -0,0 +1,42 @@
/*
* 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.hash.serialization.DataAccess;
import org.jetbrains.annotations.Nullable;
public final class CharSequenceUtf8DataAccess
extends AbstractCharSequenceUtf8DataAccess<CharSequence> {
@Override
public CharSequence getUsing(@Nullable CharSequence using) {
StringBuilder sb;
if (using instanceof StringBuilder) {
sb = (StringBuilder) using;
sb.setLength(0);
} else {
sb = new StringBuilder(cs.length());
}
sb.append(cs);
return sb;
}
@Override
public DataAccess<CharSequence> copy() {
return new CharSequenceUtf8DataAccess();
}
}
@@ -82,13 +82,13 @@ public final class SerializationBuilder<T> implements Cloneable {
sizeMarshaller(constant(((Byteable) OS.memory().allocateInstance(tClass)).maxSize()));
} else if (tClass == CharSequence.class) {
reader((SizedReader<T>) CharSequenceSizedReader.INSTANCE);
writer((SizedWriter<T>) CharSequenceSizedWriter.INSTANCE);
dataAccess((DataAccess<T>) new CharSequenceUtf8DataAccess());
} else if (tClass == StringBuilder.class) {
reader((SizedReader<T>) StringBuilderSizedReader.INSTANCE);
writer((SizedWriter<T>) CharSequenceSizedWriter.INSTANCE);
dataAccess((DataAccess<T>) new StringBuilderUtf8DataAccess());
} else if (tClass == String.class) {
reader((SizedReader<T>) new StringSizedReader());
notReusingWriter((SizedWriter<T>) CharSequenceSizedWriter.INSTANCE);
dataAccess((DataAccess<T>) new StringUtf8DataAccess());
} else if (tClass == Boolean.class) {
reader((SizedReader<T>) BooleanMarshaller.INSTANCE);
notReusingWriter((SizedWriter<T>) BooleanMarshaller.INSTANCE);
@@ -0,0 +1,40 @@
/*
* 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.hash.serialization.DataAccess;
import org.jetbrains.annotations.Nullable;
public final class StringBuilderUtf8DataAccess
extends AbstractCharSequenceUtf8DataAccess<StringBuilder> {
@Override
public StringBuilder getUsing(@Nullable StringBuilder using) {
if (using != null) {
using.setLength(0);
} else {
using = new StringBuilder(cs.length());
}
using.append(cs);
return using;
}
@Override
public DataAccess<StringBuilder> copy() {
return new StringBuilderUtf8DataAccess();
}
}
@@ -27,8 +27,8 @@ import org.jetbrains.annotations.NotNull;
import org.jetbrains.annotations.Nullable;
/**
* {@link BytesReader} implementation for String, for the primary ChronicleMap's key of value type
* {@link StringSizedReader} + {@link CharSequenceSizedWriter} are more efficient (because don't
* {@link BytesReader} implementation for String, for the primary ChronicleMap's key or value type
* {@link StringSizedReader} + {@link StringUtf8DataAccess} are more efficient (because don't
* store the size twice), so this reader is useful in conjunction with {@link ListMarshaller} or
* {@link SetMarshaller}.
*
@@ -0,0 +1,33 @@
/*
* 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.hash.serialization.DataAccess;
import org.jetbrains.annotations.Nullable;
public final class StringUtf8DataAccess extends AbstractCharSequenceUtf8DataAccess<String> {
@Override
public String getUsing(@Nullable String using) {
return cs;
}
@Override
public DataAccess<String> copy() {
return new StringUtf8DataAccess();
}
}