From 95b94ad19b025cef0617610a2605f23688a48a2f Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 02:16:55 +0000 Subject: [PATCH 1/4] In `pool`, construct `entry_t`s in-place and add an rvalue-accepting-and-forwarding `insert()` method. --- kernel/hashlib.h | 27 +++++++++++++++++++++++++-- 1 file changed, 25 insertions(+), 2 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 996bda38e..1a21494a7 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -745,11 +745,24 @@ protected: int do_insert(const K &value, int &hash) { if (hashtable.empty()) { - entries.push_back(entry_t(value, -1)); + entries.emplace_back(value, -1); do_rehash(); hash = do_hash(value); } else { - entries.push_back(entry_t(value, hashtable[hash])); + entries.emplace_back(value, hashtable[hash]); + hashtable[hash] = entries.size() - 1; + } + return entries.size() - 1; + } + + int do_insert(K &&value, int &hash) + { + if (hashtable.empty()) { + entries.emplace_back(std::forward(value), -1); + do_rehash(); + hash = do_hash(value); + } else { + entries.emplace_back(std::forward(value), hashtable[hash]); hashtable[hash] = entries.size() - 1; } return entries.size() - 1; @@ -847,6 +860,16 @@ public: return std::pair(iterator(this, i), true); } + std::pair insert(K &&value) + { + int hash = do_hash(value); + int i = do_lookup(value, hash); + if (i >= 0) + return std::pair(iterator(this, i), false); + i = do_insert(std::forward(value), hash); + return std::pair(iterator(this, i), true); + } + int erase(const K &key) { int hash = do_hash(key); From ecaa892e3530a3cc0fde56e1724538475c017857 Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Mon, 20 Apr 2020 05:37:10 +0000 Subject: [PATCH 2/4] Add rvalue-reference-accepting `entry_t` constructor for `pool`. --- kernel/hashlib.h | 1 + 1 file changed, 1 insertion(+) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index 1a21494a7..b0e946072 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -642,6 +642,7 @@ protected: entry_t() { } entry_t(const K &udata, int next) : udata(udata), next(next) { } + entry_t(K &&udata, int next) : udata(std::move(udata)), next(next) { } }; std::vector hashtable; From 746c29b1716b7eac4f4be0eded64e7aa05a6682d Mon Sep 17 00:00:00 2001 From: Alberto Gonzalez Date: Tue, 21 Apr 2020 17:17:47 +0000 Subject: [PATCH 3/4] kernel: Rename arguments to rvalue-reference-accepting functions. --- kernel/hashlib.h | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index b0e946072..ad2ed6033 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -756,14 +756,14 @@ protected: return entries.size() - 1; } - int do_insert(K &&value, int &hash) + int do_insert(K &&rvalue, int &hash) { if (hashtable.empty()) { - entries.emplace_back(std::forward(value), -1); + entries.emplace_back(std::forward(rvalue), -1); do_rehash(); - hash = do_hash(value); + hash = do_hash(rvalue); } else { - entries.emplace_back(std::forward(value), hashtable[hash]); + entries.emplace_back(std::forward(rvalue), hashtable[hash]); hashtable[hash] = entries.size() - 1; } return entries.size() - 1; @@ -861,13 +861,13 @@ public: return std::pair(iterator(this, i), true); } - std::pair insert(K &&value) + std::pair insert(K &&rvalue) { - int hash = do_hash(value); - int i = do_lookup(value, hash); + int hash = do_hash(rvalue); + int i = do_lookup(rvalue, hash); if (i >= 0) return std::pair(iterator(this, i), false); - i = do_insert(std::forward(value), hash); + i = do_insert(std::forward(rvalue), hash); return std::pair(iterator(this, i), true); } From a7c66fdc61508017113a93b51ea79827fb67adfc Mon Sep 17 00:00:00 2001 From: Eddie Hung Date: Wed, 22 Apr 2020 08:14:07 -0700 Subject: [PATCH 4/4] pool: add emplace() function --- kernel/hashlib.h | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/kernel/hashlib.h b/kernel/hashlib.h index ad2ed6033..ba224191c 100644 --- a/kernel/hashlib.h +++ b/kernel/hashlib.h @@ -871,6 +871,12 @@ public: return std::pair(iterator(this, i), true); } + template + std::pair emplace(Args&&... args) + { + return insert(K(std::forward(args)...)); + } + int erase(const K &key) { int hash = do_hash(key);