diff --git a/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaCompilationUnitState.java b/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaCompilationUnitState.java index a8847d87163..8ccaecd0803 100644 --- a/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaCompilationUnitState.java +++ b/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaCompilationUnitState.java @@ -41,7 +41,6 @@ public class JavaCompilationUnitState { JavaParser.Builder javaParser; List sourceFiles; List inputs; - JavaTypeCache snappyTypeCache; AdaptiveRadixJavaTypeCache radixMapTypeCache; MapJavaTypeCache typeCache; @@ -103,18 +102,13 @@ public void setup() throws URISyntaxException { for (Map.Entry entry : typeCache.map().entrySet()) { radixMapTypeCache.put(entry.getKey(), entry.getValue()); } - - snappyTypeCache = new JavaTypeCache(); - for (Map.Entry entry : typeCache.map().entrySet()) { - snappyTypeCache.put(entry.getKey(), entry.getValue()); - } } void printMemory() { long retainedSize = GraphLayout.parseInstance(radixMapTypeCache).totalSize(); System.out.printf("Retained AdaptiveRadixTree size: %10d bytes\n", retainedSize); - retainedSize = GraphLayout.parseInstance(snappyTypeCache).totalSize(); - System.out.printf("Retained Snappy size: %10d bytes\n", retainedSize); + retainedSize = GraphLayout.parseInstance(typeCache).totalSize(); + System.out.printf("Retained HashMap size: %10d bytes\n", retainedSize); } @TearDown(Level.Trial) @@ -154,11 +148,6 @@ public void clear() { typeCache.clear(); } - @Override - public int size() { - return typeCache.size(); - } - @Override public MapJavaTypeCache clone() { MapJavaTypeCache clone = (MapJavaTypeCache) super.clone(); diff --git a/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaTypeCacheBenchmark.java b/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaTypeCacheBenchmark.java index 42a5da8d16e..ae6df2a3db9 100644 --- a/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaTypeCacheBenchmark.java +++ b/rewrite-benchmarks/src/jmh/java/org/openrewrite/benchmarks/java/JavaTypeCacheBenchmark.java @@ -38,8 +38,8 @@ public class JavaTypeCacheBenchmark { @Benchmark - public void writeSnappy(JavaCompilationUnitState state, Blackhole bh) { - JavaTypeCache typeCache = new JavaTypeCache(); + public void writeHashMap(JavaCompilationUnitState state, Blackhole bh) { + JavaTypeCache typeCache = new JavaCompilationUnitState.MapJavaTypeCache(); for (Map.Entry entry : state.typeCache.map().entrySet()) { typeCache.put(entry.getKey(), entry.getValue()); } @@ -54,9 +54,9 @@ public void writeAdaptiveRadix(JavaCompilationUnitState state, Blackhole bh) { } @Benchmark - public void readSnappy(JavaCompilationUnitState state, Blackhole bh) { + public void readHashMap(JavaCompilationUnitState state, Blackhole bh) { for (Map.Entry entry : state.typeCache.map().entrySet()) { - bh.consume(state.snappyTypeCache.get(entry.getKey())); + bh.consume(state.typeCache.get(new String(entry.getKey()))); } } diff --git a/rewrite-java/build.gradle.kts b/rewrite-java/build.gradle.kts index 3af7ee05274..5d0f0aab3c1 100644 --- a/rewrite-java/build.gradle.kts +++ b/rewrite-java/build.gradle.kts @@ -50,8 +50,6 @@ dependencies { implementation("org.apache.commons:commons-text:latest.release") implementation("io.github.classgraph:classgraph:latest.release") - implementation("org.xerial.snappy:snappy-java:1.1.10.+") - api("com.fasterxml.jackson.core:jackson-annotations") // these are required for now so that `ChangeType` and `ChangePackage` can use the `Reference` trait diff --git a/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaTypeCache.java b/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaTypeCache.java index 16bb1c6ab36..b893f057432 100644 --- a/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaTypeCache.java +++ b/rewrite-java/src/main/java/org/openrewrite/java/internal/JavaTypeCache.java @@ -15,69 +15,31 @@ */ package org.openrewrite.java.internal; -import lombok.Value; import org.jspecify.annotations.Nullable; -import org.xerial.snappy.Snappy; - -import java.io.IOException; -import java.io.UncheckedIOException; -import java.nio.charset.StandardCharsets; -import java.util.HashMap; -import java.util.Map; +import org.openrewrite.internal.AdaptiveRadixTree; public class JavaTypeCache implements Cloneable { - // empirical value: below this size, the compressed key is larger or only slightly smaller - // although also note that a String object has a 24 bytes overhead vs. the 16 bytes of a BytesKey object - public static final int COMPRESSION_THRESHOLD = 50; - - @SuppressWarnings("ClassCanBeRecord") - @Value - private static class BytesKey { - byte[] data; - } - - Map typeCache = new HashMap<>(); + AdaptiveRadixTree typeCache = new AdaptiveRadixTree<>(); public @Nullable T get(String signature) { //noinspection unchecked - return (T) typeCache.get(key(signature)); + return (T) typeCache.search(signature); } public void put(String signature, Object o) { - typeCache.put(key(signature), o); - } - - @Nullable - private static boolean snappyUsable = true; - - private Object key(String signature) { - if (signature.length() > COMPRESSION_THRESHOLD && snappyUsable) { - try { - return new BytesKey(Snappy.compress(signature.getBytes(StandardCharsets.UTF_8))); - } catch (IOException e) { - throw new UncheckedIOException(e); - } catch (NoClassDefFoundError e) { - // Some systems fail to load Snappy native components, so fall back to not compressing - snappyUsable = false; - } - } - return signature; + typeCache.insert(signature, o); } public void clear() { typeCache.clear(); } - public int size() { - return typeCache.size(); - } - @Override public JavaTypeCache clone() { try { JavaTypeCache clone = (JavaTypeCache) super.clone(); - clone.typeCache = new HashMap<>(this.typeCache); + clone.typeCache = this.typeCache.copy(); return clone; } catch (CloneNotSupportedException e) { throw new RuntimeException(e);