avoid expensive string concatenation in frequently-called place

Change-Id: I86cde10a4c15ae7e2286bd8ea1d236cac89959ce
This commit is contained in:
Axel Uhl
2019-07-18 00:34:43 +02:00
parent b6e2b97712
commit a47d56988d
3 changed files with 23 additions and 10 deletions
@@ -10,6 +10,7 @@ import java.util.HashMap;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Supplier;
import org.junit.Before;
import org.junit.Test;
@@ -20,9 +21,9 @@ import com.sap.sailing.windestimation.aggregator.graph.ElementAdjacencyQualityMe
import com.sap.sailing.windestimation.aggregator.graph.ElementWithQuality;
import com.sap.sailing.windestimation.aggregator.graph.GroupOutOfWhichToPickTheBestElement;
import com.sap.sailing.windestimation.aggregator.graph.InnerGraphSuccessorSupplier;
import com.sap.sse.common.Named;
import com.sap.sse.common.Util;
import com.sap.sse.common.Util.Pair;
import com.sap.sse.common.impl.NamedImpl;
public class DijkstraTest {
private Node startNode;
@@ -30,13 +31,23 @@ public class DijkstraTest {
private Map<Node, Set<Node>> successors;
private Map<Pair<Node, Node>, Double> edgeQuality;
private static class Node extends NamedImpl implements ElementWithQuality {
private static final long serialVersionUID = 6000367519447493367L;
private static class Node implements ElementWithQuality, Named {
private static final long serialVersionUID = 4495016215564212071L;
private final double quality;
private final Supplier<String> nameSupplier;
public Node(String name, double quality) {
super(name);
this(()->name, quality);
}
public Node(Supplier<String> nameSupplier, double quality) {
this.quality = quality;
this.nameSupplier = nameSupplier;
}
@Override
public String getName() {
return nameSupplier.get();
}
@Override
@@ -6,6 +6,7 @@ import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import java.util.function.Function;
import java.util.function.Supplier;
import com.sap.sse.common.Util;
@@ -31,7 +32,7 @@ implements Function<T, Iterable<T>> {
private final Map<T, Set<T>> successors;
private final T artificialRoot;
private final Map<G, T> artificialLeaves;
private final Function<String, T> artificialInnerNodeConstructor;
private final Function<Supplier<String>, T> artificialInnerNodeConstructor;
/**
* Constructs the inner graph's edge structure from the {@code overarchingTree}.
@@ -54,10 +55,10 @@ implements Function<T, Iterable<T>> {
* quality} of the element created is expected to be set to the same value for all invocations, e.g.,
* {@code 1.0}.
*/
public InnerGraphSuccessorSupplier(Tree<G> overarchingTree, Function<String, T> artificialInnerNodeConstructor) {
public InnerGraphSuccessorSupplier(Tree<G> overarchingTree, Function<Supplier<String>, T> artificialInnerNodeConstructor) {
successors = new HashMap<>();
this.artificialInnerNodeConstructor = artificialInnerNodeConstructor;
artificialRoot = artificialInnerNodeConstructor.apply("End Node at Root");
artificialRoot = artificialInnerNodeConstructor.apply(()->"End Node at Root");
artificialLeaves = new HashMap<>();
if (overarchingTree.getRoot() != null) {
// add the edges to the artificial root node
@@ -72,7 +73,7 @@ implements Function<T, Iterable<T>> {
final Iterable<T> innerChildElements;
if (Util.isEmpty(node.getChildren())) {
// leaf node; add an artificial leaf:
final T artificialLeaf = artificialInnerNodeConstructor.apply("Start Node for "+node);
final T artificialLeaf = artificialInnerNodeConstructor.apply(()->"Start Node for "+node);
artificialLeaves.put(node, artificialLeaf);
innerChildElements = Collections.singleton(artificialLeaf);
} else {
@@ -8,6 +8,7 @@ import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.function.Supplier;
import java.util.stream.Collectors;
import com.sap.sailing.windestimation.aggregator.graph.DijkstraShortestPathFinderImpl;
@@ -129,10 +130,10 @@ public class MstBestPathsCalculatorImpl implements MstBestPathsCalculator {
final InnerGraphSuccessorSupplier<GraphNode<MstGraphLevel>, MstGraphLevel> innerGraphSuccessorSupplier =
new InnerGraphSuccessorSupplier<GraphNode<MstGraphLevel>, MstGraphLevel>(graphComponents,
// supplier for artificial nodes; always full confidence and full possible wind course range
(final String name)->new GraphNode<MstGraphLevel>(/* maneuverType */ null, /* tackAfter */ null, new WindCourseRange(0, 360), /* confidence */ 1.0, /* indexInLevel */ 0, /* graphLevel */ null) {
(final Supplier<String> nameSupplier)->new GraphNode<MstGraphLevel>(/* maneuverType */ null, /* tackAfter */ null, new WindCourseRange(0, 360), /* confidence */ 1.0, /* indexInLevel */ 0, /* graphLevel */ null) {
@Override
public String toString() {
return name;
return nameSupplier.get();
}
});
final DijsktraShortestPathFinder<GraphNode<MstGraphLevel>> dijsktraShortestPathFinder = new DijkstraShortestPathFinderImpl<GraphNode<MstGraphLevel>>(