调整中

master
linbin 2024-04-15 20:36:00 +08:00
parent c92ed14cee
commit 8488a4fe3e
5 changed files with 92 additions and 659 deletions

View File

@ -1,5 +1,9 @@
package com.jfirer.se;
/**
* intlongshortchar
* varInt varLong
*/
public class InternalByteArray extends ByteArray
{
public InternalByteArray(int size)
@ -838,11 +842,10 @@ public class InternalByteArray extends ByteArray
byte coder = UNSAFE.getByte(value, STRING_CODER_FIELD_OFFSET);
int idx = writerIndex;
int numBytes = bytes.length;
ensureNewWriterIndex(idx + 9 + bytes.length);
ensureNewWriterIndex(idx + 9 + numBytes);
UNSAFE.putByte(array, BYTE_ARRAY_OFFSET + idx, coder);
idx += writePositiveVarIntWithoutEnsure(numBytes) + 1;
final long destAddr = BYTE_ARRAY_OFFSET + idx;
copyMemory(bytes, BYTE_ARRAY_OFFSET, array, destAddr, numBytes);
System.arraycopy(bytes, 0, array, idx, numBytes);
writerIndex = idx + numBytes;
}
else
@ -851,14 +854,16 @@ public class InternalByteArray extends ByteArray
writeCharsWithSizeEmbedded(chars);
}
}
public String readString(){
public String readString()
{
if (STRING_VALUE_FIELD_IS_BYTES)
{
}
else{
else
{
}
return null;
}
public boolean remainRead()

View File

@ -1,5 +1,6 @@
package com.jfirer.se.serializer;
import com.jfirer.fse.serializer.array.*;
import com.jfirer.se.InternalByteArray;
import com.jfirer.se.JfireSE;
import com.jfirer.se.serializer.impl.ArraySerializer;
@ -16,23 +17,6 @@ public class SerializerResolver
public SerializerResolver()
{
store.put(int[].class, new IntArraySerializer());
store.put(long[].class, new LongArraySerializer());
store.put(double[].class, new DoubleArraySerializer());
store.put(float[].class, new FloatArraySerializer());
store.put(short[].class, new ShortArraySerializer());
store.put(char[].class, new CharArraySerializer());
store.put(byte[].class, new ByteArraySerializer());
store.put(boolean[].class, new BooleanArraySerializer());
store.put(Integer[].class, new BoxedIntArraySerializer());
store.put(Long[].class, new BoxedLongArraySerializer());
store.put(Double[].class, new BoxedDoubleArraySerializer());
store.put(Float[].class, new BoxedFloatArraySerializer());
store.put(Short[].class, new BoxedShortArraySerializer());
store.put(Character[].class, new BoxedCharArraySerializer());
store.put(Byte[].class, new BoxedByteArraySerializer());
store.put(Boolean[].class, new BoxedBooleanArraySerializer());
store.put(String[].class, new StringArraySerializer());
}
public Serializer getSerializer(Class clazz, JfireSE jfireSE)
@ -70,631 +54,4 @@ public class SerializerResolver
return count-1;
}
class IntArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(int[].class);
int shift = getShift(UNSAFE.arrayIndexScale(int[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
int[] array = (int[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeVarInt(UNSAFE.getInt(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
int[] array = new int[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putInt(array, offset + ((long) i << shift), byteArray.readVarInt());
}
return array;
}
}
class LongArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(long[].class);
int shift = getShift(UNSAFE.arrayIndexScale(long[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
long[] array = (long[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeVarLong(UNSAFE.getLong(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
long[] array = new long[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putLong(array, offset + ((long) i << shift), byteArray.readVarLong());
}
return array;
}
}
class DoubleArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(double[].class);
int shift = getShift(UNSAFE.arrayIndexScale(double[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
double[] array = (double[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeDouble(UNSAFE.getDouble(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
double[] array = new double[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putDouble(array, offset + ((long) i << shift), byteArray.readDouble());
}
return array;
}
}
class FloatArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(float[].class);
int shift = getShift(UNSAFE.arrayIndexScale(float[].class) );
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
float[] array = (float[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeFloat(UNSAFE.getFloat(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
float[] array = new float[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putFloat(array, offset + ((long) i << shift), byteArray.readFloat());
}
return array;
}
}
class CharArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(char[].class);
int shift = getShift(UNSAFE.arrayIndexScale(char[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
char[] array = (char[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeVarChar(UNSAFE.getChar(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
char[] array = new char[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putChar(array, offset + ((long) i << shift), byteArray.readVarChar());
}
return array;
}
}
class ShortArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(short[].class);
int shift = getShift(UNSAFE.arrayIndexScale(short[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
short[] array = (short[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.writeShort(UNSAFE.getShort(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
short[] array = new short[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putShort(array, offset + ((long) i << shift), byteArray.readShort());
}
return array;
}
}
class ByteArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(byte[].class);
int shift = getShift(UNSAFE.arrayIndexScale(byte[].class) );
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
byte[] array = (byte[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.put(UNSAFE.getByte(array, offset + ((long) i << shift)));
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
byte[] array = new byte[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putByte(array, offset + ((long) i << shift), byteArray.get());
}
return array;
}
}
class BooleanArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(boolean[].class);
int shift = getShift(UNSAFE.arrayIndexScale(boolean[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
boolean[] array = (boolean[]) instance;
byteArray.writeVarInt(array.length);
for (int i = 0; i < array.length; i++)
{
byteArray.put(UNSAFE.getBoolean(array, offset + ((long) i << shift)) ? (byte) 1 : (byte) 0);
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
boolean[] array = new boolean[length];
for (int i = 0; i < length; i++)
{
UNSAFE.putBoolean(array, offset + ((long) i << shift), byteArray.get() == 1);
}
return array;
}
}
class BoxedIntArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Integer[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Integer[].class) );
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Integer[] array = (Integer[]) instance;
byteArray.writeVarInt(array.length);
for (Integer i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeVarInt(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Integer[] array = new Integer[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readVarInt());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedLongArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Long[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Long[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Long[] array = (Long[]) instance;
byteArray.writeVarInt(array.length);
for (Long i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeVarLong(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Long[] array = new Long[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readVarLong());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedDoubleArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Double[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Double[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Double[] array = (Double[]) instance;
byteArray.writeVarInt(array.length);
for (Double i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeDouble(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Double[] array = new Double[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readDouble());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedFloatArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Float[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Float[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Float[] array = (Float[]) instance;
byteArray.writeVarInt(array.length);
for (Float i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeFloat(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Float[] array = new Float[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readFloat());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedBooleanArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Boolean[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Boolean[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Boolean[] array = (Boolean[]) instance;
byteArray.writeVarInt(array.length);
for (Boolean i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.put(i ? (byte) 1 : 0);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Boolean[] array = new Boolean[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.get() == 1);
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedCharArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Character[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Character[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Character[] array = (Character[]) instance;
byteArray.writeVarInt(array.length);
for (Character i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeVarChar(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Character[] array = new Character[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readVarChar());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedByteArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Byte[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Byte[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Byte[] array = (Byte[]) instance;
byteArray.writeVarInt(array.length);
for (Byte i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.put(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Byte[] array = new Byte[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.get());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class BoxedShortArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(Short[].class);
int shift = getShift(UNSAFE.arrayIndexScale(Short[].class) );
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
Short[] array = (Short[]) instance;
byteArray.writeVarInt(array.length);
for (Short i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeShort(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
Short[] array = new Short[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readShort());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
class StringArraySerializer implements Serializer
{
long offset = UNSAFE.arrayBaseOffset(String[].class);
int shift = getShift(UNSAFE.arrayIndexScale(String[].class));
@Override
public void writeBytes(InternalByteArray byteArray, Object instance)
{
String[] array = (String[]) instance;
byteArray.writeVarInt(array.length);
for (String i : array)
{
if (i == null)
{
byteArray.put(JfireSE.NULL);
}
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeString(i);
}
}
}
@Override
public Object readBytes(InternalByteArray byteArray)
{
int length = byteArray.readVarInt();
String[] array = new String[length];
for (int i = 0; i < length; i++)
{
if (byteArray.get() == JfireSE.NOT_NULL)
{
UNSAFE.putReference(array, offset + ((long) i << shift), byteArray.readString());
}
else
{
UNSAFE.putReference(array, offset + ((long) i << shift), null);
}
}
return array;
}
}
}

View File

@ -120,8 +120,8 @@ public class ObjectSerializer implements Serializer
case ReflectUtil.PRIMITIVE_LONG -> accessor.set(instance, byteArray.readVarLong());
case ReflectUtil.PRIMITIVE_FLOAT -> accessor.set(instance, byteArray.readFloat());
case ReflectUtil.PRIMITIVE_DOUBLE -> accessor.set(instance, byteArray.readDouble());
case ReflectUtil.PRIMITIVE_BOOL -> accessor.set(instance, byteArray.readPositive() == 1);
case ReflectUtil.PRIMITIVE_CHAR -> accessor.set(instance, byteArray.readVarChar());
case ReflectUtil.PRIMITIVE_BOOL -> accessor.set(instance, byteArray.readBoolean() );
case ReflectUtil.PRIMITIVE_CHAR -> accessor.set(instance, byteArray.readChar());
case ReflectUtil.PRIMITIVE_SHORT -> accessor.set(instance, byteArray.readShort());
case ReflectUtil.PRIMITIVE_BYTE -> accessor.set(instance, byteArray.get());
case ReflectUtil.CLASS_INT ->
@ -176,7 +176,7 @@ public class ObjectSerializer implements Serializer
}
else
{
accessor.set(instance, byteArray.readPositive() == 1);
accessor.set(instance, byteArray.readBoolean());
}
}
case ReflectUtil.CLASS_CHAR ->
@ -187,7 +187,7 @@ public class ObjectSerializer implements Serializer
}
else
{
accessor.set(instance, byteArray.readVarChar());
accessor.set(instance, byteArray.readChar());
}
}
case ReflectUtil.CLASS_SHORT ->
@ -234,8 +234,8 @@ public class ObjectSerializer implements Serializer
case ReflectUtil.PRIMITIVE_LONG -> byteArray.writeVarLong(accessor.getLong(instance));
case ReflectUtil.PRIMITIVE_FLOAT -> byteArray.writeFloat(accessor.getFloat(instance));
case ReflectUtil.PRIMITIVE_DOUBLE -> byteArray.writeDouble(accessor.getDouble(instance));
case ReflectUtil.PRIMITIVE_BOOL -> byteArray.writePositive(accessor.getBoolean(instance) ? 1 : 0);
case ReflectUtil.PRIMITIVE_CHAR -> byteArray.writeVarChar(accessor.getChar(instance));
case ReflectUtil.PRIMITIVE_BOOL -> byteArray.writeBoolean(accessor.getBoolean(instance));
case ReflectUtil.PRIMITIVE_CHAR -> byteArray.writeChar(accessor.getChar(instance));
case ReflectUtil.PRIMITIVE_SHORT -> byteArray.writeShort(accessor.getShort(instance));
case ReflectUtil.PRIMITIVE_BYTE -> byteArray.put(accessor.getByte(instance));
case ReflectUtil.CLASS_INT ->
@ -300,7 +300,7 @@ public class ObjectSerializer implements Serializer
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writePositive(value ? 1 : 0);
byteArray.put((byte) (value ? 1 : 0));
}
}
case ReflectUtil.CLASS_CHAR ->
@ -313,7 +313,7 @@ public class ObjectSerializer implements Serializer
else
{
byteArray.put(JfireSE.NOT_NULL);
byteArray.writeVarChar(value);
byteArray.writeChar(value);
}
}
case ReflectUtil.CLASS_SHORT ->

View File

@ -0,0 +1,43 @@
package org.example;
import io.github.karlatemp.unsafeaccessor.Unsafe;
import org.openjdk.jmh.annotations.*;
import org.openjdk.jmh.runner.Runner;
import org.openjdk.jmh.runner.RunnerException;
import org.openjdk.jmh.runner.options.Options;
import org.openjdk.jmh.runner.options.OptionsBuilder;
import java.util.concurrent.TimeUnit;
@BenchmarkMode(Mode.Throughput)
@Warmup(iterations = 2, time = 1)
@Measurement(iterations = 3, time = 10)
@OutputTimeUnit(TimeUnit.SECONDS)
@Fork(1)
@State(Scope.Benchmark)
public class BenchMarkUnsafe
{
private static final Unsafe UNSAFE = Unsafe.getUnsafe();
char c = '你';
char Rec = Character.reverseBytes(c);
private byte[] array = new byte[2];
private long offset = UNSAFE.arrayBaseOffset(byte[].class);
@Benchmark
public void write()
{
UNSAFE.putChar(array, offset, c);
}
@Benchmark
public void writeRe()
{
UNSAFE.putChar(array, offset, Rec);
}
public static void main(String[] args) throws RunnerException
{
Options opt = new OptionsBuilder().include(BenchMarkUnsafe.class.getSimpleName()).build();
new Runner(opt).run();
}
}

View File

@ -0,0 +1,28 @@
package org.example;
import io.github.karlatemp.unsafeaccessor.Unsafe;
import org.junit.Test;
import java.io.IOException;
import java.lang.reflect.Field;
import java.nio.ByteOrder;
public class Demo
{
private long l = 0x0102030405060708L;
@Test
public void test() throws IOException, NoSuchFieldException
{
Unsafe unsafe = Unsafe.getUnsafe();
boolean isBig = ByteOrder.nativeOrder() == ByteOrder.BIG_ENDIAN;
System.out.println(isBig);
Field l1 = Demo.class.getDeclaredField("l");
long offset = unsafe.objectFieldOffset(l1);
System.out.println(unsafe.getLong(this, offset));
System.out.println(l);
System.out.println((byte)(l>>56));
System.out.println(unsafe.getByte(this,offset));
System.out.println(unsafe.getByte(this,offset+1));
}
}