RFR: 8352075: Perf regression accessing fields [v4]
Hannes Greule
hgreule at openjdk.org
Tue May 13 13:18:55 UTC 2025
On Tue, 13 May 2025 11:39:32 GMT, Coleen Phillimore <coleenp at openjdk.org> wrote:
> I was going to write a test with JVMTI GetFields and > 16 fields because I'm not sure how/if you're fixing the order that the fields are returned by that API.
I experimented with that a bit:
class MyClass {
public static void main(String[] args) {
System.out.println("reflection:");
for (var field : MyClass.class.getDeclaredFields()) {
System.out.println(field.getName());
}
}
Object d;
int a;
Object c;
int b;
Object x;
int m;
Object e;
int f;
Object w;
int z;
Object g;
int s;
Object q;
int i;
Object h;
int o;
Object v;
int y;
Object j;
}
#include <string.h>
#include "jvmti.h"
#include "jni.h"
void ClassPrepare(jvmtiEnv* jvmti_env, JNIEnv* jni_env, jthread thread, jclass klass) {
char* ptr;
jvmtiError err = (*jvmti_env)->GetClassSignature(jvmti_env, klass, &ptr, NULL);
if (err != JNI_OK) {
return;
}
if (strcmp("LMyClass;", ptr) == 0) {
printf("%s\n", ptr);
jfieldID* fields;
jint field_count;
err = (*jvmti_env)->GetClassFields(jvmti_env, klass, &field_count, &fields);
if (err != JNI_OK) {
printf("error\n");
return;
}
printf("JVMTI:\n");
for (int i = 0; i < field_count; i++) {
err = (*jvmti_env)->GetFieldName(jvmti_env, klass, fields[i], &ptr, NULL, NULL);
if (err != JNI_OK) {
printf("error\n");
}
printf("%s\n", ptr);
}
}
}
jint Agent_OnLoad(JavaVM *vm, char *options, void *reserved) {
jvmtiEnv *environment;
jvmtiEventCallbacks callbacks;
jvmtiError error;
jint result;
result = (*vm)->GetEnv(vm, (void**)&environment, JVMTI_VERSION_1_2);
if(result != 0){
return JNI_ERR;
}
(void)memset(&callbacks, 0, sizeof(callbacks));
callbacks.ClassPrepare = (void*)&ClassPrepare;
error = (*environment)->SetEventCallbacks(environment, &callbacks, (jint)sizeof(callbacks));
if(error != JNI_OK){
return JNI_ERR;
}
error = (*environment)->SetEventNotificationMode(environment, JVMTI_ENABLE, JVMTI_EVENT_CLASS_PREPARE, NULL);
if (error != JNI_OK) {
return JNI_ERR;
}
printf("Agent is present and registered hooks\n");
return JNI_OK;
}
With this change, the order of both JVMTI and reflections depends on the field names. If the original order information is still present, I'd assume sorting the array provided by JVMTI is acceptable.
-------------
PR Comment: https://git.openjdk.org/jdk/pull/24847#issuecomment-2876483703
More information about the hotspot-dev
mailing list