Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
8250563: Add KVHashtable::add_if_absent
Reviewed-by: ccheung, coleenp
  • Loading branch information
iklam committed Sep 8, 2020
1 parent 91a20ca commit 001e51d
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 13 deletions.
8 changes: 3 additions & 5 deletions src/hotspot/share/memory/archiveBuilder.cpp
Expand Up @@ -333,14 +333,12 @@ bool ArchiveBuilder::gather_one_source_obj(MetaspaceClosure::Ref* enclosing_ref,

FollowMode follow_mode = get_follow_mode(ref);
SourceObjInfo src_info(ref, read_only, follow_mode);
bool created = false;
SourceObjInfo* p = _src_obj_table.lookup(src_obj);
if (p == NULL) {
p = _src_obj_table.add(src_obj, src_info);
bool created;
SourceObjInfo* p = _src_obj_table.add_if_absent(src_obj, src_info, &created);
if (created) {
if (_src_obj_table.maybe_grow(MAX_TABLE_SIZE)) {
log_info(cds, hashtables)("Expanded _src_obj_table table to %d", _src_obj_table.table_size());
}
created = true;
}

assert(p->read_only() == src_info.read_only(), "must be");
Expand Down
7 changes: 3 additions & 4 deletions src/hotspot/share/memory/metaspaceClosure.cpp
Expand Up @@ -94,12 +94,11 @@ MetaspaceClosure::~MetaspaceClosure() {
}

bool UniqueMetaspaceClosure::do_ref(MetaspaceClosure::Ref* ref, bool read_only) {
bool* found = _has_been_visited.lookup(ref->obj());
if (found != NULL) {
assert(*found == read_only, "must be");
bool created;
_has_been_visited.add_if_absent(ref->obj(), read_only, &created);
if (!created) {
return false; // Already visited: no need to iterate embedded pointers.
} else {
_has_been_visited.add(ref->obj(), read_only);
if (_has_been_visited.maybe_grow(MAX_TABLE_SIZE)) {
log_info(cds, hashtables)("Expanded _has_been_visited table to %d", _has_been_visited.table_size());
}
Expand Down
23 changes: 22 additions & 1 deletion src/hotspot/share/utilities/hashtable.hpp
Expand Up @@ -312,13 +312,34 @@ class KVHashtable : public BasicHashtable<F> {
unsigned int hash = HASH(key);
int index = BasicHashtable<F>::hash_to_index(hash);
for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
if (e->hash() == hash && e->_key == key) {
if (e->hash() == hash && EQUALS(e->_key, key)) {
return &(e->_value);
}
}
return NULL;
}

// Look up the key.
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
// If no entry for the key exists, create a new entry from key and value and return a
// pointer to the value.
// *p_created is true if entry was created, false if entry pre-existed.
V* add_if_absent(K key, V value, bool* p_created) {
unsigned int hash = HASH(key);
int index = BasicHashtable<F>::hash_to_index(hash);
for (KVHashtableEntry* e = bucket(index); e != NULL; e = e->next()) {
if (e->hash() == hash && EQUALS(e->_key, key)) {
*p_created = false;
return &(e->_value);
}
}

KVHashtableEntry* entry = new_entry(hash, key, value);
BasicHashtable<F>::add_entry(BasicHashtable<F>::hash_to_index(hash), entry);
*p_created = true;
return &(entry->_value);
}

int table_size() const {
return BasicHashtable<F>::table_size();
}
Expand Down
6 changes: 3 additions & 3 deletions src/hotspot/share/utilities/resourceHash.hpp
@@ -1,5 +1,5 @@
/*
* Copyright (c) 2012, 2019, Oracle and/or its affiliates. All rights reserved.
* Copyright (c) 2012, 2020, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
Expand Down Expand Up @@ -133,7 +133,7 @@ class ResourceHashtable : public ResourceObj {
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
// If no entry for the key exists, create a new entry from key and a default-created value
// and return a pointer to the value.
// *p_created is new if entry was created, false if entry pre-existed.
// *p_created is true if entry was created, false if entry pre-existed.
V* put_if_absent(K const& key, bool* p_created) {
unsigned hv = HASH(key);
Node** ptr = lookup_node(hv, key);
Expand All @@ -150,7 +150,7 @@ class ResourceHashtable : public ResourceObj {
// If an entry for the key exists, leave map unchanged and return a pointer to its value.
// If no entry for the key exists, create a new entry from key and value and return a
// pointer to the value.
// *p_created is new if entry was created, false if entry pre-existed.
// *p_created is true if entry was created, false if entry pre-existed.
V* put_if_absent(K const& key, V const& value, bool* p_created) {
unsigned hv = HASH(key);
Node** ptr = lookup_node(hv, key);
Expand Down

2 comments on commit 001e51d

@bridgekeeper
Copy link

@bridgekeeper bridgekeeper bot commented on 001e51d Sep 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Issues

@openjdk-notifier
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Review

Issues

Please sign in to comment.