Object.getClass() is called often in reflection code, and some customers (e.g. Nortel) use the class object for hash table lookup of class specific data:
Object obj = ...
ClassInfo info = lookup_class_info(obj.getClass());
ClassInfo lookup_class_info(Class cls) {
int hash = cls.hashCode();
// do hash table lookup
...
}
Calling Object.hashCode() is already fast, but getClass() is not. Storing a pointer to the class (or class info) in each instance will have footprint impact.
Inlining it is trivial. Object.getClass() is already "final native", and the implementation would be:
const int klass_offset = oopDesc::klass_offset_in_bytes();
const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
// receiver in eax
__ movl(eax, Address(eax, oopDesc::klass_offset_in_bytes()));
__ movl(eax, Address(eax, mirror_offset));
There has to be a null check on the receiver somewhere (throwing a NullPointerException).
This should be done in both compiler1 and compiler2.
steffen.grarup@eng 1999-11-10
Object obj = ...
ClassInfo info = lookup_class_info(obj.getClass());
ClassInfo lookup_class_info(Class cls) {
int hash = cls.hashCode();
// do hash table lookup
...
}
Calling Object.hashCode() is already fast, but getClass() is not. Storing a pointer to the class (or class info) in each instance will have footprint impact.
Inlining it is trivial. Object.getClass() is already "final native", and the implementation would be:
const int klass_offset = oopDesc::klass_offset_in_bytes();
const int mirror_offset = klassOopDesc::klass_part_offset_in_bytes() + Klass::java_mirror_offset_in_bytes();
// receiver in eax
__ movl(eax, Address(eax, oopDesc::klass_offset_in_bytes()));
__ movl(eax, Address(eax, mirror_offset));
There has to be a null check on the receiver somewhere (throwing a NullPointerException).
This should be done in both compiler1 and compiler2.
steffen.grarup@eng 1999-11-10