Skip to content
Permalink
Browse files
8266642: improve ResolvedMethodTable hash function
Reviewed-by: vlivanov, coleenp
  • Loading branch information
Denghui Dong authored and Vladimir Ivanov committed May 20, 2021
1 parent 1c7a131 commit 83b360729060abaefb00a0b3f6165926db956bf8
@@ -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 @@ private MethodHandle generate(String className) throws ReflectiveOperationExcept
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 static void main(String[] args) throws Exception {
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");
}

0 comments on commit 83b3607

Please sign in to comment.