8266642: improve ResolvedMethodTable hash function

Reviewed-by: vlivanov, coleenp
This commit is contained in:
Denghui Dong 2021-05-20 22:19:04 +00:00 committed by Vladimir Ivanov
parent 1c7a1310d9
commit 83b3607290
3 changed files with 21 additions and 2 deletions
src/hotspot/share
test/hotspot/jtreg/runtime/MemberName

@ -325,6 +325,10 @@ class ClassLoaderData : public CHeapObj<mtClass> {
const char* loader_name_and_id() const;
Symbol* name_and_id() const { return _name_and_id; }
unsigned identity_hash() const {
return (unsigned)((uintptr_t)this >> 3);
}
JFR_ONLY(DEFINE_TRACE_ID_METHODS;)
};

@ -23,6 +23,7 @@
*/
#include "precompiled.hpp"
#include "classfile/classLoaderData.hpp"
#include "classfile/javaClasses.hpp"
#include "gc/shared/oopStorage.inline.hpp"
#include "gc/shared/oopStorageSet.hpp"
@ -53,7 +54,8 @@ static const size_t GROW_HINT = 32;
static const size_t ResolvedMethodTableSizeLog = 10;
unsigned int method_hash(const Method* method) {
unsigned int hash = method->klass_name()->identity_hash();
unsigned int hash = method->method_holder()->class_loader_data()->identity_hash();
hash = (hash * 31) ^ method->klass_name()->identity_hash();
hash = (hash * 31) ^ method->name()->identity_hash();
hash = (hash * 31) ^ method->signature()->identity_hash();
return hash;

@ -48,6 +48,14 @@ public class ResolvedMethodTableHash extends ClassLoader {
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
}
private MethodHandle generateWithSameName() throws ReflectiveOperationException {
byte[] buf = new byte[100];
int size = writeClass(buf, "MH$$");
// use different classloader instances to load the classes with the same name
Class<?> cls = new ResolvedMethodTableHash().defineClass(null, buf, 0, size);
return MethodHandles.publicLookup().findStatic(cls, "m", MethodType.methodType(void.class));
}
// Produce a class file with the given name and a single method:
// public static native void m();
private int writeClass(byte[] buf, String className) {
@ -82,7 +90,12 @@ public class ResolvedMethodTableHash extends ClassLoader {
int count = args.length > 0 ? Integer.parseInt(args[0]) : 200000;
for (int i = 0; i < count; i++) {
handles.add(generator.generate("MH$" + i));
// prevents metaspace oom
if (i % 20 != 0) {
handles.add(generator.generate("MH$" + i));
} else {
handles.add(generator.generateWithSameName());
}
if (i % 1000 == 0) {
System.out.println("Generated " + i + " handles");
}