mirror of
https://github.com/facebook/sapling.git
synced 2025-01-06 04:43:19 +03:00
make generateUniqueID noexcept
Summary: In a later diff, I needed generateUniqueID to be noexcept. folly::ThreadLocal does not guarantee that (and it allocates the first time a thread calls get()), so use C++ thread_local instead. Bonus: it's about half a nanosecond faster now. Reviewed By: strager Differential Revision: D12914625 fbshipit-source-id: 9ddbe65d0ba1d317907f821c03dea5a207a73a68
This commit is contained in:
parent
f77d0963bf
commit
46441b3765
@ -9,13 +9,13 @@
|
||||
*/
|
||||
#include "eden/fs/utils/IDGen.h"
|
||||
#include <folly/CachelinePadded.h>
|
||||
#include <folly/ThreadLocal.h>
|
||||
#include <folly/Likely.h>
|
||||
#include <atomic>
|
||||
|
||||
namespace {
|
||||
/**
|
||||
* Allocating one unique ID per nanosecond would wrap around in over 500 years.
|
||||
* Initialized to 1 so that consumers can assume 0 is not a valid id
|
||||
* Initialized to 1 so that consumers can assume 0 is not a valid id.
|
||||
*
|
||||
* CachelinePadded may be excessive here.
|
||||
*/
|
||||
@ -25,8 +25,7 @@ struct LocalRange {
|
||||
uint64_t begin{0};
|
||||
uint64_t end{0};
|
||||
};
|
||||
|
||||
folly::ThreadLocal<LocalRange> localRange;
|
||||
thread_local LocalRange localRange;
|
||||
|
||||
/**
|
||||
* Number of unique IDs to hand out to a thread at a time. This avoids cache
|
||||
@ -44,15 +43,15 @@ constexpr uint64_t kRangeSize = 2000;
|
||||
namespace facebook {
|
||||
namespace eden {
|
||||
|
||||
uint64_t generateUniqueID() {
|
||||
auto range = localRange.get();
|
||||
if (UNLIKELY(range->begin == range->end)) {
|
||||
uint64_t generateUniqueID() noexcept {
|
||||
auto& range = localRange;
|
||||
if (UNLIKELY(range.begin == range.end)) {
|
||||
auto begin =
|
||||
globalCounter->fetch_add(kRangeSize, std::memory_order_relaxed);
|
||||
range->begin = begin;
|
||||
range->end = begin + kRangeSize;
|
||||
range.begin = begin;
|
||||
range.end = begin + kRangeSize;
|
||||
}
|
||||
return range->begin++;
|
||||
return range.begin++;
|
||||
}
|
||||
|
||||
} // namespace eden
|
||||
|
@ -18,10 +18,13 @@ namespace eden {
|
||||
* Very efficiently returns a new uint64_t unique to this process. Amortizes
|
||||
* the cost of synchronizing threads across many ID allocations.
|
||||
*
|
||||
* TODO: It could be beneficial to add a parameter to request more than one
|
||||
* unique ID at a time.
|
||||
* All returned IDs are nonzero.
|
||||
*
|
||||
* TODO: It might be beneficial to add a parameter to request more than one
|
||||
* unique ID at a time, though such an API would make it possible to exhaust
|
||||
* the range of a 64-bit integer.
|
||||
*/
|
||||
uint64_t generateUniqueID();
|
||||
uint64_t generateUniqueID() noexcept;
|
||||
|
||||
} // namespace eden
|
||||
} // namespace facebook
|
||||
|
Loading…
Reference in New Issue
Block a user