From 874dac69f15f1a1abb9ff6bab47a9d982930529b Mon Sep 17 00:00:00 2001 From: CalciteM Team Date: Sat, 14 Sep 2019 23:51:41 +0800 Subject: [PATCH] =?UTF-8?q?perf:=20=E5=93=88=E5=B8=8C=E8=A1=A8=E4=B8=8D?= =?UTF-8?q?=E5=8A=A0=E9=94=81=E5=8E=BB=E6=8E=89=E7=BA=BF=E7=A8=8B=E5=AE=89?= =?UTF-8?q?=E5=85=A8=E6=9C=BA=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 自对弈性能测试: 加锁: 2"93 = 213s 不加锁: 2"33 = 153s 提速 28%. --- include/config.h | 2 ++ src/base/hashmap.h | 6 ++++++ 2 files changed, 8 insertions(+) diff --git a/include/config.h b/include/config.h index 9f7db11d..9108e31f 100644 --- a/include/config.h +++ b/include/config.h @@ -135,6 +135,8 @@ // 不使用哈希桶 #define DISABLE_HASHBUCKET +// 哈希表不加锁 +#define HASHMAP_NOLOCK #ifdef WIN32 #define sscanf sscanf_s diff --git a/src/base/hashmap.h b/src/base/hashmap.h index c5934197..99845181 100644 --- a/src/base/hashmap.h +++ b/src/base/hashmap.h @@ -61,7 +61,9 @@ namespace CTSL //Concurrent Thread Safe Library size_t hashValue = hashFn(key) & (hashSize - 1) ; #ifdef DISABLE_HASHBUCKET // A shared mutex is used to enable multiple concurrent reads +#ifndef HASHMAP_NOLOCK std::shared_lock lock(mutex_); +#endif /* HASHMAP_NOLOCK */ if (hashTable[hashValue].getKey() == key) { value = hashTable[hashValue].getValue(); @@ -80,7 +82,9 @@ namespace CTSL //Concurrent Thread Safe Library { size_t hashValue = hashFn(key) & (hashSize - 1); #ifdef DISABLE_HASHBUCKET +#ifndef HASHMAP_NOLOCK std::unique_lock lock(mutex_); +#endif /* HASHMAP_NOLOCK */ hashTable[hashValue].setKey(key); hashTable[hashValue].setValue(value); #else @@ -151,7 +155,9 @@ namespace CTSL //Concurrent Thread Safe Library #endif const size_t hashSize; #ifdef DISABLE_HASHBUCKET +#ifndef HASHMAP_NOLOCK mutable std::shared_timed_mutex mutex_; +#endif /* HASHMAP_NOLOCK */ #endif }; }