Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced stream with for loop for slightly better performance #35

Merged
merged 5 commits into from
Nov 8, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions hope-core/src/main/java/io/appform/hope/core/utils/Converters.java
Original file line number Diff line number Diff line change
Expand Up @@ -25,12 +25,25 @@
import io.appform.hope.core.exceptions.errorstrategy.ErrorHandlingStrategy;
import io.appform.hope.core.functions.FunctionRegistry;
import io.appform.hope.core.functions.HopeFunction;
import io.appform.hope.core.values.*;
import io.appform.hope.core.values.ArrayValue;
import io.appform.hope.core.values.BooleanValue;
import io.appform.hope.core.values.FunctionValue;
import io.appform.hope.core.values.JsonPathValue;
import io.appform.hope.core.values.JsonPointerValue;
import io.appform.hope.core.values.NumericValue;
import io.appform.hope.core.values.ObjectValue;
import io.appform.hope.core.values.StringValue;
import io.appform.hope.core.visitors.Evaluator;
import lombok.extern.slf4j.Slf4j;
import lombok.val;

import java.lang.reflect.Constructor;
import java.util.*;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Spliterator;
import java.util.Spliterators;
import java.util.function.Function;
import java.util.function.Supplier;
import java.util.stream.StreamSupport;
Expand Down Expand Up @@ -705,10 +718,12 @@ private static List<Object> arrayToObjectList(
Evaluator.EvaluationContext evaluationContext,
List<Value> values,
Object defaultValue) {
return values
.stream()
.map(value -> objectValue(evaluationContext, value, defaultValue))
.toList();
val list = new ArrayList<>();
for (final Value value : values) {
val o = objectValue(evaluationContext, value, defaultValue);
list.add(o);
}
return list;
}

}
Original file line number Diff line number Diff line change
Expand Up @@ -17,24 +17,39 @@
import com.fasterxml.jackson.annotation.JsonInclude;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.jayway.jsonpath.*;
import com.jayway.jsonpath.Configuration;
import com.jayway.jsonpath.DocumentContext;
import com.jayway.jsonpath.JsonPath;
import com.jayway.jsonpath.Option;
import com.jayway.jsonpath.ParseContext;
import com.jayway.jsonpath.spi.json.JacksonJsonNodeJsonProvider;
import io.appform.hope.core.Evaluatable;
import io.appform.hope.core.VisitorAdapter;
import io.appform.hope.core.combiners.AndCombiner;
import io.appform.hope.core.combiners.OrCombiner;
import io.appform.hope.core.exceptions.errorstrategy.DefaultErrorHandlingStrategy;
import io.appform.hope.core.exceptions.errorstrategy.ErrorHandlingStrategy;
import io.appform.hope.core.operators.*;
import io.appform.hope.core.operators.And;
import io.appform.hope.core.operators.Equals;
import io.appform.hope.core.operators.Greater;
import io.appform.hope.core.operators.GreaterEquals;
import io.appform.hope.core.operators.Lesser;
import io.appform.hope.core.operators.LesserEquals;
import io.appform.hope.core.operators.Not;
import io.appform.hope.core.operators.NotEquals;
import io.appform.hope.core.operators.Or;
import io.appform.hope.core.utils.Converters;
import lombok.Builder;
import lombok.Data;
import lombok.Getter;
import lombok.val;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.OptionalInt;

/**
* Evaluates a hope expression
Expand All @@ -58,9 +73,9 @@ public Evaluator() {
public Evaluator(ErrorHandlingStrategy errorHandlingStrategy) {
this.errorHandlingStrategy = errorHandlingStrategy;
parseContext = JsonPath.using(Configuration.builder()
.jsonProvider(new JacksonJsonNodeJsonProvider())
.options(Option.SUPPRESS_EXCEPTIONS)
.build());
.jsonProvider(new JacksonJsonNodeJsonProvider())
.options(Option.SUPPRESS_EXCEPTIONS)
.build());

}

Expand All @@ -72,18 +87,25 @@ public List<Boolean> evaluate(
final List<Evaluatable> evaluatables,
final JsonNode node) {
val logicEvaluator = new LogicEvaluator(new EvaluationContext(parseContext.parse(node), node, this));
return evaluatables.stream()
.map(evaluatable -> evaluatable.accept(logicEvaluator))
.collect(Collectors.toList());
val list = new ArrayList<Boolean>();
for (final Evaluatable evaluatable : evaluatables) {
Boolean accept = evaluatable.accept(logicEvaluator);
list.add(accept);
}
return list;
}

public OptionalInt evaluateFirst(
final List<Evaluatable> rules,
final JsonNode node) {
val logicEvaluator = new LogicEvaluator(new EvaluationContext(parseContext.parse(node), node, this));
return IntStream.range(0, rules.size())
.filter(index -> rules.get(index).accept(logicEvaluator))
.findFirst();
val bound = rules.size();
for (int index = 0; index < bound; index++) {
if (rules.get(index).accept(logicEvaluator)) {
return OptionalInt.of(index);
}
}
return OptionalInt.empty();
}

@Data
Expand Down
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name" : "io.appform.hope.lang.JsonPathPerfTest.testBulkEval",
"mode" : "Throughput",
"iterations" : 2,
"iterations" : 3,
"threads" : 1,
"forks" : 3,
"mean_ops" : 44851.40995773682
"forks" : 2,
"mean_ops" : 98359.40086170378
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name" : "io.appform.hope.lang.JsonPathPerfTest.testSingleEval",
"mode" : "Throughput",
"iterations" : 2,
"iterations" : 3,
"threads" : 1,
"forks" : 3,
"mean_ops" : 5292.365475932045
"forks" : 2,
"mean_ops" : 12628.986025477061
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name" : "io.appform.hope.lang.JsonPointerPerfTest.testPerfBulkEval",
"mode" : "Throughput",
"iterations" : 2,
"iterations" : 3,
"threads" : 1,
"forks" : 3,
"mean_ops" : 50739.771145738465
"forks" : 2,
"mean_ops" : 116812.50719529158
}
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
{
"name" : "io.appform.hope.lang.JsonPointerPerfTest.testPerfSingleEval",
"mode" : "Throughput",
"iterations" : 2,
"iterations" : 3,
"threads" : 1,
"forks" : 3,
"mean_ops" : 14457.761592563897
"forks" : 2,
"mean_ops" : 38475.51902529204
}
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,9 @@ void testBenchmark() throws RunnerException {
.warmupTime(TimeValue.seconds(5))
.warmupIterations(2)
.measurementTime(TimeValue.seconds(10))
.measurementIterations(2)
.measurementIterations(3)
.threads(1)
.forks(3)
.forks(2)
.shouldFailOnError(true)
.shouldDoGC(true)
.build();
Expand Down
Loading