Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(230)

Unified Diff: runtime/vm/hash_table.h

Issue 1884583004: - Avoid calling % in a loop when probing. (Closed) Base URL: git@github.com:dart-lang/sdk.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
« no previous file with comments | « no previous file | no next file » | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: runtime/vm/hash_table.h
diff --git a/runtime/vm/hash_table.h b/runtime/vm/hash_table.h
index c1622322137a2364d095f335ee86e41403ec263d..1f59c59af47b7d1d5494fe26a8c365559e579859 100644
--- a/runtime/vm/hash_table.h
+++ b/runtime/vm/hash_table.h
@@ -151,21 +151,24 @@ class HashTable : public ValueObject {
// Returns the entry that matches 'key', or -1 if none exists.
template<typename Key>
intptr_t FindKey(const Key& key) const {
- ASSERT(NumOccupied() < NumEntries());
+ const intptr_t num_entries = NumEntries();
+ ASSERT(NumOccupied() < num_entries);
// TODO(koda): Add salt.
- intptr_t probe = static_cast<uword>(KeyTraits::Hash(key)) % NumEntries();
+ uword hash = KeyTraits::Hash(key);
+ intptr_t probe = hash % num_entries;
// TODO(koda): Consider quadratic probing.
- for (; ; probe = (probe + 1) % NumEntries()) {
+ while (true) {
if (IsUnused(probe)) {
return -1;
- } else if (IsDeleted(probe)) {
- continue;
- } else {
+ } else if (!IsDeleted(probe)) {
key_handle_ = GetKey(probe);
if (KeyTraits::IsMatch(key, key_handle_)) {
return probe;
}
}
+ // Advance probe.
+ probe++;
+ probe = (probe == num_entries) ? 0 : probe;
}
UNREACHABLE();
return -1;
@@ -177,12 +180,14 @@ class HashTable : public ValueObject {
// and returns false.
template<typename Key>
bool FindKeyOrDeletedOrUnused(const Key& key, intptr_t* entry) const {
+ const intptr_t num_entries = NumEntries();
ASSERT(entry != NULL);
- ASSERT(NumOccupied() < NumEntries());
- intptr_t probe = static_cast<uword>(KeyTraits::Hash(key)) % NumEntries();
+ ASSERT(NumOccupied() < num_entries);
+ uword hash = KeyTraits::Hash(key);
+ intptr_t probe = hash % num_entries;
intptr_t deleted = -1;
// TODO(koda): Consider quadratic probing.
- for (; ; probe = (probe + 1) % NumEntries()) {
+ while (true) {
if (IsUnused(probe)) {
*entry = (deleted != -1) ? deleted : probe;
return false;
@@ -197,6 +202,9 @@ class HashTable : public ValueObject {
return true;
}
}
+ // Advance probe.
+ probe++;
+ probe = (probe == num_entries) ? 0 : probe;
}
UNREACHABLE();
return false;
« no previous file with comments | « no previous file | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698