Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Verify AdaptiveRadixTree in JavaTypeCache #4641

Open
wants to merge 4 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ public class JavaCompilationUnitState {
JavaParser.Builder<? extends JavaParser, ?> javaParser;
List<SourceFile> sourceFiles;
List<Path> inputs;
JavaTypeCache snappyTypeCache;
AdaptiveRadixJavaTypeCache radixMapTypeCache;
MapJavaTypeCache typeCache;

Expand Down Expand Up @@ -103,18 +102,13 @@ public void setup() throws URISyntaxException {
for (Map.Entry<String, Object> entry : typeCache.map().entrySet()) {
radixMapTypeCache.put(entry.getKey(), entry.getValue());
}

snappyTypeCache = new JavaTypeCache();
for (Map.Entry<String, Object> 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)
Expand Down Expand Up @@ -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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, Object> entry : state.typeCache.map().entrySet()) {
typeCache.put(entry.getKey(), entry.getValue());
}
Expand All @@ -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<String, Object> entry : state.typeCache.map().entrySet()) {
bh.consume(state.snappyTypeCache.get(entry.getKey()));
bh.consume(state.typeCache.get(new String(entry.getKey())));
}
}

Expand Down
2 changes: 0 additions & 2 deletions rewrite-java/build.gradle.kts
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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<Object, Object> typeCache = new HashMap<>();
AdaptiveRadixTree<Object> typeCache = new AdaptiveRadixTree<>();

public <T> @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);
Expand Down
Loading