Skip to content

Commit

Permalink
Merge pull request #100 from DataDog/revert-86-rgs/remove-custom-cont…
Browse files Browse the repository at this point in the history
…ext-cache

Revert "remove unnecessary cache for custom context encodings"
  • Loading branch information
richardstartin authored May 15, 2024
2 parents 3bb1dfa + 8af7be4 commit 1ac8ce4
Showing 1 changed file with 15 additions and 1 deletion.
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,16 @@
import java.util.HashSet;
import java.util.List;
import java.util.Set;
import java.util.concurrent.ConcurrentHashMap;

public class ContextSetter {

private static final int TAGS_STORAGE_LIMIT = 10;
private final List<String> attributes;
private final JavaProfiler profiler;

private final ConcurrentHashMap<String, Integer> jniCache = new ConcurrentHashMap<>();

public ContextSetter(JavaProfiler profiler, List<String> attributes) {
this.profiler = profiler;
Set<String> unique = new HashSet<>(attributes);
Expand All @@ -24,7 +27,18 @@ public ContextSetter(JavaProfiler profiler, List<String> attributes) {
}

public int encode(String key) {
return key == null ? 0 : profiler.registerConstant(key);
if (key != null) {
Integer encoding = jniCache.get(key);
if (encoding != null) {
return encoding;
} else if (jniCache.size() <= 1 << 16) {
int e = profiler.registerConstant(key);
if (e > 0 && jniCache.putIfAbsent(key, e) == null) {
return e;
}
}
}
return 0;
}

public int[] snapshotTags() {
Expand Down

0 comments on commit 1ac8ce4

Please sign in to comment.