summaryrefslogtreecommitdiffstats
path: root/src/corelib/tools/qcache.h
diff options
context:
space:
mode:
authorLars Knoll <lars.knoll@qt.io>2020-02-07 12:29:59 +0100
committerLars Knoll <lars.knoll@qt.io>2020-04-09 20:03:32 +0200
commit0375757bfb3852ace3bf3237a7de0ed2dbb371d8 (patch)
treeda0241b0cb4870b42a72cd572e7b6845a4d8700e /src/corelib/tools/qcache.h
parentc6cdf38e752c22babdbe645366bdfb7ce51d01ff (diff)
Implement emplace() for QHash and QMultiHash
At the same time use the opportunity to refactor the insertion code inside the implementation of QHash to avoid copy and move constructors as much as possible and always construct nodes in place. Change-Id: I951b4cf2c77a17f7db825c6a776aae38c2662d23 Reviewed-by: MÃ¥rten Nordheim <marten.nordheim@qt.io>
Diffstat (limited to 'src/corelib/tools/qcache.h')
-rw-r--r--src/corelib/tools/qcache.h18
1 files changed, 14 insertions, 4 deletions
diff --git a/src/corelib/tools/qcache.h b/src/corelib/tools/qcache.h
index f043ea9cff..3582a44e32 100644
--- a/src/corelib/tools/qcache.h
+++ b/src/corelib/tools/qcache.h
@@ -99,9 +99,13 @@ class QCache
value(std::move(t))
{
}
- static Node create(Key &&k, Value &&t) noexcept(std::is_nothrow_move_assignable_v<Key> && std::is_nothrow_move_assignable_v<T>)
+ static void createInPlace(Node *n, const Key &k, T *o, int cost)
{
- return Node(std::move(k), std::move(t));
+ new (n) Node{ Key(k), Value(o, cost) };
+ }
+ void emplace(T *o, int cost)
+ {
+ value = Value(o, cost);
}
static Node create(const Key &k, Value &&t) noexcept(std::is_nothrow_move_assignable_v<Key> && std::is_nothrow_move_assignable_v<T>)
{
@@ -230,9 +234,15 @@ public:
return false;
}
trim(mx - cost);
- auto it = d.insert(Node::create(Key(key), Value(object, cost)));
+ auto result = d.findOrInsert(key);
+ Node *n = result.it.node();
+ if (result.initialized) {
+ cost -= n->value.cost;
+ result.it.node()->emplace(object, cost);
+ } else {
+ Node::createInPlace(n, key, object, cost);
+ }
total += cost;
- Node *n = it.node();
n->prev = &chain;
n->next = chain.next;
chain.next->prev = n;