Skip to content

Commit

Permalink
Fast functors
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Sep 10, 2023
1 parent 11e88e2 commit 713d978
Show file tree
Hide file tree
Showing 14 changed files with 159 additions and 136 deletions.
16 changes: 8 additions & 8 deletions include/momo/HashMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -671,7 +671,7 @@ class HashMap
};
extPair.Remove(pairRemover);
};
return pvAdd<true>(pos, std::move(pairCreator));
return pvAdd<true>(pos, FastMovableFunctor(std::move(pairCreator)));
}
}
return PositionProxy(mHashSet.Add(ConstPositionProxy::GetHashSetPosition(pos),
Expand Down Expand Up @@ -811,8 +811,8 @@ class HashMap
}

private:
template<typename RKey, internal::conceptTrivialObjectCreator<Value> ValueCreator>
InsertResult pvInsert(RKey&& key, ValueCreator valueCreator)
template<typename RKey, internal::conceptObjectCreator<Value> ValueCreator>
InsertResult pvInsert(RKey&& key, FastMovableFunctor<ValueCreator> valueCreator)
{
auto itemCreator = [this, &key, valueCreator = std::move(valueCreator)]
(KeyValuePair* newItem) mutable
Expand All @@ -825,17 +825,17 @@ class HashMap
return { PositionProxy(res.position), res.inserted };
}

template<bool extraCheck, internal::conceptTrivialMapPairCreator<Key, Value> PairCreator>
Position pvAdd(ConstPosition pos, PairCreator pairCreator)
template<bool extraCheck, internal::conceptMapPairCreator<Key, Value> PairCreator>
Position pvAdd(ConstPosition pos, FastMovableFunctor<PairCreator> pairCreator)
{
auto itemCreator = [this, pairCreator = std::move(pairCreator)] (KeyValuePair* newItem) mutable
{ std::construct_at(newItem, mHashSet.GetMemManager(), std::move(pairCreator)); };
return PositionProxy(mHashSet.template AddCrt<decltype(itemCreator), extraCheck>(
ConstPositionProxy::GetHashSetPosition(pos), std::move(itemCreator)));
}

template<bool extraCheck, typename RKey, internal::conceptTrivialObjectCreator<Value> ValueCreator>
Position pvAdd(ConstPosition pos, RKey&& key, ValueCreator valueCreator)
template<bool extraCheck, typename RKey, internal::conceptObjectCreator<Value> ValueCreator>
Position pvAdd(ConstPosition pos, RKey&& key, FastMovableFunctor<ValueCreator> valueCreator)
{
auto itemCreator = [this, &key, valueCreator = std::move(valueCreator)]
(KeyValuePair* newItem) mutable
Expand All @@ -852,7 +852,7 @@ class HashMap
{
MemManager& memManager = GetMemManager();
InsertResult res = pvInsert(std::forward<RKey>(key),
ValueCreator<ValueArg>(memManager, std::forward<ValueArg>(valueArg)));
FastMovableFunctor(ValueCreator<ValueArg>(memManager, std::forward<ValueArg>(valueArg))));
if (!res.inserted)
KeyValueTraits::AssignValue(memManager, std::forward<ValueArg>(valueArg), res.position->value);
return res;
Expand Down
16 changes: 8 additions & 8 deletions include/momo/HashMultiMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -1226,9 +1226,9 @@ class HashMultiMap
return pvMakeIterator(keyIter, valueIndex, true);
}

template<typename RKey, internal::conceptTrivialObjectCreator<Value> ValueCreator>
template<typename RKey, internal::conceptObjectCreator<Value> ValueCreator>
requires std::is_same_v<Key, std::decay_t<RKey>>
Iterator pvAdd(RKey&& key, ValueCreator valueCreator)
Iterator pvAdd(RKey&& key, FastMovableFunctor<ValueCreator> valueCreator)
{
KeyIterator keyIter = Find(std::as_const(key));
if (!!keyIter)
Expand All @@ -1246,8 +1246,8 @@ class HashMultiMap
return pvMakeIterator(keyIter, 0, false);
}

template<internal::conceptTrivialObjectCreator<Value> ValueCreator>
Iterator pvAdd(ConstKeyIterator keyIter, ValueCreator valueCreator)
template<internal::conceptObjectCreator<Value> ValueCreator>
Iterator pvAdd(ConstKeyIterator keyIter, FastMovableFunctor<ValueCreator> valueCreator)
{
HashMapIterator hashMapIter = mHashMap.MakeMutableIterator(
ConstKeyIteratorProxy::GetBaseIterator(keyIter));
Expand All @@ -1257,8 +1257,8 @@ class HashMultiMap
valueArray.GetBounds().GetCount() - 1, false);
}

template<internal::conceptTrivialObjectCreator<Key> KeyCreator>
KeyIterator pvAddKey(ConstKeyIterator keyIter, KeyCreator keyCreator)
template<internal::conceptObjectCreator<Key> KeyCreator>
KeyIterator pvAddKey(ConstKeyIterator keyIter, FastMovableFunctor<KeyCreator> keyCreator)
{
auto pairCreator = [keyCreator = std::move(keyCreator)]
(Key* newKey, ValueArray* newValueArray) mutable
Expand All @@ -1270,8 +1270,8 @@ class HashMultiMap
ConstKeyIteratorProxy::GetBaseIterator(keyIter), std::move(pairCreator)));
}

template<internal::conceptTrivialObjectCreator<Value> ValueCreator>
void pvAddValue(ValueArray& valueArray, ValueCreator valueCreator)
template<internal::conceptObjectCreator<Value> ValueCreator>
void pvAddValue(ValueArray& valueArray, FastMovableFunctor<ValueCreator> valueCreator)
{
valueArray.AddBackCrt(mValueCrew.GetValueArrayParams(), std::move(valueCreator));
++mValueCount;
Expand Down
52 changes: 28 additions & 24 deletions include/momo/HashSet.h
Original file line number Diff line number Diff line change
Expand Up @@ -593,7 +593,7 @@ class HashSet
{
size_t hashCode = hashTraits.GetHashCode(ItemTraits::GetKey(item));
pvAddNogrow<false>(*mBuckets, hashCode,
Creator<const Item&>(GetMemManager(), item));
FastMovableFunctor(Creator<const Item&>(GetMemManager(), item)));
}
}
catch (...)
Expand Down Expand Up @@ -787,7 +787,8 @@ class HashSet
{ ItemTraits::Relocate(nullptr, &GetMemManager(), item, newItem); };
extItem.Remove(itemRemover);
};
return pvInsert<false>(ItemTraits::GetKey(extItem.GetItem()), itemCreator);
return pvInsert<false>(ItemTraits::GetKey(extItem.GetItem()),
FastMovableFunctor(std::move(itemCreator)));
}

template<internal::conceptSetArgIterator<Item> ArgIterator>
Expand Down Expand Up @@ -838,14 +839,14 @@ class HashSet
{ ItemTraits::Relocate(nullptr, &GetMemManager(), item, newItem); };
extItem.Remove(itemRemover);
};
return pvAdd<true>(pos, std::move(itemCreator));
return pvAdd<true>(pos, FastMovableFunctor(std::move(itemCreator)));
}

Iterator Remove(ConstIterator iter)
{
auto itemReplacer = [this] (Item& srcItem, Item& dstItem)
{ ItemTraits::Replace(GetMemManager(), srcItem, dstItem); };
return pvRemove(iter, itemReplacer);
return pvRemove(iter, FastMovableFunctor(std::move(itemReplacer)));
}

void Remove(ConstPosition pos)
Expand All @@ -866,7 +867,7 @@ class HashSet
else
ItemTraits::ReplaceRelocate(memManager, srcItem, dstItem, newItem);
};
resIter = pvRemove(iter, itemReplacer);
resIter = pvRemove(iter, FastMovableFunctor(std::move(itemReplacer)));
};
extItem.Create(itemCreator);
return resIter;
Expand Down Expand Up @@ -1052,7 +1053,7 @@ class HashSet
Buckets* buckets = mBuckets;
while (true)
{
bucketIter = pvFind(indexCode, *buckets, pred);
bucketIter = pvFind(indexCode, *buckets, FastCopyableFunctor(pred));
if (bucketIter != BucketIterator() || areItemsNothrowRelocatable)
break;
buckets = buckets->GetNextBuckets();
Expand All @@ -1063,8 +1064,9 @@ class HashSet
return PositionProxy(indexCode, bucketIter, mCrew.GetVersion());
}

template<internal::conceptTrivialObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE BucketIterator pvFind(size_t& indexCode, Buckets& buckets, Predicate pred) const
template<internal::conceptObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE BucketIterator pvFind(size_t& indexCode, Buckets& buckets,
FastCopyableFunctor<Predicate> pred) const
{
size_t hashCode = indexCode;
BucketParams& bucketParams = buckets.GetBucketParams();
Expand Down Expand Up @@ -1092,8 +1094,8 @@ class HashSet
return BucketIterator();
}

template<bool extraCheck, internal::conceptTrivialObjectCreator<Item> ItemCreator>
InsertResult pvInsert(const Key& key, ItemCreator itemCreator)
template<bool extraCheck, internal::conceptObjectCreator<Item> ItemCreator>
InsertResult pvInsert(const Key& key, FastMovableFunctor<ItemCreator> itemCreator)
{
Position pos = pvFind(key);
if (!!pos)
Expand All @@ -1102,8 +1104,8 @@ class HashSet
return { pos, true };
}

template<bool extraCheck, internal::conceptTrivialObjectCreator<Item> ItemCreator>
Position pvAdd(ConstPosition pos, ItemCreator itemCreator)
template<bool extraCheck, internal::conceptObjectCreator<Item> ItemCreator>
Position pvAdd(ConstPosition pos, FastMovableFunctor<ItemCreator> itemCreator)
{
ConstPositionProxy::Check(pos, mCrew.GetVersion(), false);
MOMO_CHECK(ConstPositionProxy::GetBucketIterator(pos) == BucketIterator());
Expand All @@ -1119,8 +1121,9 @@ class HashSet
return resPos;
}

template<bool incCount, internal::conceptTrivialObjectCreator<Item> ItemCreator>
Position pvAddNogrow(Buckets& buckets, size_t hashCode, ItemCreator itemCreator)
template<bool incCount, internal::conceptObjectCreator<Item> ItemCreator>
Position pvAddNogrow(Buckets& buckets, size_t hashCode,
FastMovableFunctor<ItemCreator> itemCreator)
{
size_t bucketCount = buckets.GetCount();
size_t bucketIndex = Bucket::GetStartBucketIndex(hashCode, bucketCount);
Expand All @@ -1146,8 +1149,8 @@ class HashSet
return PositionProxy(bucketIndex, bucketIter, mCrew.GetVersion());
}

template<internal::conceptTrivialObjectCreator<Item> ItemCreator>
MOMO_NOINLINE Position pvAddGrow(size_t hashCode, ItemCreator itemCreator)
template<internal::conceptObjectCreator<Item> ItemCreator>
MOMO_NOINLINE Position pvAddGrow(size_t hashCode, FastMovableFunctor<ItemCreator> itemCreator)
{
const HashTraits& hashTraits = GetHashTraits();
size_t newLogBucketCount = pvGetNewLogBucketCount();
Expand Down Expand Up @@ -1183,8 +1186,8 @@ class HashSet
return resPos;
}

template<internal::conceptTrivialObjectReplacer<Item> ItemReplacer>
Iterator pvRemove(ConstIterator iter, ItemReplacer itemReplacer)
template<internal::conceptObjectReplacer<Item> ItemReplacer>
Iterator pvRemove(ConstIterator iter, FastMovableFunctor<ItemReplacer> itemReplacer)
{
MOMO_CHECK(mBuckets != nullptr);
Position pos = iter;
Expand All @@ -1194,7 +1197,7 @@ class HashSet
size_t bucketIndex = ConstPositionProxy::GetBucketIndex(pos);
Buckets* buckets = pvFindBuckets(bucketIndex, bucketIter);
Bucket& bucket = (*buckets)[bucketIndex];
bucketIter = bucket.Remove(buckets->GetBucketParams(), bucketIter, itemReplacer);
bucketIter = bucket.Remove(buckets->GetBucketParams(), bucketIter, std::move(itemReplacer));
--mCount;
mCrew.IncVersion();
if (!ConstIteratorProxy::IsMovable(iter))
Expand Down Expand Up @@ -1270,17 +1273,18 @@ class HashSet
for (size_t c = bucketBounds.GetCount(); c > 0; --c)
{
--bucketIter;
size_t hashCode = bucket.GetHashCodePart(hashCodeFullGetter, bucketIter, i,
buckets->GetLogCount(), mBuckets->GetLogCount());
size_t hashCode = bucket.GetHashCodePart(FastCopyableFunctor(hashCodeFullGetter),
bucketIter, i, buckets->GetLogCount(), mBuckets->GetLogCount());
auto itemReplacer = [this, &memManager, hashCode]
([[maybe_unused]] Item& srcItem, Item& dstItem)
{
MOMO_ASSERT(std::addressof(srcItem) == std::addressof(dstItem));
auto itemCreator = [&memManager, &dstItem] (Item* newItem)
{ ItemTraits::Relocate(&memManager, &memManager, dstItem, newItem); };
pvAddNogrow<false>(*mBuckets, hashCode, itemCreator);
pvAddNogrow<false>(*mBuckets, hashCode, FastMovableFunctor(std::move(itemCreator)));
};
bucketIter = bucket.Remove(bucketParams, bucketIter, itemReplacer);
bucketIter = bucket.Remove(bucketParams, bucketIter,
FastMovableFunctor(std::move(itemReplacer)));
}
}
buckets->Destroy(memManager, false);
Expand All @@ -1302,7 +1306,7 @@ class HashSet
MOMO_ASSERT(std::addressof(srcItem) == std::addressof(dstItem));
ItemTraits::Relocate(&memManager, &dstMemManager, dstItem, newItem);
};
iter = pvRemove(iter, itemReplacer);
iter = pvRemove(iter, FastMovableFunctor(std::move(itemReplacer)));
};
if (!dstSet.InsertCrt(ItemTraits::GetKey(*iter), std::move(itemCreator)).inserted)
++iter;
Expand Down
3 changes: 3 additions & 0 deletions include/momo/ObjectManager.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,9 @@ namespace internal
template<typename Remover, typename Object>
concept conceptObjectRemover = conceptMoveFunctor<Remover, void, Object&>;

template<typename Replacer, typename Object>
concept conceptObjectReplacer = conceptMoveFunctor<Replacer, void, Object&, Object&>;

template<typename Predicate, typename Object>
concept conceptObjectPredicate = conceptPredicate<Predicate, const Object&>;

Expand Down
7 changes: 4 additions & 3 deletions include/momo/details/BucketUtility.h
Original file line number Diff line number Diff line change
Expand Up @@ -105,9 +105,10 @@ namespace internal
{
}

template<conceptTrivialConstFunctor<size_t> HashCodeFullGetter, typename Iterator> //?
size_t GetHashCodePart(HashCodeFullGetter hashCodeFullGetter, Iterator /*iter*/,
size_t /*bucketIndex*/, size_t /*logBucketCount*/, size_t /*newLogBucketCount*/)
template<conceptConstFunctor<size_t> HashCodeFullGetter, typename Iterator> //?
size_t GetHashCodePart(FastCopyableFunctor<HashCodeFullGetter> hashCodeFullGetter,
Iterator /*iter*/, size_t /*bucketIndex*/, size_t /*logBucketCount*/,
size_t /*newLogBucketCount*/)
{
return hashCodeFullGetter();
}
Expand Down
15 changes: 8 additions & 7 deletions include/momo/details/HashBucketLim4.h
Original file line number Diff line number Diff line change
Expand Up @@ -136,8 +136,9 @@ namespace internal
return Bounds(items, data.count);
}

template<bool first, conceptTrivialObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& params, Predicate pred, size_t /*hashCode*/)
template<bool first, conceptObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& params,
FastCopyableFunctor<Predicate> pred, size_t /*hashCode*/)
{
for (Item& item : GetBounds(params))
{
Expand Down Expand Up @@ -168,9 +169,9 @@ namespace internal
mPtrState = stateNull;
}

template<conceptTrivialObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, ItemCreator itemCreator, size_t /*hashCode*/,
size_t /*logBucketCount*/, size_t /*probe*/)
template<conceptObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, FastMovableFunctor<ItemCreator> itemCreator,
size_t /*hashCode*/, size_t /*logBucketCount*/, size_t /*probe*/)
{
if (pvIsEmpty())
{
Expand Down Expand Up @@ -216,8 +217,8 @@ namespace internal
}
}

template<conceptTrivialObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, ItemReplacer itemReplacer)
template<conceptObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, FastMovableFunctor<ItemReplacer> itemReplacer)
{
MOMO_ASSERT(!pvIsEmpty());
Data data = pvGetData();
Expand Down
30 changes: 16 additions & 14 deletions include/momo/details/HashBucketLimP.h
Original file line number Diff line number Diff line change
Expand Up @@ -128,8 +128,9 @@ namespace internal
return Bounds(pvGetItems(), pvGetCount());
}

template<bool first, conceptTrivialObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, Predicate pred, size_t /*hashCode*/)
template<bool first, conceptObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
FastCopyableFunctor<Predicate> pred, size_t /*hashCode*/)
{
if (pvIsEmpty())
return nullptr;
Expand Down Expand Up @@ -170,9 +171,9 @@ namespace internal
mPtr = ptrNull;
}

template<conceptTrivialObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, ItemCreator itemCreator, size_t /*hashCode*/,
size_t /*logBucketCount*/, size_t /*probe*/)
template<conceptObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, FastMovableFunctor<ItemCreator> itemCreator,
size_t /*hashCode*/, size_t /*logBucketCount*/, size_t /*probe*/)
{
if (pvIsEmpty())
{
Expand Down Expand Up @@ -212,8 +213,8 @@ namespace internal
}
}

template<conceptTrivialObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, ItemReplacer itemReplacer)
template<conceptObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, FastMovableFunctor<ItemReplacer> itemReplacer)
{
size_t count = pvGetCount();
MOMO_ASSERT(count > 0);
Expand Down Expand Up @@ -404,8 +405,9 @@ namespace internal
return pvGetBounds();
}

template<bool first, conceptTrivialObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/, Predicate pred, size_t /*hashCode*/)
template<bool first, conceptObjectPredicate<Item> Predicate>
MOMO_FORCEINLINE Iterator Find(Params& /*params*/,
FastCopyableFunctor<Predicate> pred, size_t /*hashCode*/)
{
if (pvIsEmpty())
return nullptr;
Expand Down Expand Up @@ -444,9 +446,9 @@ namespace internal
mPtrState = stateNull;
}

template<conceptTrivialObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, ItemCreator itemCreator, size_t /*hashCode*/,
size_t /*logBucketCount*/, size_t /*probe*/)
template<conceptObjectCreator<Item> ItemCreator>
Iterator AddCrt(Params& params, FastMovableFunctor<ItemCreator> itemCreator,
size_t /*hashCode*/, size_t /*logBucketCount*/, size_t /*probe*/)
{
if (pvIsEmpty())
{
Expand Down Expand Up @@ -488,8 +490,8 @@ namespace internal
}
}

template<conceptTrivialObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, ItemReplacer itemReplacer)
template<conceptObjectReplacer<Item> ItemReplacer>
Iterator Remove(Params& params, Iterator iter, FastMovableFunctor<ItemReplacer> itemReplacer)
{
MOMO_ASSERT(!pvIsEmpty());
Bounds bounds = pvGetBounds();
Expand Down
Loading

0 comments on commit 713d978

Please sign in to comment.