Skip to content
This repository was archived by the owner on Sep 27, 2019. It is now read-only.

Commit 6cdca15

Browse files
poojanilangekarapavlo
authored andcommitted
Performance Fixes (#1375)
* Change RWSet to use tbb::concurrent_unordered_map * Modify tile_group_locator to use cuckoohash * Style Fix * Remove unused attribute
1 parent af8efb2 commit 6cdca15

File tree

8 files changed

+75
-92
lines changed

8 files changed

+75
-92
lines changed

src/catalog/manager.cpp

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ std::shared_ptr<storage::TileGroup> Manager::empty_tile_group_;
2525

2626
std::shared_ptr<storage::IndirectionArray> Manager::empty_indirection_array_;
2727

28+
Manager::Manager() : tile_group_locator_(DEFAULT_LOCATOR_SIZE) {}
29+
2830
Manager &Manager::GetInstance() {
2931
static Manager manager;
3032
return manager;
@@ -37,24 +39,24 @@ Manager &Manager::GetInstance() {
3739
void Manager::AddTileGroup(const oid_t oid,
3840
std::shared_ptr<storage::TileGroup> location) {
3941
// add/update the catalog reference to the tile group
40-
tile_group_locator_[oid] = location;
42+
tile_group_locator_.Upsert(oid, location);
4143
}
4244

4345
void Manager::DropTileGroup(const oid_t oid) {
4446
// drop the catalog reference to the tile group
45-
tile_group_locator_[oid] = empty_tile_group_;
47+
tile_group_locator_.Erase(oid);
4648
}
4749

4850
std::shared_ptr<storage::TileGroup> Manager::GetTileGroup(const oid_t oid) {
49-
auto iter = tile_group_locator_.find(oid);
50-
if (iter == tile_group_locator_.end()) {
51-
return empty_tile_group_;
51+
std::shared_ptr<storage::TileGroup> location;
52+
if (tile_group_locator_.Find(oid, location)) {
53+
return location;
5254
}
53-
return iter->second;
55+
return empty_tile_group_;
5456
}
5557

5658
// used for logging test
57-
void Manager::ClearTileGroup() { tile_group_locator_.clear(); }
59+
void Manager::ClearTileGroup() { tile_group_locator_.Clear(); }
5860

5961
void Manager::AddIndirectionArray(
6062
const oid_t oid, std::shared_ptr<storage::IndirectionArray> location) {

src/common/container/cuckoo_map.cpp

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -48,17 +48,23 @@ bool CUCKOO_MAP_TYPE::Insert(const KeyType &key, ValueType value) {
4848
return status;
4949
}
5050

51+
CUCKOO_MAP_TEMPLATE_ARGUMENTS
52+
void CUCKOO_MAP_TYPE::Upsert(const KeyType &key, ValueType value) {
53+
auto update_fn = [](UNUSED_ATTRIBUTE ValueType &value) { return; };
54+
cuckoo_map.upsert(key, update_fn, value);
55+
}
56+
5157
CUCKOO_MAP_TEMPLATE_ARGUMENTS
5258
bool CUCKOO_MAP_TYPE::Update(const KeyType &key, ValueType value) {
5359
auto status = cuckoo_map.update(key, value);
60+
LOG_TRACE("update status : %d", status);
5461
return status;
5562
}
5663

5764
CUCKOO_MAP_TEMPLATE_ARGUMENTS
5865
bool CUCKOO_MAP_TYPE::Erase(const KeyType &key) {
5966
auto status = cuckoo_map.erase(key);
6067
LOG_TRACE("erase status : %d", status);
61-
6268
return status;
6369
}
6470

@@ -102,6 +108,7 @@ CUCKOO_MAP_TYPE::GetConstIterator() const {
102108
// Explicit template instantiation
103109
template class CuckooMap<uint32_t, uint32_t>;
104110

111+
// Used in Catalog::Manager
105112
template class CuckooMap<oid_t, std::shared_ptr<storage::TileGroup>>;
106113

107114
// Used in Shared Pointer test and iterator test
@@ -118,8 +125,4 @@ template class CuckooMap<std::shared_ptr<oid_t>, std::shared_ptr<oid_t>>;
118125
// Used in StatementCacheManager
119126
template class CuckooMap<StatementCache *, StatementCache *>;
120127

121-
// Used in InternalTypes
122-
template class CuckooMap<ItemPointer, RWType, ItemPointerHasher,
123-
ItemPointerComparator>;
124-
125128
} // namespace peloton

src/concurrency/timestamp_ordering_transaction_manager.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -760,7 +760,7 @@ ResultType TimestampOrderingTransactionManager::CommitTransaction(
760760
oid_t database_id = 0;
761761
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
762762
settings::SettingId::stats_mode)) != StatsType::INVALID) {
763-
for (const auto &tuple_entry : rw_set.GetConstIterator()) {
763+
for (const auto &tuple_entry : rw_set) {
764764
// Call the GetConstIterator() function to explicitly lock the cuckoohash
765765
// and initilaize the iterator
766766
const auto tile_group_id = tuple_entry.first.block;
@@ -779,7 +779,7 @@ ResultType TimestampOrderingTransactionManager::CommitTransaction(
779779

780780
// TODO (Pooja): This might be inefficient since we will have to get the
781781
// tile_group_header for each entry. Check if this needs to be consolidated
782-
for (const auto &tuple_entry : rw_set.GetConstIterator()) {
782+
for (const auto &tuple_entry : rw_set) {
783783
ItemPointer item_ptr = tuple_entry.first;
784784
oid_t tile_group_id = item_ptr.block;
785785
oid_t tuple_slot = item_ptr.offset;
@@ -939,7 +939,7 @@ ResultType TimestampOrderingTransactionManager::AbortTransaction(
939939
oid_t database_id = 0;
940940
if (static_cast<StatsType>(settings::SettingsManager::GetInt(
941941
settings::SettingId::stats_mode)) != StatsType::INVALID) {
942-
for (const auto &tuple_entry : rw_set.GetConstIterator()) {
942+
for (const auto &tuple_entry : rw_set) {
943943
// Call the GetConstIterator() function to explicitly lock the cuckoohash
944944
// and initilaize the iterator
945945
const auto tile_group_id = tuple_entry.first.block;
@@ -953,7 +953,7 @@ ResultType TimestampOrderingTransactionManager::AbortTransaction(
953953
// Iterate through each item pointer in the read write set
954954
// TODO (Pooja): This might be inefficient since we will have to get the
955955
// tile_group_header for each entry. Check if this needs to be consolidated
956-
for (const auto &tuple_entry : rw_set.GetConstIterator()) {
956+
for (const auto &tuple_entry : rw_set) {
957957
ItemPointer item_ptr = tuple_entry.first;
958958
oid_t tile_group_id = item_ptr.block;
959959
oid_t tuple_slot = item_ptr.offset;

src/concurrency/transaction_context.cpp

Lines changed: 39 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -50,25 +50,14 @@ namespace concurrency {
5050

5151
TransactionContext::TransactionContext(const size_t thread_id,
5252
const IsolationLevelType isolation,
53-
const cid_t &read_id)
54-
: rw_set_(INTITIAL_RW_SET_SIZE) {
53+
const cid_t &read_id) {
5554
Init(thread_id, isolation, read_id);
5655
}
5756

5857
TransactionContext::TransactionContext(const size_t thread_id,
5958
const IsolationLevelType isolation,
6059
const cid_t &read_id,
61-
const cid_t &commit_id)
62-
: rw_set_(INTITIAL_RW_SET_SIZE) {
63-
Init(thread_id, isolation, read_id, commit_id);
64-
}
65-
66-
TransactionContext::TransactionContext(const size_t thread_id,
67-
const IsolationLevelType isolation,
68-
const cid_t &read_id,
69-
const cid_t &commit_id,
70-
const size_t rw_set_size)
71-
: rw_set_(rw_set_size) {
60+
const cid_t &commit_id) {
7261
Init(thread_id, isolation, read_id, commit_id);
7362
}
7463

@@ -95,107 +84,95 @@ void TransactionContext::Init(const size_t thread_id,
9584

9685
insert_count_ = 0;
9786

98-
rw_set_.Clear();
9987
gc_set_.reset(new GCSet());
10088
gc_object_set_.reset(new GCObjectSet());
10189

10290
on_commit_triggers_.reset();
10391
}
10492

10593
RWType TransactionContext::GetRWType(const ItemPointer &location) {
106-
RWType rw_type;
94+
RWType rw_type = RWType::INVALID;
10795

108-
if (!rw_set_.Find(location, rw_type)) {
109-
rw_type = RWType::INVALID;
96+
const auto rw_set_it = rw_set_.find(location);
97+
if (rw_set_it != rw_set_.end()) {
98+
return rw_set_it->second;
11099
}
111100
return rw_type;
112101
}
113102

114103
void TransactionContext::RecordRead(const ItemPointer &location) {
115-
RWType rw_type;
116104

117-
if (rw_set_.Find(location, rw_type)) {
105+
const auto rw_set_it = rw_set_.find(location);
106+
if (rw_set_it != rw_set_.end()) {
107+
UNUSED_ATTRIBUTE RWType rw_type = rw_set_it->second;
118108
PELOTON_ASSERT(rw_type != RWType::DELETE && rw_type != RWType::INS_DEL);
119109
return;
120-
} else {
121-
rw_set_.Insert(location, RWType::READ);
122110
}
111+
rw_set_[location] = RWType::READ;
123112
}
124113

125114
void TransactionContext::RecordReadOwn(const ItemPointer &location) {
126-
RWType rw_type;
127-
128-
if (rw_set_.Find(location, rw_type)) {
115+
const auto rw_set_it = rw_set_.find(location);
116+
if (rw_set_it != rw_set_.end()) {
117+
RWType rw_type = rw_set_it->second;
129118
PELOTON_ASSERT(rw_type != RWType::DELETE && rw_type != RWType::INS_DEL);
130-
if (rw_type == RWType::READ) {
131-
rw_set_.Update(location, RWType::READ_OWN);
119+
if (rw_type != RWType::READ) {
120+
return;
132121
}
133-
} else {
134-
rw_set_.Insert(location, RWType::READ_OWN);
135122
}
123+
rw_set_[location] = RWType::READ_OWN;
136124
}
137125

138126
void TransactionContext::RecordUpdate(const ItemPointer &location) {
139-
RWType rw_type;
140-
141-
if (rw_set_.Find(location, rw_type)) {
127+
const auto rw_set_it = rw_set_.find(location);
128+
if (rw_set_it != rw_set_.end()) {
129+
RWType rw_type = rw_set_it->second;
142130
if (rw_type == RWType::READ || rw_type == RWType::READ_OWN) {
143131
is_written_ = true;
144-
rw_set_.Update(location, RWType::UPDATE);
145-
return;
146-
}
147-
if (rw_type == RWType::UPDATE) {
148-
return;
149-
}
150-
if (rw_type == RWType::INSERT) {
132+
} else if (rw_type == RWType::UPDATE || rw_type == RWType::INSERT) {
151133
return;
152-
}
153-
if (rw_type == RWType::DELETE) {
134+
} else {
135+
// DELETE or INS_DELETE
154136
PELOTON_ASSERT(false);
155137
return;
156138
}
157-
PELOTON_ASSERT(false);
158-
} else {
159-
rw_set_.Insert(location, RWType::UPDATE);
160139
}
140+
rw_set_[location] = RWType::UPDATE;
161141
}
162142

163143
void TransactionContext::RecordInsert(const ItemPointer &location) {
164-
if (IsInRWSet(location)) {
144+
const auto rw_set_it = rw_set_.find(location);
145+
if (rw_set_it != rw_set_.end()) {
165146
PELOTON_ASSERT(false);
166-
} else {
167-
rw_set_.Insert(location, RWType::INSERT);
168-
++insert_count_;
147+
return;
169148
}
149+
rw_set_[location] = RWType::INSERT;
150+
++insert_count_;
170151
}
171152

172153
bool TransactionContext::RecordDelete(const ItemPointer &location) {
173-
RWType rw_type;
154+
const auto rw_set_it = rw_set_.find(location);
155+
if (rw_set_it != rw_set_.end()) {
156+
RWType rw_type = rw_set_it->second;
174157

175-
if (rw_set_.Find(location, rw_type)) {
176158
if (rw_type == RWType::READ || rw_type == RWType::READ_OWN) {
177-
rw_set_.Update(location, RWType::DELETE);
178-
// record write
159+
rw_set_[location] = RWType::DELETE;
179160
is_written_ = true;
180161
return false;
181-
}
182-
if (rw_type == RWType::UPDATE) {
183-
rw_set_.Update(location, RWType::DELETE);
162+
} else if (rw_type == RWType::UPDATE) {
163+
rw_set_[location] = RWType::DELETE;
184164
return false;
185-
}
186-
if (rw_type == RWType::INSERT) {
187-
rw_set_.Update(location, RWType::INS_DEL);
165+
} else if (rw_type == RWType::INSERT) {
166+
rw_set_[location] = RWType::INS_DEL;
188167
--insert_count_;
189168
return true;
190-
}
191-
if (rw_type == RWType::DELETE) {
169+
} else {
170+
// DELETE and INS_DEL
192171
PELOTON_ASSERT(false);
193172
return false;
194173
}
195-
PELOTON_ASSERT(false);
196-
} else {
197-
rw_set_.Insert(location, RWType::DELETE);
198174
}
175+
rw_set_[location] = RWType::DELETE;
199176
return false;
200177
}
201178

src/include/catalog/manager.h

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -20,10 +20,13 @@
2020
#include <memory>
2121
#include "common/internal_types.h"
2222

23-
#include "common/macros.h"
23+
#include "common/container/cuckoo_map.h"
2424
#include "common/internal_types.h"
25-
#include "common/container/lock_free_array.h"
25+
#include "common/macros.h"
2626
#include "tbb/concurrent_unordered_map.h"
27+
28+
#define DEFAULT_LOCATOR_SIZE 1024
29+
2730
namespace peloton {
2831

2932
namespace storage {
@@ -39,7 +42,7 @@ namespace catalog {
3942

4043
class Manager {
4144
public:
42-
Manager() {}
45+
Manager();
4346

4447
// Singleton
4548
static Manager &GetInstance();
@@ -94,8 +97,7 @@ class Manager {
9497
//===--------------------------------------------------------------------===//
9598
std::atomic<oid_t> tile_group_oid_ = ATOMIC_VAR_INIT(START_OID);
9699

97-
tbb::concurrent_unordered_map<oid_t, std::shared_ptr<storage::TileGroup>>
98-
tile_group_locator_;
100+
CuckooMap<oid_t, std::shared_ptr<storage::TileGroup>> tile_group_locator_;
99101
static std::shared_ptr<storage::TileGroup> empty_tile_group_;
100102

101103
//===--------------------------------------------------------------------===//

src/include/common/container/cuckoo_map.h

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,10 @@ class CuckooMap {
5151
// Inserts a item
5252
bool Insert(const KeyType &key, ValueType value);
5353

54+
// Inserts the item if not present, updates value otherwise
55+
// Upsert operations always succeed
56+
void Upsert(const KeyType &key, ValueType value);
57+
5458
// Extracts item with high priority
5559
bool Update(const KeyType &key, ValueType value);
5660

src/include/common/internal_types.h

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,12 @@
2424
#include <unordered_set>
2525
#include <vector>
2626

27+
#include "tbb/concurrent_unordered_map.h"
2728
#include "tbb/concurrent_unordered_set.h"
2829
#include "tbb/concurrent_vector.h"
2930

3031
#include "common/logger.h"
3132
#include "common/macros.h"
32-
#include "container/cuckoo_map.h"
3333
#include "parser/pg_trigger.h"
3434
#include "type/type_id.h"
3535

@@ -1213,7 +1213,8 @@ RWType StringToRWType(const std::string &str);
12131213
std::ostream &operator<<(std::ostream &os, const RWType &type);
12141214

12151215
// ItemPointer -> type
1216-
typedef CuckooMap<ItemPointer, RWType, ItemPointerHasher, ItemPointerComparator>
1216+
typedef tbb::concurrent_unordered_map<ItemPointer, RWType, ItemPointerHasher,
1217+
ItemPointerComparator>
12171218
ReadWriteSet;
12181219

12191220
typedef tbb::concurrent_unordered_set<ItemPointer, ItemPointerHasher,

src/include/concurrency/transaction_context.h

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -24,8 +24,6 @@
2424
#include "common/printable.h"
2525
#include "common/internal_types.h"
2626

27-
#define INTITIAL_RW_SET_SIZE 64
28-
2927
namespace peloton {
3028

3129
namespace trigger {
@@ -51,10 +49,6 @@ class TransactionContext : public Printable {
5149

5250
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
5351
const cid_t &read_id, const cid_t &commit_id);
54-
55-
TransactionContext(const size_t thread_id, const IsolationLevelType isolation,
56-
const cid_t &read_id, const cid_t &commit_id,
57-
const size_t read_write_set_size);
5852

5953
/**
6054
* @brief Destroys the object.
@@ -201,7 +195,7 @@ class TransactionContext : public Printable {
201195
* @return True if in rw set, False otherwise.
202196
*/
203197
bool IsInRWSet(const ItemPointer &location) {
204-
return rw_set_.Contains(location);
198+
return (rw_set_.find(location) != rw_set_.end());
205199
}
206200

207201
/**

0 commit comments

Comments
 (0)