further code moved to jwo

This commit is contained in:
2019-12-12 00:20:04 +00:00
parent 0441c02cb0
commit c428060a30
12 changed files with 20 additions and 479 deletions

View File

@@ -3,11 +3,11 @@ package net.woggioni.worth.serialization.json;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import lombok.SneakyThrows;
import net.woggioni.jwo.Chronometer;
import net.woggioni.worth.antlr.JSONLexer;
import net.woggioni.worth.antlr.JSONListenerImpl;
import net.woggioni.worth.serialization.binary.JBONDumper;
import net.woggioni.worth.serialization.binary.JBONParser;
import net.woggioni.worth.utils.Chronometer;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Dumper;
import net.woggioni.worth.xface.Parser;
@@ -66,7 +66,7 @@ public class PerformanceTest {
JsonNode jsonNode = om.readTree(smallTestData());
}
}
jacksonTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
jacksonTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Jackson time: %8s msec\n", String.format("%.3f", jacksonTime));
}
{
@@ -76,7 +76,7 @@ public class PerformanceTest {
Value value = new JSONParser().parse(smallTestData());
}
}
worthTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
worthTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Worth time: %8s msec\n", String.format("%.3f", worthTime));
}
{
@@ -92,7 +92,7 @@ public class PerformanceTest {
walker.walk(listener, parser.json());
}
}
antlrTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
antlrTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Antlr time: %8s msec\n", String.format("%.3f", antlrTime));
}
}
@@ -110,20 +110,20 @@ public class PerformanceTest {
chr.reset();
ObjectMapper om = new ObjectMapper();
om.readTree(is);
jacksonTime = chr.stop(Chronometer.TimeUnit.SECOND);
jacksonTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Jackson time: %8s sec\n", String.format("%.3f", jacksonTime));
}
try(InputStream is = extractTestData()) {
chr.reset();
new JSONParser(cfg).parse(is);
worthTime = chr.stop(Chronometer.TimeUnit.SECOND);
worthTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Worth time: %8s sec\n", String.format("%.3f", worthTime));
}
try(InputStream is = extractBinaryTestData()) {
chr.reset();
new JBONParser(cfg).parse(is);
worthTime = chr.stop(Chronometer.TimeUnit.SECOND);
worthTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Worth time binary: %8s sec\n", String.format("%.3f", worthTime));
}
@@ -136,7 +136,7 @@ public class PerformanceTest {
JSONListenerImpl listener = new JSONListenerImpl();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(listener, parser.json());
antlrTime = chr.stop(Chronometer.TimeUnit.SECOND);
antlrTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Antlr time: %8s sec\n", String.format("%.3f", antlrTime));
}
}

View File

@@ -7,7 +7,7 @@ import net.woggioni.worth.antlr.JSONLexer;
import net.woggioni.worth.antlr.JSONListenerImpl;
import net.woggioni.worth.serialization.binary.JBONParser;
import net.woggioni.worth.serialization.json.JSONParser;
import net.woggioni.worth.utils.Chronometer;
import net.woggioni.jwo.Chronometer;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Value;
import org.antlr.v4.runtime.CharStream;
@@ -57,7 +57,7 @@ public class Main {
JsonNode jsonNode = om.readTree(smallTestData());
}
}
jacksonTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
jacksonTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Jackson time: %8s msec\n", String.format("%.3f", jacksonTime));
}
{
@@ -67,7 +67,7 @@ public class Main {
Value value = new JSONParser().parse(new BufferedReader(new InputStreamReader(smallTestData())));
}
}
worthTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
worthTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Worth time: %8s msec\n", String.format("%.3f", worthTime));
}
{
@@ -83,7 +83,7 @@ public class Main {
walker.walk(listener, parser.json());
}
}
antlrTime = chr.stop(Chronometer.TimeUnit.MILLISECOND);
antlrTime = chr.elapsed(Chronometer.UnitOfMeasure.MILLISECONDS);
System.out.printf("Antlr time: %8s msec\n", String.format("%.3f", antlrTime));
}
}
@@ -101,7 +101,7 @@ public class Main {
chr.reset();
ObjectMapper om = new ObjectMapper();
om.readTree(new InputStreamReader(is));
double elapsedTime = chr.stop(Chronometer.TimeUnit.SECOND);
double elapsedTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Jackson time: %8s sec\n", String.format("%.3f", elapsedTime));
}
}
@@ -112,7 +112,7 @@ public class Main {
try (InputStream is = extractTestData()) {
chr.reset();
new JSONParser(buildConfiguration()).parse(is);
double elapsedTime = chr.stop(Chronometer.TimeUnit.SECOND);
double elapsedTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Worth json time: %8s sec\n", String.format("%.3f", elapsedTime));
}
}
@@ -123,7 +123,7 @@ public class Main {
try (InputStream is = extractBinaryTestData()) {
chr.reset();
new JBONParser(buildConfiguration()).parse(is);
double elapsedTime = chr.stop(Chronometer.TimeUnit.SECOND);
double elapsedTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Worth jbon time: %8s sec\n", String.format("%.3f", elapsedTime));
}
}
@@ -140,7 +140,7 @@ public class Main {
JSONListenerImpl listener = new JSONListenerImpl();
ParseTreeWalker walker = new ParseTreeWalker();
walker.walk(listener, parser.json());
double elapsedTime = chr.stop(Chronometer.TimeUnit.SECOND);
double elapsedTime = chr.elapsed(Chronometer.UnitOfMeasure.SECONDS);
System.out.printf("Antlr time: %8s sec\n", String.format("%.3f", elapsedTime));
}
}

View File

@@ -1,10 +1,10 @@
package net.woggioni.worth.serialization.binary;
import lombok.SneakyThrows;
import net.woggioni.jwo.Leb128;
import net.woggioni.worth.exception.NotImplementedException;
import net.woggioni.worth.serialization.ValueDumper;
import net.woggioni.worth.traversal.ValueIdentity;
import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.value.ArrayValue;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Dumper;

View File

@@ -1,11 +1,10 @@
package net.woggioni.worth.serialization.binary;
import lombok.SneakyThrows;
import net.woggioni.jwo.Leb128;
import net.woggioni.worth.buffer.LookAheadInputStream;
import net.woggioni.worth.exception.ParseException;
import net.woggioni.worth.serialization.ValueParser;
import net.woggioni.worth.utils.Leb128;
import net.woggioni.worth.utils.WorthUtils;
import net.woggioni.worth.xface.Parser;
import net.woggioni.worth.xface.Value;

View File

@@ -1,34 +0,0 @@
package net.woggioni.worth.utils;
public class Chronometer {
public enum TimeUnit {
NANOSECOND(1e-9), MICROSECOND(1e-6), MILLISECOND(1e-3), SECOND(1);
private double factor;
TimeUnit(double factor) {
this.factor = factor;
}
}
private long start = System.nanoTime();
public void start() {
start = System.nanoTime();
}
public void reset() {
start();
}
public double stop(TimeUnit unit) {
return (System.nanoTime() - start) / (1e9 * unit.factor);
}
public double stop() {
return stop(TimeUnit.MILLISECOND);
}
}

View File

@@ -1,76 +0,0 @@
package net.woggioni.worth.utils;
import lombok.Getter;
import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import java.io.InputStream;
import java.io.OutputStream;
public class Leb128 {
public static long reverse(long n) {
long res = 0;
for(int i = 0; i < 8; i++) {
long b = (n & (0xFFL << (i * 8))) >>> (i * 8);
res |= b << ((7 - i) * 8);
}
return res;
}
public static int encode(OutputStream os, double input) {
return encode(os, reverse(Double.doubleToLongBits(input)));
}
@SneakyThrows
public static int encode(OutputStream os, long input) {
int bytes_written = 0;
long number = input >= 0 ? (input << 1) : (-(input + 1)) << 1 | 1;
while((number & 127L) != number) {
os.write((int) (number & 127) | 128);
bytes_written++;
number >>>= 7;
}
os.write((int) number);
return ++bytes_written;
}
@RequiredArgsConstructor
public static class Leb128Decoder {
@Getter
private int bytesRead = 0;
private final InputStream is;
public byte decodeByte() {
return (byte) decode();
}
public short decodeShort() {
return (short) decode();
}
public int decodeInt() {
return (int) decode();
}
public double decodeDouble() {
return Double.longBitsToDouble(reverse(decode()));
}
@SneakyThrows
public long decode() {
long res = 0;
for(int i = 0; i < (8 * 8 + 6) / 7; i++) {
int c = is.read();
bytesRead++;
if(c < 0) {
throw new IllegalArgumentException("Unexpected end of file");
}
res |= ((long)(c & 127)) << (i * 7);
if((byte) c >= 0) break;
}
return (res & 1) != 0 ? - (res >>> 1) - 1 : (res >>> 1);
}
}
}

View File

@@ -1,249 +0,0 @@
package net.woggioni.worth.utils;
import lombok.RequiredArgsConstructor;
import java.util.Arrays;
import java.util.Collection;
import java.util.Iterator;
import java.util.List;
import java.util.ListIterator;
import java.util.Objects;
import static java.lang.Math.min;
@RequiredArgsConstructor
public class ListView<T> implements List<T> {
private final List<T> delegate;
private final int start;
private final int end;
public ListView(List<T> delegate, int start) {
this(delegate, start, -1);
}
@Override
public int size() {
return end < 0 ? delegate.size() : min(end, delegate.size()) - start;
}
@Override
public boolean isEmpty() {
return size() == 0;
}
@Override
public boolean contains(Object o) {
Iterator<T> it = iterator();
while (it.hasNext()) {
if(Objects.equals(o, it.next())) {
return true;
}
}
return false;
}
@Override
public Iterator<T> iterator() {
return new Iterator<T>() {
int index = start;
@Override
public boolean hasNext() {
return end < 0 ? index < size() : index < min(end, size());
}
@Override
public T next() {
return get(index++);
}
};
}
@Override
public Object[] toArray() {
int size = size();
Object[] result = new Object[size];
for(int i = 0; i < size; i++) {
result[i] = get(i);
}
return result;
}
@Override
public <T1> T1[] toArray(T1[] t1s) {
int size = size();
T1[] result = Arrays.copyOf(t1s, size);
for(int i = 0; i < size; i++) {
result[i] = (T1) get(i);
}
return result;
}
@Override
public boolean add(T t) {
throw new UnsupportedOperationException();
}
@Override
public boolean remove(Object o) {
throw new UnsupportedOperationException();
}
@Override
public boolean containsAll(Collection<?> collection) {
return false;
}
@Override
public boolean addAll(Collection<? extends T> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean addAll(int i, Collection<? extends T> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean removeAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public boolean retainAll(Collection<?> collection) {
throw new UnsupportedOperationException();
}
@Override
public void clear() {
throw new UnsupportedOperationException();
}
@Override
public T get(int i) {
int index = start + i;
if(end >= 0 && index < end) {
throw new IndexOutOfBoundsException(Integer.toString(i));
}
return delegate.get(start + i);
}
@Override
public T set(int i, T t) {
throw new UnsupportedOperationException();
}
@Override
public void add(int i, T t) {
throw new UnsupportedOperationException();
}
@Override
public T remove(int i) {
throw new UnsupportedOperationException();
}
@Override
public int indexOf(Object o) {
int size = size();
for(int i = 0; i < size; i++) {
if(Objects.equals(o, get(i))) {
return i;
}
}
return -1;
}
@Override
public int lastIndexOf(Object o) {
int size = size();
for(int i = size - 1; i >= 0; i--) {
if(Objects.equals(o, get(i))) {
return i;
}
}
return -1;
}
@Override
public ListIterator<T> listIterator() {
return new ListViewIterator<>(this, 0);
}
@Override
public ListIterator<T> listIterator(int i) {
if(i < 0 || i > size()) {
throw new IndexOutOfBoundsException(Integer.toString(0));
} else {
return new ListViewIterator<>(this, start);
}
}
@Override
public List<T> subList(int i, int i1) {
if(i < 0) {
throw new IndexOutOfBoundsException(Integer.toString(0));
} else if(i1 > size()) {
throw new IndexOutOfBoundsException(Integer.toString(i1));
} else {
return new ListView<>(delegate, start + i, start + i1);
}
}
@RequiredArgsConstructor
private static class ListViewIterator<T> implements ListIterator<T> {
private final ListView<T> listView;
int size;
int i;
public ListViewIterator(ListView<T> listView, int start) {
this.listView = listView;
size = listView.size();
i = start;
}
@Override
public boolean hasNext() {
return i < size;
}
@Override
public T next() {
return listView.get(i++);
}
@Override
public boolean hasPrevious() {
return i > 0;
}
@Override
public T previous() {
return listView.get(--i);
}
@Override
public int nextIndex() {
return i + 1;
}
@Override
public int previousIndex() {
return i;
}
@Override
public void remove() {
throw new UnsupportedOperationException();
}
@Override
public void set(T t) {
throw new UnsupportedOperationException();
}
@Override
public void add(T t) {
throw new UnsupportedOperationException();
}
}
}

View File

@@ -1,11 +0,0 @@
package net.woggioni.worth.utils;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
@EqualsAndHashCode
@RequiredArgsConstructor
public class Tuple2<T, U> {
public final T _1;
public final U _2;
}

View File

@@ -4,7 +4,7 @@ import lombok.SneakyThrows;
import net.woggioni.jwo.JWO;
import net.woggioni.worth.serialization.json.JSONParser;
import net.woggioni.worth.serialization.json.JSONTest;
import net.woggioni.worth.utils.Tuple2;
import net.woggioni.jwo.tuple.Tuple2;
import net.woggioni.worth.value.ArrayValue;
import net.woggioni.worth.value.ObjectValue;
import net.woggioni.worth.xface.Parser;

View File

@@ -1,62 +0,0 @@
package net.woggioni.worth.utils;
import lombok.SneakyThrows;
import org.junit.Assert;
import org.junit.Test;
import java.io.ByteArrayInputStream;
import java.io.ByteArrayOutputStream;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;
public class Leb128Test {
@Test
public void testLong() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
List<Long> numbers = Arrays.asList(0L, 1L, -3L, 5L, 7L, 8L, 125L, 255L, 10325L, -2000L, 1024L * 1024L * 1024L * 12L);
numbers.forEach(n -> Leb128.encode(baos, n));
byte[] bytes = baos.toByteArray();
Leb128.Leb128Decoder decoder = new Leb128.Leb128Decoder(new ByteArrayInputStream(bytes));
numbers.forEach(n -> Assert.assertEquals((long) n, decoder.decode()));
}
@Test
public void testDouble() {
ByteArrayOutputStream baos = new ByteArrayOutputStream();
List<Double> numbers = Arrays.asList(
0.0, 1.5, -3.0, 0.5, 2.5, 8.25, -125.0, 255.0, 10325.0, -2000.0, 1024.0 * 1024 * 1024 * 12,
-122.42200352825247, 37.80848009696725);
numbers.forEach(n -> Leb128.encode(baos, n));
byte[] bytes = baos.toByteArray();
Leb128.Leb128Decoder decoder = new Leb128.Leb128Decoder(new ByteArrayInputStream(bytes));
numbers.forEach(n -> Assert.assertEquals(n, decoder.decodeDouble(), 0.0));
}
@Test
public void reverseTest() {
long n = 101325;
Assert.assertEquals(n, Leb128.reverse(Leb128.reverse(n)));
}
@Test
@SneakyThrows
public void reverseTestDouble() {
double n = 0.25;
long doubleLong = Double.doubleToLongBits(n);
long reverse = Leb128.reverse(doubleLong);
try(ByteArrayOutputStream os = new ByteArrayOutputStream()) {
Leb128.encode(os, reverse);
byte[] bytes = os.toByteArray();
Assert.assertEquals(3, bytes.length);
}
}
}

View File

@@ -1,26 +0,0 @@
package net.woggioni.worth.utils;
import lombok.EqualsAndHashCode;
import lombok.RequiredArgsConstructor;
import java.util.Comparator;
@EqualsAndHashCode
@RequiredArgsConstructor
public class Tuple2<T, U> {
public final T _1;
public final U _2;
public static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Tuple2<X, Y> tuple) {
return Comparator
.comparing((Tuple2<X, Y> t) -> t._1)
.thenComparing((Tuple2<X, Y> t) -> t._2);
}
public static <X extends Comparable<X>, Y extends Comparable<Y>> Comparator<Tuple2<X, Y>> getComparator(Class<X> cls1, Class<Y> cls2) {
return Comparator
.comparing((Tuple2<X, Y> t) -> t._1)
.thenComparing((Tuple2<X, Y> t) -> t._2);
}
}

View File

@@ -1,6 +1,6 @@
package net.woggioni.worth.value;
import net.woggioni.worth.utils.Tuple2;
import net.woggioni.jwo.tuple.Tuple2;
import net.woggioni.worth.xface.Value;
import org.junit.Assert;
import org.junit.Test;