Skip to content

Commit

Permalink
Performance tunning
Browse files Browse the repository at this point in the history
  • Loading branch information
wanglingsong committed Nov 5, 2015
1 parent 706b0dd commit e53ebc9
Show file tree
Hide file tree
Showing 6 changed files with 66 additions and 124 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,9 +51,6 @@
import java.util.Iterator;
import java.util.concurrent.TimeUnit;

/**
* Created by lwang on 16/7/2015.
*/
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Threads(1)
Expand Down Expand Up @@ -85,23 +82,23 @@ public void setup() throws Exception {
}

@Benchmark
public Object benchmarkGsonSurfer() {
public Object benchmarkGsonWithJsonSurfer() {
gsonSurfer.surf(json, surfingConfiguration);
Object value = collectOneListener.getValue();
LOGGER.trace("The author of the first book: {}", value);
return value;
}

@Benchmark
public Object benchmarkJacksonSurfer() {
public Object benchmarkJacksonWithJsonSurfer() {
jacksonSurfer.surf(json, surfingConfiguration);
Object value = collectOneListener.getValue();
LOGGER.trace("The author of the first book: {}", value);
return value;
}

@Benchmark
public Object benchmarkSimpleSurfer() {
public Object benchmarkJsonSimpleWithJsonSurfer() {
simpleSurfer.surf(json, surfingConfiguration);
Object value = collectOneListener.getValue();
LOGGER.trace("The author of the first book: {}", value);
Expand All @@ -128,7 +125,7 @@ public Object benchmarkJackson() throws IOException {
public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(BenchmarkCollectSingleValue.class.getSimpleName())
.addProfiler(FlightRecordingProfiler.class)
// .addProfiler(FlightRecordingProfiler.class)
.build();
new Runner(opt).run();
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -47,9 +47,6 @@
import java.nio.charset.StandardCharsets;
import java.util.concurrent.TimeUnit;

/**
* Created by lwang on 2015/7/16 0016.
*/
@Warmup(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Measurement(iterations = 10, time = 1, timeUnit = TimeUnit.SECONDS)
@Threads(1)
Expand Down Expand Up @@ -85,27 +82,27 @@ public void onValue(Object value, ParsingContext context) throws Exception {
}

@Benchmark
public Object benchmarkSimpleSufer() {
public Object benchmarkJsonSimpleWithJsonSurfer() {
simpleSurfer.surf(json, surfingConfiguration);
return null;
}

@Benchmark
public Object benchmarkGsonSufer() {
public Object benchmarkGsonWithJsonSurfer() {
gsonSurfer.surf(json, surfingConfiguration);
return null;
}

@Benchmark
public Object benchmarkJacksonSufer() {
public Object benchmarkJacksonWithJsonSurfer() {
jacksonSurfer.surf(json, surfingConfiguration);
return null;
}

public static void main(String[] args) throws RunnerException {
Options opt = new OptionsBuilder()
.include(BenchmarkParseLargeJson.class.getSimpleName())
.addProfiler(FlightRecordingProfiler.class)
// .addProfiler(FlightRecordingProfiler.class)
.build();
new Runner(opt).run();
}
Expand Down
16 changes: 16 additions & 0 deletions jsurfer-core/src/main/java/org/jsfr/json/JsonPosition.java
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,14 @@ static JsonPosition start() {
}

void stepIntoObject() {
if (operators.length > size) {
PathOperator next = operators[size];
if (next instanceof ChildNode) {
size++;
((ChildNode) next).setKey(null);
return;
}
}
push(new ChildNode(null));
}

Expand All @@ -48,6 +56,14 @@ void stepOutObject() {
}

void stepIntoArray() {
if (operators.length > size) {
PathOperator next = operators[size];
if (next instanceof ArrayIndex) {
size++;
((ArrayIndex) next).reset();
return;
}
}
push(new ArrayIndex());
}

Expand Down
72 changes: 36 additions & 36 deletions jsurfer-core/src/main/java/org/jsfr/json/path/JsonPath.java
Original file line number Diff line number Diff line change
Expand Up @@ -32,20 +32,20 @@

public class JsonPath implements Iterable<PathOperator> {

private static final int JSON_PATH_INITIAL_CAPACITY = 20;

private class JsonPathIterator implements Iterator<PathOperator> {

private JsonPathNode current = head;
private int current = 0;

@Override
public boolean hasNext() {
return current != null;
return current < size;
}

@Override
public PathOperator next() {
PathOperator op = current.operator();
current = current.next();
return op;
return operators[current++];
}

@Override
Expand Down Expand Up @@ -139,13 +139,12 @@ public JsonPath build() {
private boolean definite = true;
private int minimumDepth = 0;

protected JsonPathNode head;
protected JsonPathNode tail;
protected PathOperator[] operators;
protected int size;

protected JsonPath() {
head = new JsonPathNode(null, Root.instance());
tail = head;
operators = new PathOperator[JSON_PATH_INITIAL_CAPACITY];
operators[0] = Root.instance();
size = 1;
}

Expand All @@ -161,61 +160,62 @@ public Object resolve(Object document, DocumentResolver resolver) {
}

public boolean match(JsonPath jsonPath) {
JsonPathNode pointer1 = this.tail;
JsonPathNode pointer2 = jsonPath.tail;
if (!pointer1.operator().match(pointer2.operator())) {
int pointer1 = this.size - 1;
int pointer2 = jsonPath.size - 1;
if (!get(pointer1).match(jsonPath.get(pointer2))) {
return false;
}
while (pointer1.hasPrevious()) {
if (!pointer2.hasPrevious()) {
while (pointer1 >= 0) {
if (!(pointer2 >= 0)) {
return false;
}
PathOperator o1 = (pointer1 = pointer1.previous()).operator();
PathOperator o2 = (pointer2 = pointer2.previous()).operator();
PathOperator o1 = this.get(pointer1--);
PathOperator o2 = jsonPath.get(pointer2--);
if (o1.getType() == PathOperator.Type.DEEP_SCAN) {
PathOperator prevScan = (pointer1 = pointer1.previous()).operator();
while (!prevScan.match(o2) && pointer2.hasPrevious()) {
o2 = (pointer2 = pointer2.previous()).operator();
PathOperator prevScan = this.get(pointer1--);
while (!prevScan.match(o2) && pointer2 >= 0) {
o2 = jsonPath.get(pointer2--);
}
} else {
if (!o1.match(o2)) {
return false;
}
}
}
return !pointer2.hasPrevious();
return !(pointer2 >= 0);
}

public PathOperator get(int i) {
return operators[i];
}

public PathOperator peek() {
return tail.operator();
return operators[size - 1];
}

protected void push(PathOperator operator) {
this.tail = this.tail.createNext(operator);
this.size++;
ensureCapacity(size + 1);
operators[size++] = operator;
}

private void ensureCapacity(int capacity) {
if (operators.length < capacity) {
PathOperator[] newOperators = new PathOperator[operators.length * 2];
System.arraycopy(operators, 0, newOperators, 0, operators.length);
operators = newOperators;
}
}

protected PathOperator pop() {
JsonPathNode top = tail;
tail = top.previous();
tail.resetNext();
PathOperator topOperator = top.operator();
top.reset();
this.size--;
return topOperator;
return operators[--size];
}

public int pathDepth() {
return this.size;
}

public void clear() {
while (tail.hasPrevious()) {
pop();
}
tail.reset();
head = null;
tail = null;
operators = null;
}

public int minimumPathDepth() {
Expand Down
74 changes: 0 additions & 74 deletions jsurfer-core/src/main/java/org/jsfr/json/path/JsonPathNode.java

This file was deleted.

Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,10 @@ public void shallResolvePOJO() throws Exception {
assertEquals("bar", compile("$.list[1]").resolve(map, JavaCollectionProvider.INSTANCE));
}

@Test
public void shallEnsureJsonPathCapacity() throws Exception {
JsonPath path = compile("$.a.b.c.d.e.f.g.h.a.b.c.d.e.f.g.h.a.b.c.d.e.f.g.h.a.b.c.d.e.f.g.h.a.b.c.d.e.f.g.h");
assertEquals(41, path.size);
}

}

0 comments on commit e53ebc9

Please sign in to comment.