Skip to content

Commit

Permalink
Fast functors
Browse files Browse the repository at this point in the history
  • Loading branch information
morzhovets committed Sep 9, 2023
1 parent 8cc4cda commit ca88d13
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 10 deletions.
21 changes: 13 additions & 8 deletions include/momo/Array.h
Original file line number Diff line number Diff line change
Expand Up @@ -338,16 +338,17 @@ class Array
}
}

template<bool grow, typename ItemsCreator>
void Reset(size_t capacity, size_t count, ItemsCreator itemsCreator)
template<bool grow, internal::conceptMoveFunctor<void, Item*> ItemsCreator>
void Reset(size_t capacity, size_t count,
internal::FastMovableFunctor<ItemsCreator> itemsCreator)
{
MOMO_ASSERT(count <= capacity);
if (grow || capacity > internalCapacity)
{
Item* items = pvAllocate(capacity);
try
{
itemsCreator(items);
std::move(itemsCreator)(items);
}
catch (...)
{
Expand All @@ -371,7 +372,7 @@ class Array
{
MOMO_ASSERT(!pvIsInternal());
size_t initCapacity = mCapacity;
itemsCreator(&mInternalItems);
std::move(itemsCreator)(&mInternalItems);
pvDeallocate(mItems, initCapacity);
mItems = &mInternalItems;
mCount = count;
Expand Down Expand Up @@ -683,7 +684,8 @@ class Array
{
auto itemsCreator = [this, count] (Item* newItems)
{ ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); };
mData.template Reset<false>(capacity, count, itemsCreator);
mData.template Reset<false>(capacity, count,
internal::FastMovableFunctor(std::move(itemsCreator)));
}
}

Expand Down Expand Up @@ -937,7 +939,8 @@ class Array
size_t count = GetCount();
auto itemsCreator = [this, count] (Item* newItems)
{ ItemTraits::Relocate(GetMemManager(), GetItems(), newItems, count); };
mData.template Reset<true>(newCapacityExp, count, itemsCreator);
mData.template Reset<true>(newCapacityExp, count,
internal::FastMovableFunctor(std::move(itemsCreator)));
}
}

Expand Down Expand Up @@ -984,7 +987,8 @@ class Array
throw;
}
};
mData.template Reset<true>(newCapacity, newCount, itemsCreator);
mData.template Reset<true>(newCapacity, newCount,
internal::FastMovableFunctor(std::move(itemsCreator)));
}
}

Expand Down Expand Up @@ -1024,7 +1028,8 @@ class Array
ItemTraits::RelocateCreate(GetMemManager(), GetItems(), newItems, initCount,
std::move(itemCreator), newItems + initCount);
};
mData.template Reset<true>(newCapacity, newCount, std::move(itemsCreator));
mData.template Reset<true>(newCapacity, newCount,
internal::FastMovableFunctor(std::move(itemsCreator)));
}

template<internal::conceptTrivialObjectCreator<Item> ItemCreator>
Expand Down
4 changes: 2 additions & 2 deletions include/momo/Utility.h
Original file line number Diff line number Diff line change
Expand Up @@ -194,7 +194,7 @@ namespace internal
BaseFunctor, BaseFunctor&&> BaseFunctorReference;

public:
explicit FastMovableFunctor(BaseFunctorReference baseFunctor) noexcept
explicit FastMovableFunctor(BaseFunctor&& baseFunctor) noexcept
: mBaseFunctor(std::forward<BaseFunctor>(baseFunctor))
{
}
Expand Down Expand Up @@ -229,7 +229,7 @@ namespace internal
BaseFunctor, const BaseFunctor&> BaseFunctorReference;

public:
explicit FastCopyableFunctor(BaseFunctorReference baseFunctor) noexcept
explicit FastCopyableFunctor(const BaseFunctor& baseFunctor) noexcept
: mBaseFunctor(baseFunctor)
{
}
Expand Down

0 comments on commit ca88d13

Please sign in to comment.