|
2 | 2 |
|
3 | 3 | import java.util.ArrayList;
|
4 | 4 | import java.util.LinkedHashMap;
|
| 5 | +import java.util.List; |
5 | 6 | import java.util.Map;
|
6 | 7 | import java.util.concurrent.TimeUnit;
|
7 | 8 |
|
| 9 | +import com.fasterxml.jackson.core.type.TypeReference; |
| 10 | +import com.fasterxml.jackson.databind.JavaType; |
| 11 | +import com.fasterxml.jackson.databind.util.LookupCache; |
8 | 12 | import org.openjdk.jmh.annotations.Benchmark;
|
9 | 13 | import org.openjdk.jmh.annotations.OutputTimeUnit;
|
| 14 | +import org.openjdk.jmh.annotations.Param; |
10 | 15 | import org.openjdk.jmh.annotations.Scope;
|
| 16 | +import org.openjdk.jmh.annotations.Setup; |
11 | 17 | import org.openjdk.jmh.annotations.State;
|
12 |
| -import org.openjdk.jmh.infra.Blackhole; |
13 | 18 |
|
14 | 19 | import com.fasterxml.jackson.databind.type.TypeFactory;
|
15 | 20 |
|
16 |
| -@State(Scope.Thread) |
| 21 | +@State(Scope.Benchmark) |
17 | 22 | public class JavaTypeResolution
|
18 | 23 | {
|
| 24 | + |
| 25 | + @Param |
| 26 | + public CacheMode cache; |
| 27 | + private TypeFactory tf; |
| 28 | + |
| 29 | + @Setup |
| 30 | + public void setup() { |
| 31 | + tf = cache.apply(TypeFactory.defaultInstance().withModifier(null)); |
| 32 | + } |
| 33 | + |
19 | 34 | @Benchmark
|
20 | 35 | @OutputTimeUnit(TimeUnit.SECONDS)
|
21 |
| - public Object resolveArrayList(Blackhole bh) throws Exception { |
22 |
| - TypeFactory tf = TypeFactory.defaultInstance().withModifier(null); |
| 36 | + public JavaType resolveArrayList() { |
23 | 37 | return tf.constructType(ArrayList.class);
|
24 | 38 | }
|
25 | 39 |
|
26 | 40 | @Benchmark
|
27 | 41 | @OutputTimeUnit(TimeUnit.SECONDS)
|
28 |
| - public Object resolveMapAny(Blackhole bh) throws Exception { |
29 |
| - TypeFactory tf = TypeFactory.defaultInstance().withModifier(null); |
| 42 | + public JavaType resolveMapAny() { |
30 | 43 | return tf.constructType(Map.class);
|
31 | 44 | }
|
32 | 45 |
|
33 | 46 | @Benchmark
|
34 | 47 | @OutputTimeUnit(TimeUnit.SECONDS)
|
35 |
| - public Object resolveMapLinkedHash(Blackhole bh) throws Exception { |
36 |
| - TypeFactory tf = TypeFactory.defaultInstance().withModifier(null); |
| 48 | + public JavaType resolveMapLinkedHash() { |
37 | 49 | return tf.constructType(LinkedHashMap.class);
|
38 | 50 | }
|
39 | 51 |
|
40 | 52 | @Benchmark
|
41 | 53 | @OutputTimeUnit(TimeUnit.SECONDS)
|
42 |
| - public Object resolveObject(Blackhole bh) throws Exception { |
43 |
| - TypeFactory tf = TypeFactory.defaultInstance().withModifier(null); |
| 54 | + public JavaType resolveObject() { |
44 | 55 | return tf.constructType(Object.class);
|
45 | 56 | }
|
| 57 | + |
| 58 | + @Benchmark |
| 59 | + @OutputTimeUnit(TimeUnit.SECONDS) |
| 60 | + public JavaType constructSpecializedType() { |
| 61 | + JavaType listOfCustomObject = tf.constructType(new TypeReference<List<CustomObject>>() {}); |
| 62 | + return tf.constructSpecializedType(listOfCustomObject, CustomList.class); |
| 63 | + } |
| 64 | + |
| 65 | + public static final class CustomObject {} |
| 66 | + |
| 67 | + public static final class CustomList<U> extends ArrayList<U> {} |
| 68 | + |
| 69 | + public enum CacheMode { |
| 70 | + @SuppressWarnings("unused") // used by JMH |
| 71 | + DEFAULT() { |
| 72 | + @Override |
| 73 | + TypeFactory apply(TypeFactory input) { |
| 74 | + return input; |
| 75 | + } |
| 76 | + }, |
| 77 | + @SuppressWarnings("unused") // used by JMH |
| 78 | + NONE() { |
| 79 | + @Override |
| 80 | + TypeFactory apply(TypeFactory input) { |
| 81 | + return input.withCache(NoCacheLookupCache.INSTANCE); |
| 82 | + } |
| 83 | + }; |
| 84 | + |
| 85 | + abstract TypeFactory apply(TypeFactory input); |
| 86 | + } |
| 87 | + |
| 88 | + private enum NoCacheLookupCache implements LookupCache<Object, JavaType> { |
| 89 | + INSTANCE; |
| 90 | + |
| 91 | + @Override |
| 92 | + public int size() { |
| 93 | + return 0; |
| 94 | + } |
| 95 | + |
| 96 | + @Override |
| 97 | + public JavaType get(Object o) { |
| 98 | + return null; |
| 99 | + } |
| 100 | + |
| 101 | + @Override |
| 102 | + public JavaType put(Object o, JavaType javaType) { |
| 103 | + return null; |
| 104 | + } |
| 105 | + |
| 106 | + @Override |
| 107 | + public JavaType putIfAbsent(Object o, JavaType javaType) { |
| 108 | + return null; |
| 109 | + } |
| 110 | + |
| 111 | + @Override |
| 112 | + public void clear() {} |
| 113 | + } |
46 | 114 | }
|
0 commit comments