This repository has been archived by the owner on Oct 19, 2020. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathvolatile_class_data_caching.patch
417 lines (396 loc) · 17.4 KB
/
volatile_class_data_caching.patch
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
diff -r 7ac292e57b5a src/share/classes/java/lang/Class.java
--- a/src/share/classes/java/lang/Class.java Thu Nov 01 14:12:21 2012 -0700
+++ b/src/share/classes/java/lang/Class.java Tue Nov 06 16:08:50 2012 +0100
@@ -2212,39 +2212,72 @@
// Caches for certain reflective results
private static boolean useCaches = true;
- private volatile transient SoftReference<Field[]> declaredFields;
- private volatile transient SoftReference<Field[]> publicFields;
- private volatile transient SoftReference<Method[]> declaredMethods;
- private volatile transient SoftReference<Method[]> publicMethods;
- private volatile transient SoftReference<Constructor<T>[]> declaredConstructors;
- private volatile transient SoftReference<Constructor<T>[]> publicConstructors;
- // Intermediate results for getFields and getMethods
- private volatile transient SoftReference<Field[]> declaredPublicFields;
- private volatile transient SoftReference<Method[]> declaredPublicMethods;
+
+ // volatile data that might get invalid when JVM TI RedefineClasses() is called
+ static class VolatileData<T> {
+ volatile Field[] declaredFields;
+ volatile Field[] publicFields;
+ volatile Method[] declaredMethods;
+ volatile Method[] publicMethods;
+ volatile Constructor<T>[] declaredConstructors;
+ volatile Constructor<T>[] publicConstructors;
+ // Intermediate results for getFields and getMethods
+ volatile Field[] declaredPublicFields;
+ volatile Method[] declaredPublicMethods;
+ // Annotations
+ volatile Map<Class<? extends Annotation>, Annotation> annotations;
+ volatile Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
+ // Value of classRedefinedCount when we created this VolatileData instance
+ final int redefinedCount;
+
+ VolatileData(int redefinedCount) {
+ this.redefinedCount = redefinedCount;
+ }
+
+ // initialize Unsafe machinery here, since we need to call Class.class instance method and would like to avoid
+ // calling it in the static initializer of the Class class...
+ private static final Unsafe unsafe;
+ // offset of Class.volatileData instance field
+ private static final long volatileDataOffset;
+
+ static {
+ unsafe = Unsafe.getUnsafe();
+ // bypass caches
+ Field volatileDataField = searchFields(Class.class.getDeclaredFields0(false), "volatileData");
+ if (volatileDataField == null) throw new Error("No volatileData field found in java.lang.Class");
+ volatileDataOffset = unsafe.objectFieldOffset(volatileDataField);
+ }
+
+ static <T> boolean compareAndSwap(Class<?> clazz, SoftReference<VolatileData<T>> oldData, SoftReference<VolatileData<T>> newData) {
+ return unsafe.compareAndSwapObject(clazz, volatileDataOffset, oldData, newData);
+ }
+ }
+
+ private volatile transient SoftReference<VolatileData<T>> volatileData;
// Incremented by the VM on each call to JVM TI RedefineClasses()
// that redefines this class or a superclass.
private volatile transient int classRedefinedCount = 0;
- // Value of classRedefinedCount when we last cleared the cached values
- // that are sensitive to class redefinition.
- private volatile transient int lastRedefinedCount = 0;
+ // Lazily create and cache VolatileData
+ private VolatileData<T> volatileData() {
+ if (!useCaches) return null;
- // Clears cached values that might possibly have been obsoleted by
- // a class redefinition.
- private void clearCachesOnClassRedefinition() {
- if (lastRedefinedCount != classRedefinedCount) {
- declaredFields = publicFields = declaredPublicFields = null;
- declaredMethods = publicMethods = declaredPublicMethods = null;
- declaredConstructors = publicConstructors = null;
- annotations = declaredAnnotations = null;
-
- // Use of "volatile" (and synchronization by caller in the case
- // of annotations) ensures that no thread sees the update to
- // lastRedefinedCount before seeing the caches cleared.
- // We do not guard against brief windows during which multiple
- // threads might redundantly work to fill an empty cache.
- lastRedefinedCount = classRedefinedCount;
+ while (true)
+ {
+ SoftReference<VolatileData<T>> volatileData = this.volatileData;
+ int classRedefinedCount = this.classRedefinedCount;
+ VolatileData<T> vd;
+ if (volatileData != null && (vd = volatileData.get()) != null && vd.redefinedCount == classRedefinedCount) {
+ return vd;
+ }
+ // no SoftReference or cleared SoftReference or stale VolatileData
+ vd = new VolatileData<T>(classRedefinedCount);
+ // try to CAS it...
+ if (VolatileData.compareAndSwap(this, volatileData, new SoftReference<VolatileData<T>>(vd))) {
+ return vd;
+ }
+ // else retry
}
}
@@ -2288,26 +2321,18 @@
private Field[] privateGetDeclaredFields(boolean publicOnly) {
checkInitted();
Field[] res = null;
- if (useCaches) {
- clearCachesOnClassRedefinition();
- if (publicOnly) {
- if (declaredPublicFields != null) {
- res = declaredPublicFields.get();
- }
- } else {
- if (declaredFields != null) {
- res = declaredFields.get();
- }
- }
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = publicOnly ? vd.declaredPublicFields : vd.declaredFields;
if (res != null) return res;
}
// No cached value available; request value from VM
res = Reflection.filterFields(this, getDeclaredFields0(publicOnly));
- if (useCaches) {
+ if (vd != null) {
if (publicOnly) {
- declaredPublicFields = new SoftReference<>(res);
+ vd.declaredPublicFields = res;
} else {
- declaredFields = new SoftReference<>(res);
+ vd.declaredFields = res;
}
}
return res;
@@ -2319,11 +2344,9 @@
private Field[] privateGetPublicFields(Set<Class<?>> traversedInterfaces) {
checkInitted();
Field[] res = null;
- if (useCaches) {
- clearCachesOnClassRedefinition();
- if (publicFields != null) {
- res = publicFields.get();
- }
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = vd.publicFields;
if (res != null) return res;
}
@@ -2356,8 +2379,8 @@
res = new Field[fields.size()];
fields.toArray(res);
- if (useCaches) {
- publicFields = new SoftReference<>(res);
+ if (vd != null) {
+ vd.publicFields = res;
}
return res;
}
@@ -2381,17 +2404,9 @@
private Constructor<T>[] privateGetDeclaredConstructors(boolean publicOnly) {
checkInitted();
Constructor<T>[] res = null;
- if (useCaches) {
- clearCachesOnClassRedefinition();
- if (publicOnly) {
- if (publicConstructors != null) {
- res = publicConstructors.get();
- }
- } else {
- if (declaredConstructors != null) {
- res = declaredConstructors.get();
- }
- }
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = publicOnly ? vd.publicConstructors : vd.declaredConstructors;
if (res != null) return res;
}
// No cached value available; request value from VM
@@ -2402,11 +2417,11 @@
} else {
res = getDeclaredConstructors0(publicOnly);
}
- if (useCaches) {
+ if (vd != null) {
if (publicOnly) {
- publicConstructors = new SoftReference<>(res);
+ vd.publicConstructors = res;
} else {
- declaredConstructors = new SoftReference<>(res);
+ vd.declaredConstructors = res;
}
}
return res;
@@ -2424,26 +2439,18 @@
private Method[] privateGetDeclaredMethods(boolean publicOnly) {
checkInitted();
Method[] res = null;
- if (useCaches) {
- clearCachesOnClassRedefinition();
- if (publicOnly) {
- if (declaredPublicMethods != null) {
- res = declaredPublicMethods.get();
- }
- } else {
- if (declaredMethods != null) {
- res = declaredMethods.get();
- }
- }
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = publicOnly ? vd.declaredPublicMethods : vd.declaredMethods;
if (res != null) return res;
}
// No cached value available; request value from VM
res = Reflection.filterMethods(this, getDeclaredMethods0(publicOnly));
if (useCaches) {
if (publicOnly) {
- declaredPublicMethods = new SoftReference<>(res);
+ vd.declaredPublicMethods = res;
} else {
- declaredMethods = new SoftReference<>(res);
+ vd.declaredMethods = res;
}
}
return res;
@@ -2546,11 +2553,9 @@
private Method[] privateGetPublicMethods() {
checkInitted();
Method[] res = null;
- if (useCaches) {
- clearCachesOnClassRedefinition();
- if (publicMethods != null) {
- res = publicMethods.get();
- }
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = vd.publicMethods;
if (res != null) return res;
}
@@ -2558,7 +2563,7 @@
// Start by fetching public declared methods
MethodArray methods = new MethodArray();
{
- Method[] tmp = privateGetDeclaredMethods(true);
+ Method[] tmp = privateGetDeclaredMethods(true);
methods.addAll(tmp);
}
// Now recur over superclass and direct superinterfaces.
@@ -2598,8 +2603,8 @@
methods.addAllIfNotPresent(inheritedMethods);
methods.compactAndTrim();
res = methods.getArray();
- if (useCaches) {
- publicMethods = new SoftReference<>(res);
+ if (vd != null) {
+ vd.publicMethods = res;
}
return res;
}
@@ -2609,7 +2614,7 @@
// Helpers for fetchers of one field, method, or constructor
//
- private Field searchFields(Field[] fields, String name) {
+ private static Field searchFields(Field[] fields, String name) {
String internedName = name.intern();
for (int i = 0; i < fields.length; i++) {
if (fields[i].getName() == internedName) {
@@ -3049,8 +3054,7 @@
if (annotationClass == null)
throw new NullPointerException();
- initAnnotationsIfNecessary();
- return (A) annotations.get(annotationClass);
+ return (A) privateGetAnnotations(false).get(annotationClass);
}
/**
@@ -3070,41 +3074,47 @@
* @since 1.5
*/
public Annotation[] getAnnotations() {
- initAnnotationsIfNecessary();
- return AnnotationParser.toArray(annotations);
+ return AnnotationParser.toArray(privateGetAnnotations(false));
}
/**
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
- initAnnotationsIfNecessary();
- return AnnotationParser.toArray(declaredAnnotations);
+ return AnnotationParser.toArray(privateGetAnnotations(true));
}
- // Annotations cache
- private transient Map<Class<? extends Annotation>, Annotation> annotations;
- private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
- private synchronized void initAnnotationsIfNecessary() {
- clearCachesOnClassRedefinition();
- if (annotations != null)
- return;
- declaredAnnotations = AnnotationParser.parseAnnotations(
+ private Map<Class<? extends Annotation>, Annotation> privateGetAnnotations(boolean declaredOnly) {
+ Map<Class<? extends Annotation>, Annotation> res;
+ VolatileData<T> vd = volatileData();
+ if (vd != null) {
+ res = declaredOnly ? vd.declaredAnnotations : vd.annotations;
+ if (res != null) return res;
+ }
+
+ Map<Class<? extends Annotation>, Annotation> declaredAnnotations = AnnotationParser.parseAnnotations(
getRawAnnotations(), getConstantPool(), this);
+ Map<Class<? extends Annotation>, Annotation> annotations;
Class<?> superClass = getSuperclass();
if (superClass == null) {
annotations = declaredAnnotations;
} else {
annotations = new HashMap<>();
- superClass.initAnnotationsIfNecessary();
- for (Map.Entry<Class<? extends Annotation>, Annotation> e : superClass.annotations.entrySet()) {
+ for (Map.Entry<Class<? extends Annotation>, Annotation> e : superClass.privateGetAnnotations(false).entrySet()) {
Class<? extends Annotation> annotationClass = e.getKey();
if (AnnotationType.getInstance(annotationClass).isInherited())
annotations.put(annotationClass, e.getValue());
}
annotations.putAll(declaredAnnotations);
}
+
+ if (vd != null) {
+ vd.annotations = annotations;
+ vd.declaredAnnotations = declaredAnnotations;
+ }
+
+ return declaredOnly ? declaredAnnotations : annotations;
}
// Annotation types cache their internal (AnnotationType) form
diff -r 7ac292e57b5a src/share/classes/java/lang/reflect/Constructor.java
--- a/src/share/classes/java/lang/reflect/Constructor.java Thu Nov 01 14:12:21 2012 -0700
+++ b/src/share/classes/java/lang/reflect/Constructor.java Tue Nov 06 16:08:50 2012 +0100
@@ -482,7 +482,10 @@
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
- return super.getDeclaredAnnotations();
+ if (root != null)
+ return root.getDeclaredAnnotations();
+ else
+ return super.getDeclaredAnnotations();
}
/**
diff -r 7ac292e57b5a src/share/classes/java/lang/reflect/Executable.java
--- a/src/share/classes/java/lang/reflect/Executable.java Thu Nov 01 14:12:21 2012 -0700
+++ b/src/share/classes/java/lang/reflect/Executable.java Tue Nov 06 16:08:50 2012 +0100
@@ -378,11 +378,12 @@
return AnnotationParser.toArray(declaredAnnotations());
}
- private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
+ private volatile transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
- private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
+ private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
+ Map<Class<? extends Annotation>, Annotation> declaredAnnotations = this.declaredAnnotations;
if (declaredAnnotations == null) {
- declaredAnnotations = AnnotationParser.parseAnnotations(
+ this.declaredAnnotations = declaredAnnotations = AnnotationParser.parseAnnotations(
getAnnotationBytes(),
sun.misc.SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()),
diff -r 7ac292e57b5a src/share/classes/java/lang/reflect/Field.java
--- a/src/share/classes/java/lang/reflect/Field.java Thu Nov 01 14:12:21 2012 -0700
+++ b/src/share/classes/java/lang/reflect/Field.java Tue Nov 06 16:08:50 2012 +0100
@@ -1027,11 +1027,15 @@
return AnnotationParser.toArray(declaredAnnotations());
}
- private transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
+ private volatile transient Map<Class<? extends Annotation>, Annotation> declaredAnnotations;
- private synchronized Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
+ private Map<Class<? extends Annotation>, Annotation> declaredAnnotations() {
+ if (root != null)
+ return root.declaredAnnotations();
+
+ Map<Class<? extends Annotation>, Annotation> declaredAnnotations = this.declaredAnnotations;
if (declaredAnnotations == null) {
- declaredAnnotations = AnnotationParser.parseAnnotations(
+ this.declaredAnnotations = declaredAnnotations = AnnotationParser.parseAnnotations(
annotations, sun.misc.SharedSecrets.getJavaLangAccess().
getConstantPool(getDeclaringClass()),
getDeclaringClass());
diff -r 7ac292e57b5a src/share/classes/java/lang/reflect/Method.java
--- a/src/share/classes/java/lang/reflect/Method.java Thu Nov 01 14:12:21 2012 -0700
+++ b/src/share/classes/java/lang/reflect/Method.java Tue Nov 06 16:08:50 2012 +0100
@@ -583,7 +583,10 @@
* @since 1.5
*/
public Annotation[] getDeclaredAnnotations() {
- return super.getDeclaredAnnotations();
+ if (root != null)
+ return root.getDeclaredAnnotations();
+ else
+ return super.getDeclaredAnnotations();
}
/**