Skip to content

Commit a28e638

Browse files
committed
8353175: Eliminate double iteration of stream in FieldDescriptor reinitialization
Backport-of: f169fc5a99ee2b485e156c043134ab76b7e5ebd9
1 parent 950b4ff commit a28e638

File tree

7 files changed

+22
-31
lines changed

7 files changed

+22
-31
lines changed

src/hotspot/share/oops/fieldStreams.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ class FieldStreamBase : public StackObj {
120120

121121
// Convenient methods
122122

123-
FieldInfo to_FieldInfo() {
123+
const FieldInfo& to_FieldInfo() const {
124124
return _fi_buf;
125125
}
126126

@@ -131,7 +131,7 @@ class FieldStreamBase : public StackObj {
131131
// bridge to a heavier API:
132132
fieldDescriptor& field_descriptor() const {
133133
fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
134-
field.reinitialize(field_holder(), _index);
134+
field.reinitialize(field_holder(), to_FieldInfo());
135135
return field;
136136
}
137137
};

src/hotspot/share/oops/instanceKlass.cpp

Lines changed: 13 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1649,7 +1649,7 @@ bool InstanceKlass::find_local_field(Symbol* name, Symbol* sig, fieldDescriptor*
16491649
Symbol* f_name = fs.name();
16501650
Symbol* f_sig = fs.signature();
16511651
if (f_name == name && f_sig == sig) {
1652-
fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1652+
fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
16531653
return true;
16541654
}
16551655
}
@@ -1718,7 +1718,7 @@ Klass* InstanceKlass::find_field(Symbol* name, Symbol* sig, bool is_static, fiel
17181718
bool InstanceKlass::find_local_field_from_offset(int offset, bool is_static, fieldDescriptor* fd) const {
17191719
for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
17201720
if (fs.offset() == offset) {
1721-
fd->reinitialize(const_cast<InstanceKlass*>(this), fs.index());
1721+
fd->reinitialize(const_cast<InstanceKlass*>(this), fs.to_FieldInfo());
17221722
if (fd->is_static() == is_static) return true;
17231723
}
17241724
}
@@ -1779,19 +1779,16 @@ void InstanceKlass::do_nonstatic_fields(FieldClosure* cl) {
17791779
if (super != nullptr) {
17801780
super->do_nonstatic_fields(cl);
17811781
}
1782-
fieldDescriptor fd;
1783-
int length = java_fields_count();
1784-
for (int i = 0; i < length; i += 1) {
1785-
fd.reinitialize(this, i);
1782+
for (JavaFieldStream fs(this); !fs.done(); fs.next()) {
1783+
fieldDescriptor& fd = fs.field_descriptor();
17861784
if (!fd.is_static()) {
17871785
cl->do_field(&fd);
17881786
}
17891787
}
17901788
}
17911789

1792-
// first in Pair is offset, second is index.
1793-
static int compare_fields_by_offset(Pair<int,int>* a, Pair<int,int>* b) {
1794-
return a->first - b->first;
1790+
static int compare_fields_by_offset(FieldInfo* a, FieldInfo* b) {
1791+
return a->offset() - b->offset();
17951792
}
17961793

17971794
void InstanceKlass::print_nonstatic_fields(FieldClosure* cl) {
@@ -1800,26 +1797,20 @@ void InstanceKlass::print_nonstatic_fields(FieldClosure* cl) {
18001797
super->print_nonstatic_fields(cl);
18011798
}
18021799
ResourceMark rm;
1803-
fieldDescriptor fd;
18041800
// In DebugInfo nonstatic fields are sorted by offset.
1805-
GrowableArray<Pair<int,int> > fields_sorted;
1806-
int i = 0;
1801+
GrowableArray<FieldInfo> fields_sorted;
18071802
for (AllFieldStream fs(this); !fs.done(); fs.next()) {
18081803
if (!fs.access_flags().is_static()) {
1809-
fd = fs.field_descriptor();
1810-
Pair<int,int> f(fs.offset(), fs.index());
1811-
fields_sorted.push(f);
1812-
i++;
1804+
fields_sorted.push(fs.to_FieldInfo());
18131805
}
18141806
}
1815-
if (i > 0) {
1816-
int length = i;
1817-
assert(length == fields_sorted.length(), "duh");
1818-
// _sort_Fn is defined in growableArray.hpp.
1807+
int length = fields_sorted.length();
1808+
if (length > 0) {
18191809
fields_sorted.sort(compare_fields_by_offset);
1810+
fieldDescriptor fd;
18201811
for (int i = 0; i < length; i++) {
1821-
fd.reinitialize(this, fields_sorted.at(i).second);
1822-
assert(!fd.is_static() && fd.offset() == fields_sorted.at(i).first, "only nonstatic fields");
1812+
fd.reinitialize(this, fields_sorted.at(i));
1813+
assert(!fd.is_static() && fd.offset() == checked_cast<int>(fields_sorted.at(i).offset()), "only nonstatic fields");
18231814
cl->do_field(&fd);
18241815
}
18251816
}

src/hotspot/share/oops/instanceKlass.hpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ class InstanceKlass: public Klass {
134134
friend class JVMCIVMStructs;
135135
friend class ClassFileParser;
136136
friend class CompileReplay;
137+
friend class FieldStream;
137138

138139
public:
139140
static const KlassKind Kind = InstanceKlassKind;

src/hotspot/share/prims/jvm.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1787,7 +1787,7 @@ JVM_ENTRY(jobjectArray, JVM_GetClassDeclaredFields(JNIEnv *env, jclass ofClass,
17871787
fieldDescriptor fd;
17881788
for (JavaFieldStream fs(k); !fs.done(); fs.next()) {
17891789
if (!publicOnly || fs.access_flags().is_public()) {
1790-
fd.reinitialize(k, fs.index());
1790+
fd.reinitialize(k, fs.to_FieldInfo());
17911791
oop field = Reflection::new_field(&fd, CHECK_NULL);
17921792
result->obj_at_put(out_idx, field);
17931793
++out_idx;

src/hotspot/share/runtime/fieldDescriptor.cpp

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -87,16 +87,15 @@ oop fieldDescriptor::string_initial_value(TRAPS) const {
8787
return constants()->uncached_string_at(initial_value_index(), THREAD);
8888
}
8989

90-
void fieldDescriptor::reinitialize(InstanceKlass* ik, int index) {
90+
void fieldDescriptor::reinitialize(InstanceKlass* ik, const FieldInfo& fieldinfo) {
9191
if (_cp.is_null() || field_holder() != ik) {
9292
_cp = constantPoolHandle(Thread::current(), ik->constants());
9393
// _cp should now reference ik's constant pool; i.e., ik is now field_holder.
9494
// If the class is a scratch class, the constant pool points to the original class,
9595
// but that's ok because of constant pool merging.
9696
assert(field_holder() == ik || ik->is_scratch_class(), "must be already initialized to this class");
9797
}
98-
_fieldinfo= ik->field(index);
99-
assert((int)_fieldinfo.index() == index, "just checking");
98+
_fieldinfo = fieldinfo;
10099
guarantee(_fieldinfo.name_index() != 0 && _fieldinfo.signature_index() != 0, "bad constant pool index for fieldDescriptor");
101100
}
102101

src/hotspot/share/runtime/fieldDescriptor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -46,7 +46,7 @@ class fieldDescriptor {
4646
public:
4747
fieldDescriptor() {}
4848
fieldDescriptor(InstanceKlass* ik, int index) {
49-
reinitialize(ik, index);
49+
reinitialize(ik, ik->field(index));
5050
}
5151
inline Symbol* name() const;
5252
inline Symbol* signature() const;
@@ -102,7 +102,7 @@ class fieldDescriptor {
102102
inline void set_has_initialized_final_update(const bool value);
103103

104104
// Initialization
105-
void reinitialize(InstanceKlass* ik, int index);
105+
void reinitialize(InstanceKlass* ik, const FieldInfo& fieldinfo);
106106

107107
// Print
108108
void print() const;

src/hotspot/share/runtime/reflectionUtils.hpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -152,7 +152,7 @@ class FieldStream : public KlassStream {
152152
// bridge to a heavier API:
153153
fieldDescriptor& field_descriptor() const {
154154
fieldDescriptor& field = const_cast<fieldDescriptor&>(_fd_buf);
155-
field.reinitialize(_klass, _index);
155+
field.reinitialize(_klass, _klass->field(_index));
156156
return field;
157157
}
158158
};

0 commit comments

Comments
 (0)