Skip to content

Commit 001e51d

Browse files
committedSep 8, 2020
8250563: Add KVHashtable::add_if_absent
Reviewed-by: ccheung, coleenp
1 parent 91a20ca commit 001e51d

File tree

4 files changed

+31
-13
lines changed

4 files changed

+31
-13
lines changed
 

‎src/hotspot/share/memory/archiveBuilder.cpp

+3-5
Original file line numberDiff line numberDiff line change
@@ -333,14 +333,12 @@ bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref,
333333

334334
FollowMode follow_mode = get_follow_mode(ref);
335335
SourceObjInfo src_info(ref, read_only, follow_mode);
336-
bool created = false;
337-
SourceObjInfo* p = _src_obj_table.lookup(src_obj);
338-
if (p == NULL) {
339-
p = _src_obj_table.add(src_obj, src_info);
336+
bool created;
337+
SourceObjInfo* p = _src_obj_table.add_if_absent(src_obj, src_info, &created);
338+
if (created) {
340339
if (_src_obj_table.maybe_grow(MAX_TABLE_SIZE)) {
341340
log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
342341
}
343-
created = true;
344342
}
345343

346344
assert(p->read_only() == src_info.read_only(), "must be");

‎src/hotspot/share/memory/metaspaceClosure.cpp

+3-4
Original file line numberDiff line numberDiff line change
@@ -94,12 +94,11 @@ MetaspaceClosure::~MetaspaceClosure() {
9494
}
9595

9696
bool UniqueMetaspaceClosure::do_ref(MetaspaceClosure::Ref* ref, bool read_only) {
97-
bool* found = _has_been_visited.lookup(ref->obj());
98-
if (found != NULL) {
99-
assert(*found == read_only, "must be");
97+
bool created;
98+
_has_been_visited.add_if_absent(ref->obj(), read_only, &created);
99+
if (!created) {
100100
return false; // Already visited: no need to iterate embedded pointers.
101101
} else {
102-
_has_been_visited.add(ref->obj(), read_only);
103102
if (_has_been_visited.maybe_grow(MAX_TABLE_SIZE)) {
104103
log_info(cds, hashtables)("Expanded _has_been_visited table to %d", _has_been_visited.table_size());
105104
}

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

+22-1
Original file line numberDiff line numberDiff line change
@@ -312,13 +312,34 @@ class KVHashtable : public BasicHashtable<F> {
312312
unsigned int hash = HASH(key);
313313
int index = BasicHashtable<F>::hash_to_index(hash);
314314
for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
315-
if (e->hash() == hash && e->_key == key) {
315+
if (e->hash() == hash && EQUALS(e->_key, key)) {
316316
return &(e->_value);
317317
}
318318
}
319319
return NULL;
320320
}
321321

322+
// Look up the key.
323+
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
324+
// If no entry for the key exists, create a new entry from key and value and return a
325+
// pointer to the value.
326+
// *p_created is true if entry was created, false if entry pre-existed.
327+
V* add_if_absent(K key, V value, bool* p_created) {
328+
unsigned int hash = HASH(key);
329+
int index = BasicHashtable<F>::hash_to_index(hash);
330+
for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
331+
if (e->hash() == hash && EQUALS(e->_key, key)) {
332+
*p_created = false;
333+
return &(e->_value);
334+
}
335+
}
336+
337+
KVHashtableEntry* entry = new_entry(hash, key, value);
338+
BasicHashtable<F>::add_entry(BasicHashtable<F>::hash_to_index(hash), entry);
339+
*p_created = true;
340+
return &(entry->_value);
341+
}
342+
322343
int table_size() const {
323344
return BasicHashtable<F>::table_size();
324345
}

‎src/hotspot/share/utilities/resourceHash.hpp

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
2+
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
33
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
44
*
55
* This code is free software; you can redistribute it and/or modify it
@@ -133,7 +133,7 @@ class ResourceHashtable : public ResourceObj {
133133
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
134134
// If no entry for the key exists, create a new entry from key and a default-created value
135135
// and return a pointer to the value.
136-
// *p_created is new if entry was created, false if entry pre-existed.
136+
// *p_created is true if entry was created, false if entry pre-existed.
137137
V* put_if_absent(K const& key, bool* p_created) {
138138
unsigned hv = HASH(key);
139139
Node** ptr = lookup_node(hv, key);
@@ -150,7 +150,7 @@ class ResourceHashtable : public ResourceObj {
150150
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
151151
// If no entry for the key exists, create a new entry from key and value and return a
152152
// pointer to the value.
153-
// *p_created is new if entry was created, false if entry pre-existed.
153+
// *p_created is true if entry was created, false if entry pre-existed.
154154
V* put_if_absent(K const& key, V const& value, bool* p_created) {
155155
unsigned hv = HASH(key);
156156
Node** ptr = lookup_node(hv, key);

0 commit comments

Comments
 (0)
Please sign in to comment.