From 405cd6559e0f9b6a9c14bdbf2c5e1e0ac5fca35e Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Sat, 2 Nov 2024 15:33:25 +0100 Subject: [PATCH 1/3] Verify `AdaptiveRadixTree` in `JavaTypeCache` --- .../java/internal/JavaTypeCache.java | 48 ++----------------- 1 file changed, 5 insertions(+), 43 deletions(-) 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); From d02c3b53e1beb44511fed676ad174e14233c6782 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Sat, 2 Nov 2024 15:35:44 +0100 Subject: [PATCH 2/3] Remove Snappy --- rewrite-java/build.gradle.kts | 2 -- 1 file changed, 2 deletions(-) diff --git a/rewrite-java/build.gradle.kts b/rewrite-java/build.gradle.kts index f84627b4c2b..d306e5312e6 100644 --- a/rewrite-java/build.gradle.kts +++ b/rewrite-java/build.gradle.kts @@ -51,8 +51,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") implementation("org.ow2.asm:asm:latest.release") From 0a1645e4eeeb1b98175b5820fe250ea8af0e70f7 Mon Sep 17 00:00:00 2001 From: Knut Wannheden Date: Mon, 4 Nov 2024 10:43:27 +0100 Subject: [PATCH 3/3] Fix benchmark code --- .../benchmarks/java/JavaCompilationUnitState.java | 15 ++------------- .../benchmarks/java/JavaTypeCacheBenchmark.java | 8 ++++---- 2 files changed, 6 insertions(+), 17 deletions(-) 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 a79b9aaead9..6671c585cd5 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()))); } }