/*
- * Copyright 2015 Facebook, Inc.
+ * Copyright 2017 Facebook, Inc.
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
/**
* AtomicHashArray is the building block for AtomicHashMap. It provides the
- * core lock-free functionality, but is limitted by the fact that it cannot
- * grow past it's initialization size and is a little more awkward (no public
+ * core lock-free functionality, but is limited by the fact that it cannot
+ * grow past its initialization size and is a little more awkward (no public
* constructor, for example). If you're confident that you won't run out of
* space, don't mind the awkardness, and really need bare-metal performance,
* feel free to use AHA directly.
* @author Jordan DeLong <delong.j@fb.com>
*/
-#ifndef FOLLY_ATOMICHASHARRAY_H_
+#pragma once
#define FOLLY_ATOMICHASHARRAY_H_
#include <atomic>
#include <folly/Hash.h>
#include <folly/ThreadCachedInt.h>
+#include <folly/Utility.h>
namespace folly {
struct AtomicHashArrayLinearProbeFcn
{
- inline size_t operator()(size_t idx, size_t numProbes, size_t capacity) const{
+ inline size_t operator()(size_t idx,
+ size_t /* numProbes */,
+ size_t capacity) const {
idx += 1; // linear probing
// Avoid modulus because it's slow
}
};
-template <class KeyT, class ValueT,
- class HashFcn = std::hash<KeyT>,
- class EqualFcn = std::equal_to<KeyT>,
- class Allocator = std::allocator<char>,
- class ProbeFcn = AtomicHashArrayLinearProbeFcn>
+// Enables specializing checkLegalKey without specializing its class.
+namespace detail {
+template <typename NotKeyT, typename KeyT>
+inline void checkLegalKeyIfKeyTImpl(NotKeyT /* ignored */,
+ KeyT /* emptyKey */,
+ KeyT /* lockedKey */,
+ KeyT /* erasedKey */) {}
+
+template <typename KeyT>
+inline void checkLegalKeyIfKeyTImpl(KeyT key_in, KeyT emptyKey,
+ KeyT lockedKey, KeyT erasedKey) {
+ DCHECK_NE(key_in, emptyKey);
+ DCHECK_NE(key_in, lockedKey);
+ DCHECK_NE(key_in, erasedKey);
+}
+} // namespace detail
+
+template <
+ class KeyT,
+ class ValueT,
+ class HashFcn = std::hash<KeyT>,
+ class EqualFcn = std::equal_to<KeyT>,
+ class Allocator = std::allocator<char>,
+ class ProbeFcn = AtomicHashArrayLinearProbeFcn,
+ class KeyConvertFcn = Identity>
class AtomicHashMap;
-template <class KeyT, class ValueT,
- class HashFcn = std::hash<KeyT>,
- class EqualFcn = std::equal_to<KeyT>,
- class Allocator = std::allocator<char>,
- class ProbeFcn = AtomicHashArrayLinearProbeFcn>
+template <
+ class KeyT,
+ class ValueT,
+ class HashFcn = std::hash<KeyT>,
+ class EqualFcn = std::equal_to<KeyT>,
+ class Allocator = std::allocator<char>,
+ class ProbeFcn = AtomicHashArrayLinearProbeFcn,
+ class KeyConvertFcn = Identity>
class AtomicHashArray : boost::noncopyable {
static_assert((std::is_convertible<KeyT,int32_t>::value ||
std::is_convertible<KeyT,int64_t>::value ||
public:
typedef KeyT key_type;
typedef ValueT mapped_type;
+ typedef HashFcn hasher;
+ typedef EqualFcn key_equal;
+ typedef KeyConvertFcn key_convert;
typedef std::pair<const KeyT, ValueT> value_type;
typedef std::size_t size_type;
typedef std::ptrdiff_t difference_type;
* deleter to make sure everything is cleaned up properly.
*/
struct Config {
- KeyT emptyKey;
- KeyT lockedKey;
- KeyT erasedKey;
+ KeyT emptyKey;
+ KeyT lockedKey;
+ KeyT erasedKey;
double maxLoadFactor;
double growthFactor;
- int entryCountThreadCacheSize;
+ uint32_t entryCountThreadCacheSize;
size_t capacity; // if positive, overrides maxLoadFactor
public:
// Cannot have pre-instantiated const Config instance because of SIOF.
static SmartPtr create(size_t maxSize, const Config& c = Config());
- iterator find(KeyT k) {
- return iterator(this, findInternal(k).idx);
+ /*
+ * find --
+ *
+ *
+ * Returns the iterator to the element if found, otherwise end().
+ *
+ * As an optional feature, the type of the key to look up (LookupKeyT) is
+ * allowed to be different from the type of keys actually stored (KeyT).
+ *
+ * This enables use cases where materializing the key is costly and usually
+ * redudant, e.g., canonicalizing/interning a set of strings and being able
+ * to look up by StringPiece. To use this feature, LookupHashFcn must take
+ * a LookupKeyT, and LookupEqualFcn must take KeyT and LookupKeyT as first
+ * and second parameter, respectively.
+ *
+ * See folly/test/ArrayHashArrayTest.cpp for sample usage.
+ */
+ template <typename LookupKeyT = key_type,
+ typename LookupHashFcn = hasher,
+ typename LookupEqualFcn = key_equal>
+ iterator find(LookupKeyT k) {
+ return iterator(this,
+ findInternal<LookupKeyT, LookupHashFcn, LookupEqualFcn>(k).idx);
}
- const_iterator find(KeyT k) const {
- return const_cast<AtomicHashArray*>(this)->find(k);
+
+ template <typename LookupKeyT = key_type,
+ typename LookupHashFcn = hasher,
+ typename LookupEqualFcn = key_equal>
+ const_iterator find(LookupKeyT k) const {
+ return const_cast<AtomicHashArray*>(this)->
+ find<LookupKeyT, LookupHashFcn, LookupEqualFcn>(k);
}
/*
*
* Same contract as insert(), but performs in-place construction
* of the value type using the specified arguments.
+ *
+ * Also, like find(), this method optionally allows 'key_in' to have a type
+ * different from that stored in the table; see find(). If and only if no
+ * equal key is already present, this method converts 'key_in' to a key of
+ * type KeyT using the provided LookupKeyToKeyFcn.
*/
- template <typename... ArgTs>
- std::pair<iterator,bool> emplace(KeyT key_in, ArgTs&&... vCtorArgs) {
- SimpleRetT ret = insertInternal(key_in, std::forward<ArgTs>(vCtorArgs)...);
+ template <typename LookupKeyT = key_type,
+ typename LookupHashFcn = hasher,
+ typename LookupEqualFcn = key_equal,
+ typename LookupKeyToKeyFcn = key_convert,
+ typename... ArgTs>
+ std::pair<iterator,bool> emplace(LookupKeyT key_in, ArgTs&&... vCtorArgs) {
+ SimpleRetT ret = insertInternal<LookupKeyT,
+ LookupHashFcn,
+ LookupEqualFcn,
+ LookupKeyToKeyFcn>(
+ key_in,
+ std::forward<ArgTs>(vCtorArgs)...);
return std::make_pair(iterator(this, ret.idx), ret.success);
}
numPendingEntries_.setCacheSize(newSize);
}
- int getEntryCountThreadCacheSize() const {
+ uint32_t getEntryCountThreadCacheSize() const {
return numEntries_.getCacheSize();
}
SimpleRetT() = default;
};
-
-
- template <typename... ArgTs>
- SimpleRetT insertInternal(KeyT key, ArgTs&&... vCtorArgs);
-
- SimpleRetT findInternal(const KeyT key);
+ template <
+ typename LookupKeyT = key_type,
+ typename LookupHashFcn = hasher,
+ typename LookupEqualFcn = key_equal,
+ typename LookupKeyToKeyFcn = Identity,
+ typename... ArgTs>
+ SimpleRetT insertInternal(LookupKeyT key, ArgTs&&... vCtorArgs);
+
+ template <typename LookupKeyT = key_type,
+ typename LookupHashFcn = hasher,
+ typename LookupEqualFcn = key_equal>
+ SimpleRetT findInternal(const LookupKeyT key);
+
+ template <typename MaybeKeyT>
+ void checkLegalKeyIfKey(MaybeKeyT key) {
+ detail::checkLegalKeyIfKeyTImpl(key, kEmptyKey_, kLockedKey_, kErasedKey_);
+ }
static std::atomic<KeyT>* cellKeyPtr(const value_type& r) {
// We need some illegal casting here in order to actually store
// Force constructor/destructor private since create/destroy should be
// used externally instead
- AtomicHashArray(size_t capacity, KeyT emptyKey, KeyT lockedKey,
- KeyT erasedKey, double maxLoadFactor, size_t cacheSize);
+ AtomicHashArray(
+ size_t capacity,
+ KeyT emptyKey,
+ KeyT lockedKey,
+ KeyT erasedKey,
+ double maxLoadFactor,
+ uint32_t cacheSize);
~AtomicHashArray() = default;
std::memory_order_acq_rel);
}
- inline size_t keyToAnchorIdx(const KeyT k) const {
- const size_t hashVal = HashFcn()(k);
+ template <class LookupKeyT = key_type, class LookupHashFcn = hasher>
+ inline size_t keyToAnchorIdx(const LookupKeyT k) const {
+ const size_t hashVal = LookupHashFcn()(k);
const size_t probe = hashVal & kAnchorMask_;
return LIKELY(probe < capacity_) ? probe : hashVal % capacity_;
}
} // namespace folly
#include <folly/AtomicHashArray-inl.h>
-
-#endif // FOLLY_ATOMICHASHARRAY_H_