Last active
January 14, 2019 21:48
-
-
Save matriv/3d2557ae1621e99a3c9505b5e4e6998f to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
package org.sample; | |
import org.openjdk.jmh.annotations.Benchmark; | |
import org.openjdk.jmh.annotations.Scope; | |
import org.openjdk.jmh.annotations.State; | |
import org.openjdk.jmh.infra.Blackhole; | |
import java.util.ArrayDeque; | |
import java.util.Deque; | |
import java.util.HashMap; | |
import java.util.LinkedList; | |
import java.util.Map; | |
import java.util.StringJoiner; | |
import static java.util.Collections.singletonMap; | |
public class MyBenchmark { | |
@State(Scope.Benchmark) | |
public static class MyMap { | |
static Map<String, Object> map; | |
static { | |
Object value = "Marios"; | |
map = new HashMap<>(); | |
map.put("a", singletonMap("b", singletonMap("c", singletonMap("d", singletonMap("e", singletonMap("f", singletonMap("g", value))))))); | |
map.put("a.b", singletonMap("c.d", singletonMap("e", singletonMap("f", singletonMap("g", value))))); | |
map.put("a.b.c", singletonMap("b", singletonMap("c.d.e", singletonMap("f", singletonMap("g", value))))); | |
map.put("a.b.c.d", singletonMap("b", singletonMap("c.d.e.f", singletonMap("g", value)))); | |
map.put("a.b.c.d.e", singletonMap("b", singletonMap("c.d.e.f.g", value))); | |
map.put("a.b.c.d.e.f", singletonMap("b", singletonMap("c", singletonMap("d.e", singletonMap("f", singletonMap("g", value)))))); | |
map.put("a.b.c.d.e.f.g", singletonMap("b", singletonMap("c", singletonMap("d.e.f", singletonMap("g", value))))); | |
} | |
} | |
@Benchmark | |
public void testNoTuples(Blackhole bh) { | |
bh.consume(extractFromSource(new String[] {"a", "b", "c", "d", "e", "f", "g"}, MyMap.map)); | |
} | |
@Benchmark | |
public void testTuples(Blackhole bh) { | |
bh.consume(extractFromSourceTuples(new String[] {"a", "b", "c", "d", "e", "f", "g"}, MyMap.map)); | |
} | |
private static Object extractFromSource(String[] path, Map<String, Object> map) { | |
Object value = null; | |
Deque<Map<String, Object>> queue = new ArrayDeque<>(); | |
Deque<Integer> idxQueue = new ArrayDeque<>(); | |
queue.add(map); | |
idxQueue.add(-1); | |
while (!queue.isEmpty()) { | |
int idx = idxQueue.removeLast(); | |
Map<String, Object> subMap = queue.removeLast(); | |
StringJoiner sj = new StringJoiner("."); | |
for (int i = idx + 1; i < path.length; i++) { | |
sj.add(path[i]); | |
Object node = subMap.get(sj.toString()); | |
if (node instanceof Map) { | |
queue.add((Map<String, Object>) node); | |
idxQueue.add(i); | |
} else if (node != null) { | |
value = node; | |
} | |
} | |
} | |
return value; | |
} | |
private static Object extractFromSourceTuples(String[] path, Map<String, Object> map) { | |
Object value = null; | |
Deque<Tuple<Integer, Map<String, Object>>> queue = new LinkedList<>(); | |
queue.add(new Tuple<>(-1, map)); | |
while (!queue.isEmpty()) { | |
Tuple<Integer, Map<String, Object>> tuple = queue.removeLast(); | |
int idx = tuple.v1(); | |
Map<String, Object> subMap = tuple.v2(); | |
StringJoiner sj = new StringJoiner("."); | |
for (int i = idx + 1; i < path.length; i++) { | |
sj.add(path[i]); | |
Object node = subMap.get(sj.toString()); | |
if (node instanceof Map) { | |
queue.add(new Tuple<>(i, (Map<String, Object>) node)); | |
} else if (node != null) { | |
value = node; | |
} | |
} | |
} | |
return value; | |
} | |
public static class Tuple<V1, V2> { | |
public static <V1, V2> Tuple<V1, V2> tuple(V1 v1, V2 v2) { | |
return new Tuple<>(v1, v2); | |
} | |
private final V1 v1; | |
private final V2 v2; | |
public Tuple(V1 v1, V2 v2) { | |
this.v1 = v1; | |
this.v2 = v2; | |
} | |
public V1 v1() { | |
return v1; | |
} | |
public V2 v2() { | |
return v2; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment