Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2013 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #ifndef ART_COMPILER_UTILS_DEDUPE_SET_H_ |
| 18 | #define ART_COMPILER_UTILS_DEDUPE_SET_H_ |
| 19 | |
| 20 | #include <set> |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 21 | #include <string> |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 22 | |
| 23 | #include "base/mutex.h" |
| 24 | #include "base/stl_util.h" |
| 25 | |
| 26 | namespace art { |
| 27 | |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 28 | // A set of Keys that support a HashFunc returning HashType. Used to find duplicates of Key in the |
| 29 | // Add method. The data-structure is thread-safe through the use of internal locks, it also |
| 30 | // supports the lock being sharded. |
| 31 | template <typename Key, typename HashType, typename HashFunc, HashType kShard = 1> |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 32 | class DedupeSet { |
| 33 | typedef std::pair<HashType, Key*> HashedKey; |
| 34 | |
| 35 | class Comparator { |
| 36 | public: |
| 37 | bool operator()(const HashedKey& a, const HashedKey& b) const { |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 38 | if (a.first != b.first) { |
| 39 | return a.first < b.first; |
| 40 | } else { |
| 41 | return *a.second < *b.second; |
| 42 | } |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 43 | } |
| 44 | }; |
| 45 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 46 | public: |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 47 | Key* Add(Thread* self, const Key& key) { |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 48 | HashType raw_hash = HashFunc()(key); |
| 49 | HashType shard_hash = raw_hash / kShard; |
| 50 | HashType shard_bin = raw_hash % kShard; |
| 51 | HashedKey hashed_key(shard_hash, const_cast<Key*>(&key)); |
| 52 | MutexLock lock(self, *lock_[shard_bin]); |
| 53 | auto it = keys_[shard_bin].find(hashed_key); |
| 54 | if (it != keys_[shard_bin].end()) { |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 55 | return it->second; |
| 56 | } |
| 57 | hashed_key.second = new Key(key); |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 58 | keys_[shard_bin].insert(hashed_key); |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 59 | return hashed_key.second; |
| 60 | } |
| 61 | |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 62 | explicit DedupeSet(const char* set_name) { |
| 63 | for (HashType i = 0; i < kShard; ++i) { |
| 64 | lock_name_[i] = StringPrintf("%s lock %d", set_name, i); |
| 65 | lock_[i].reset(new Mutex(lock_name_[i].c_str())); |
| 66 | } |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | ~DedupeSet() { |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 70 | for (HashType i = 0; i < kShard; ++i) { |
| 71 | STLDeleteValues(&keys_[i]); |
| 72 | } |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 73 | } |
| 74 | |
| 75 | private: |
Ian Rogers | d133b97 | 2013-09-05 11:01:30 -0700 | [diff] [blame^] | 76 | std::string lock_name_[kShard]; |
| 77 | UniquePtr<Mutex> lock_[kShard]; |
| 78 | std::set<HashedKey, Comparator> keys_[kShard]; |
| 79 | |
Mathieu Chartier | 193bad9 | 2013-08-29 18:46:00 -0700 | [diff] [blame] | 80 | DISALLOW_COPY_AND_ASSIGN(DedupeSet); |
| 81 | }; |
| 82 | |
| 83 | } // namespace art |
| 84 | |
| 85 | #endif // ART_COMPILER_UTILS_DEDUPE_SET_H_ |