Skip to content

Commit 48e45d1

Browse files
committed
GH-609 cache value of static field java.lang.StringUTF16.HI_BYTE_SHIFT and java.lang.StringUTF16.LO_BYTE_SHIFT
1 parent 42f2e35 commit 48e45d1

File tree

2 files changed

+37
-18
lines changed
  • visualvm/libs.profiler
    • lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap
    • profiler.heapwalker/src/org/graalvm/visualvm/lib/profiler/heapwalk/details/api

2 files changed

+37
-18
lines changed

visualvm/libs.profiler/lib.profiler.heap/src/org/graalvm/visualvm/lib/jfluid/heap/HprofProxy.java

Lines changed: 18 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,9 @@
2525

2626
package org.graalvm.visualvm.lib.jfluid.heap;
2727

28+
import java.util.Map;
2829
import java.util.Properties;
30+
import java.util.WeakHashMap;
2931

3032

3133
/**
@@ -143,16 +145,24 @@ private static char[] getChars(PrimitiveArrayDump chars, Byte coder, int offset,
143145
return "*unknown coder*".toCharArray();
144146
}
145147
}
146-
148+
149+
private static final Map<Heap,int[]> CACHE = new WeakHashMap<>();
150+
147151
private static int[] getStringUTF16ShiftBytes(Heap heap) {
148-
JavaClass utf16Class = heap.getJavaClassByName("java.lang.StringUTF16"); // NOI18N
149-
Integer HI_BYTE_SHIFT = (Integer) utf16Class.getValueOfStaticField("HI_BYTE_SHIFT"); // NOI18N
150-
Integer LO_BYTE_SHIFT = (Integer) utf16Class.getValueOfStaticField("LO_BYTE_SHIFT"); // NOI18N
152+
int[] shiftBytes = CACHE.get(heap);
151153

152-
if (HI_BYTE_SHIFT != null && LO_BYTE_SHIFT != null) {
153-
return new int[] {HI_BYTE_SHIFT.intValue(),LO_BYTE_SHIFT.intValue()};
154+
if (shiftBytes == null) {
155+
JavaClass utf16Class = heap.getJavaClassByName("java.lang.StringUTF16"); // NOI18N
156+
Integer HI_BYTE_SHIFT = (Integer) utf16Class.getValueOfStaticField("HI_BYTE_SHIFT"); // NOI18N
157+
Integer LO_BYTE_SHIFT = (Integer) utf16Class.getValueOfStaticField("LO_BYTE_SHIFT"); // NOI18N
158+
159+
if (HI_BYTE_SHIFT != null && LO_BYTE_SHIFT != null) {
160+
shiftBytes = new int[] {HI_BYTE_SHIFT.intValue(), LO_BYTE_SHIFT.intValue()};
161+
}
162+
// use default
163+
shiftBytes = new int[] {0,8};
164+
CACHE.put(heap, shiftBytes);
154165
}
155-
// use default
156-
return new int[] {0,8};
166+
return shiftBytes;
157167
}
158168
}

visualvm/libs.profiler/profiler.heapwalker/src/org/graalvm/visualvm/lib/profiler/heapwalk/details/api/StringDecoder.java

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@
2525
package org.graalvm.visualvm.lib.profiler.heapwalk.details.api;
2626

2727
import java.util.List;
28+
import java.util.Map;
29+
import java.util.WeakHashMap;
2830
import org.graalvm.visualvm.lib.jfluid.heap.Heap;
2931
import org.graalvm.visualvm.lib.jfluid.heap.JavaClass;
3032

@@ -34,6 +36,8 @@
3436
*/
3537
public final class StringDecoder {
3638

39+
private static final Map<Heap,int[]> CACHE = new WeakHashMap<>();
40+
3741
private final byte coder;
3842
private final List<String> values;
3943
private int HI_BYTE_SHIFT;
@@ -43,18 +47,23 @@ public StringDecoder(Heap heap, byte c, List<String> val) {
4347
coder = c;
4448
values = val;
4549
if (coder == 1) {
46-
JavaClass utf16Class = heap.getJavaClassByName("java.lang.StringUTF16"); // NOI18N
47-
Integer hiShift = (Integer) utf16Class.getValueOfStaticField("HI_BYTE_SHIFT"); // NOI18N
48-
Integer loShift = (Integer) utf16Class.getValueOfStaticField("LO_BYTE_SHIFT"); // NOI18N
50+
int[] shiftBytes = CACHE.get(heap);
51+
52+
if (shiftBytes == null) {
53+
JavaClass utf16Class = heap.getJavaClassByName("java.lang.StringUTF16"); // NOI18N
54+
Integer hiShift = (Integer) utf16Class.getValueOfStaticField("HI_BYTE_SHIFT"); // NOI18N
55+
Integer loShift = (Integer) utf16Class.getValueOfStaticField("LO_BYTE_SHIFT"); // NOI18N
4956

50-
if (hiShift != null && loShift != null) {
51-
HI_BYTE_SHIFT = hiShift.intValue();
52-
LO_BYTE_SHIFT = loShift.intValue();
53-
} else {
54-
// use default
55-
HI_BYTE_SHIFT = 0;
56-
LO_BYTE_SHIFT = 8;
57+
if (hiShift != null && loShift != null) {
58+
shiftBytes = new int[] {hiShift.intValue(), loShift.intValue()};
59+
} else {
60+
// use default
61+
shiftBytes = new int[] {0,8};
62+
}
63+
CACHE.put(heap, shiftBytes);
5764
}
65+
HI_BYTE_SHIFT = shiftBytes[0];
66+
LO_BYTE_SHIFT = shiftBytes[1];
5867
}
5968
}
6069

0 commit comments

Comments
 (0)