Skip to content

Commit 1efe946

Browse files
committedOct 22, 2021
8275712: Hashtable literal_size functions are broken
Reviewed-by: coleenp, zgu
1 parent fab3d6c commit 1efe946

File tree

2 files changed

+23
-18
lines changed

2 files changed

+23
-18
lines changed
 

‎src/hotspot/share/classfile/stringTable.cpp

+10-9
Original file line numberDiff line numberDiff line change
@@ -538,18 +538,19 @@ void StringTable::rehash_table() {
538538
}
539539

540540
// Statistics
541-
static int literal_size(oop obj) {
542-
// NOTE: this would over-count if (pre-JDK8)
543-
// java_lang_Class::has_offset_field() is true and the String.value array is
544-
// shared by several Strings. However, starting from JDK8, the String.value
545-
// array is not shared anymore.
541+
static size_t literal_size(oop obj) {
546542
if (obj == NULL) {
547543
return 0;
548-
} else if (obj->klass() == vmClasses::String_klass()) {
549-
return (obj->size() + java_lang_String::value(obj)->size()) * HeapWordSize;
550-
} else {
551-
return obj->size();
552544
}
545+
546+
size_t word_size = obj->size();
547+
548+
if (obj->klass() == vmClasses::String_klass()) {
549+
// This may overcount if String.value arrays are shared.
550+
word_size += java_lang_String::value(obj)->size();
551+
}
552+
553+
return word_size * HeapWordSize;
553554
}
554555

555556
struct SizeFunc : StackObj {

‎src/hotspot/share/utilities/hashtable.cpp

+13-9
Original file line numberDiff line numberDiff line change
@@ -75,24 +75,28 @@ template <MEMFLAGS F> void BasicHashtable<F>::free_buckets() {
7575
}
7676

7777
// Default overload, for types that are uninteresting.
78-
template<typename T> static int literal_size(T) { return 0; }
78+
template<typename T> static size_t literal_size(T) { return 0; }
7979

80-
static int literal_size(Symbol *symbol) {
80+
static size_t literal_size(Symbol *symbol) {
8181
return symbol->size() * HeapWordSize;
8282
}
8383

84-
static int literal_size(oop obj) {
84+
static size_t literal_size(oop obj) {
8585
if (obj == NULL) {
8686
return 0;
87-
} else if (obj->klass() == vmClasses::String_klass()) {
87+
}
88+
89+
size_t word_size = obj->size();
90+
91+
if (obj->klass() == vmClasses::String_klass()) {
8892
// This may overcount if String.value arrays are shared.
89-
return (obj->size() + java_lang_String::value(obj)->size()) * HeapWordSize;
90-
} else {
91-
return obj->size();
93+
word_size += java_lang_String::value(obj)->size();
9294
}
95+
96+
return word_size * HeapWordSize;
9397
}
9498

95-
static int literal_size(WeakHandle v) {
99+
static size_t literal_size(WeakHandle v) {
96100
return literal_size(v.peek());
97101
}
98102

@@ -175,7 +179,7 @@ template <MEMFLAGS F> bool BasicHashtable<F>::maybe_grow(int max_size, int load_
175179

176180
template <class T, MEMFLAGS F> TableStatistics Hashtable<T, F>::statistics_calculate(T (*literal_load_barrier)(HashtableEntry<T, F>*)) {
177181
NumberSeq summary;
178-
int literal_bytes = 0;
182+
size_t literal_bytes = 0;
179183
for (int i = 0; i < this->table_size(); ++i) {
180184
int count = 0;
181185
for (HashtableEntry<T, F>* e = this->bucket(i);

0 commit comments

Comments
 (0)
Please sign in to comment.