1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660 |
- #pragma once
- #include <yql/essentials/utils/hash.h>
- #include "aligned_page_pool.h"
- #include "primes.h"
- #include <util/generic/vector.h>
- #include <util/generic/ptr.h>
- #include <util/generic/hash.h>
- #include <util/generic/algorithm.h>
- #include <util/generic/bitops.h>
- #include <util/generic/utility.h>
- #include <util/generic/strbuf.h>
- #include <util/generic/mem_copy.h>
- #include <util/generic/scope.h>
- #include <util/system/defaults.h>
- #include <util/system/unaligned_mem.h>
- #include <util/system/align.h>
- #include <util/stream/output.h>
- #include <type_traits>
- #include <cmath>
- #include <array>
- // TODO: Allow only PODs in key/value. Use runtime key/value sizes instead of compile time type instantiation. Use Read/WriteUnaligned
- namespace NKikimr {
- namespace NCHash {
- class TListPoolBase {
- public:
- enum {
- SMALL_MARK = 1,
- MEDIUM_MARK,
- LARGE_MARK,
- };
- static const size_t MAX_SMALL_LIST_SIZE = 16;
- static const size_t MAX_MEDIUM_LIST_INDEX = 10;
- public:
- struct TPageListItem: public TIntrusiveListItem<TPageListItem> {
- template <class T>
- T* As() {
- return static_cast<T*>(TAlignedPagePool::GetPageStart(this));
- }
- template <class T>
- const T* As() const {
- return static_cast<const T*>(TAlignedPagePool::GetPageStart(this));
- }
- };
- struct TListHeader {
- ui16 Mark;
- ui16 ListSize;
- ui16 FreeLists;
- ui16 FreeListOffset;
- TPageListItem ListItem;
- TListHeader(ui16 mark, ui16 listSize, ui16 listCount)
- : Mark(mark)
- , ListSize(listSize)
- , FreeLists(listCount)
- , FreeListOffset(0u)
- {
- Y_ASSERT(ListItem.As<TListHeader>() == this);
- *reinterpret_cast<ui16*>(this + 1) = 0u; // Mark first list for initial usage
- }
- ~TListHeader() {
- Mark = 0u; // Reset mark
- }
- };
- struct TLargeListHeader {
- ui16 Mark;
- ui32 Capacity;
- ui32 Size;
- TPageListItem ListItem;
- TLargeListHeader(ui32 capacity)
- : Mark(LARGE_MARK)
- , Capacity(capacity)
- , Size(0u)
- {
- Y_ASSERT(ListItem.As<TLargeListHeader>() == this);
- }
- ~TLargeListHeader() {
- Mark = 0u; // Reset mark
- }
- template <typename T>
- T* GetList() {
- return reinterpret_cast<T*>(this + 1);
- }
- template <typename T>
- const T* GetList() const {
- return reinterpret_cast<const T*>(this + 1);
- }
- TLargeListHeader* Next() {
- return ListItem.Next()->Node()->As<TLargeListHeader>();
- }
- const TLargeListHeader* Next() const {
- return ListItem.Next()->Node()->As<TLargeListHeader>();
- }
- void Append(TLargeListHeader* header) {
- header->ListItem.LinkAfter(ListItem);
- }
- };
- template <typename T, class THeader>
- class TListIterator {
- using TRawByte = std::conditional_t<std::is_const<T>::value, const ui8*, ui8*>;
- public:
- using TRaw = std::conditional_t<std::is_const<T>::value, const void*, void*>;
- TListIterator() {
- }
- TListIterator(T* list) {
- if (LARGE_MARK == GetMark(list)) {
- CurrentPage = EndPage = GetLargeListHeader(list)->Next();
- Current = CurrentPage->template GetList<T>();
- End = (TRawByte)Current + CurrentPage->Size * sizeof(T);
- } else {
- Current = list;
- End = (TRawByte)Current + GetPartListSize(list) * sizeof(T);
- }
- }
- TListIterator(TRaw start, TRaw end)
- : Current(start)
- , End(end)
- {
- }
- TListIterator& operator++() {
- Y_ASSERT(Ok());
- Current = (TRawByte)Current + sizeof(T);
- if (Current == End) {
- if (CurrentPage) {
- CurrentPage = CurrentPage->Next();
- if (CurrentPage != EndPage) {
- Current = (TRaw)CurrentPage->template GetList<T>();
- End = (TRawByte)Current + CurrentPage->Size * sizeof(T);
- } else {
- CurrentPage = EndPage = nullptr;
- Current = End = nullptr;
- }
- } else {
- Current = End = nullptr;
- }
- }
- return *this;
- }
- bool operator ==(const TListIterator& it) const {
- return Current == it.Current && CurrentPage == it.CurrentPage;
- }
- bool operator !=(const TListIterator& it) const {
- return !operator ==(it);
- }
- T Get() const {
- using TClean = typename std::remove_cv<T>::type;
- return ::ReadUnaligned<TClean>(Current);
- }
- void Set(T val) const {
- ::WriteUnaligned<T>(Current, val);
- }
- TRaw GetRaw() const {
- return Current;
- }
- bool Ok() const {
- return Current != nullptr;
- }
- private:
- TRaw Current = nullptr;
- TRaw End = nullptr;
- THeader* CurrentPage = nullptr;
- THeader* EndPage = nullptr;
- };
- public:
- static inline TListHeader* GetListHeader(void* addr) {
- TListHeader* header = reinterpret_cast<TListHeader*>(TAlignedPagePool::GetPageStart(addr));
- Y_ASSERT(SMALL_MARK == header->Mark || MEDIUM_MARK == header->Mark);
- return header;
- }
- static inline const TListHeader* GetListHeader(const void* addr) {
- const TListHeader* header = reinterpret_cast<const TListHeader*>(TAlignedPagePool::GetPageStart(addr));
- Y_ASSERT(SMALL_MARK == header->Mark || MEDIUM_MARK == header->Mark);
- return header;
- }
- static inline TLargeListHeader* GetLargeListHeader(void* addr) {
- TLargeListHeader* header = reinterpret_cast<TLargeListHeader*>(TAlignedPagePool::GetPageStart(addr));
- Y_ASSERT(LARGE_MARK == header->Mark);
- return header;
- }
- static inline const TLargeListHeader* GetLargeListHeader(const void* addr) {
- const TLargeListHeader* header = reinterpret_cast<const TLargeListHeader*>(TAlignedPagePool::GetPageStart(addr));
- Y_ASSERT(LARGE_MARK == header->Mark);
- return header;
- }
- template <typename T>
- static inline constexpr ui16 GetSmallPageCapacity(size_t listSize) {
- // Always keep at least ui16 per list in order to track collection free lists
- return static_cast<ui16>((TAlignedPagePool::POOL_PAGE_SIZE - sizeof(TListHeader)) / (AlignUp<size_t>(listSize * sizeof(T), sizeof(ui16)))) & 0x7FFF;
- }
- template <typename T>
- static inline constexpr ui16 GetMediumPageCapacity(size_t listSize) {
- return static_cast<ui16>((TAlignedPagePool::POOL_PAGE_SIZE - sizeof(TListHeader)) / (FastClp2(listSize) * sizeof(T) + sizeof(ui16))) & 0x7FFF;
- }
- template <typename T>
- static inline constexpr ui32 GetLargePageCapacity() {
- return static_cast<ui32>((TAlignedPagePool::POOL_PAGE_SIZE - sizeof(TLargeListHeader)) / sizeof(T));
- }
- template <typename T>
- static inline constexpr size_t GetMaxListSize() {
- #define NCHASH_RAW_SIZE ((((TAlignedPagePool::POOL_PAGE_SIZE - sizeof(TListHeader)) / 2) - sizeof(ui16)) / sizeof(T))
- // For medium lists get downward pow2 num
- return NCHASH_RAW_SIZE > 16 ? 1ULL << MostSignificantBitCT(NCHASH_RAW_SIZE) : NCHASH_RAW_SIZE;
- #undef NCHASH_RAW_SIZE
- }
- protected:
- using TListType = TIntrusiveList<TPageListItem>;
- struct TUsedPages {
- TUsedPages() = default;
- TUsedPages(const TUsedPages&) = delete;
- TUsedPages(TUsedPages&& other)
- : SmallPages(std::move(other.SmallPages))
- , MediumPages(std::move(other.MediumPages))
- , FullPages(std::move(other.FullPages))
- {
- }
- TUsedPages& operator =(const TUsedPages& other) = delete;
- TUsedPages& operator =(TUsedPages&& other) {
- TUsedPages(std::move(other)).Swap(*this);
- return *this;
- }
- void Swap(TUsedPages& other) {
- DoSwap(SmallPages, other.SmallPages);
- DoSwap(MediumPages, other.MediumPages);
- DoSwap(FullPages, other.FullPages);
- }
- // Returns count of used pages
- size_t PrintStat(const TStringBuf& header, IOutputStream& out) const;
- TString DebugInfo() const;
- std::array<TListType, MAX_SMALL_LIST_SIZE - 1> SmallPages; // 2-16 sizes
- std::array<TListType, MAX_MEDIUM_LIST_INDEX> MediumPages; // 32,64,128,...,16384. Indexed by pow2
- TListType FullPages;
- };
- public:
- TListPoolBase(TAlignedPagePool& pagePool)
- : PagePool_(pagePool)
- {}
- TListPoolBase(const TListPoolBase&) = delete;
- TListPoolBase(TListPoolBase&& other)
- : PagePool_(other.PagePool_)
- {
- }
- TListPoolBase& operator =(const TListPoolBase& other) = delete;
- TListPoolBase& operator =(TListPoolBase&& other) {
- PagePool_.Swap(other.PagePool_);
- return *this;
- }
- void Swap(TListPoolBase& other) {
- PagePool_.Swap(other.PagePool_);
- }
- TAlignedPagePool& GetPagePool() const {
- return PagePool_;
- }
- // Returns full list size for short and medium lists. Returns size of last page for a large list
- static size_t GetPartListSize(const void* list) {
- switch (GetMark(list)) {
- case SMALL_MARK:
- return GetListHeader(list)->ListSize;
- case MEDIUM_MARK:
- return *(reinterpret_cast<const ui16*>(list) - 1);
- case LARGE_MARK:
- return GetLargeListHeader(list)->Size;
- default:
- Y_ABORT("Bad list address");
- }
- return 0;
- }
- static size_t GetFullListSize(const void* list) {
- switch (GetMark(list)) {
- case SMALL_MARK:
- return GetListHeader(list)->ListSize;
- case MEDIUM_MARK:
- return *(reinterpret_cast<const ui16*>(list) - 1);
- case LARGE_MARK:
- {
- const TLargeListHeader* start = GetLargeListHeader(list);
- const TLargeListHeader* next = start;
- size_t size = 0;
- do {
- size += next->Size;
- next = next->Next();
- } while (next != start);
- return size;
- }
- default:
- Y_ABORT("Bad list address");
- }
- return 0;
- }
- static size_t GetListCapacity(const void* list) {
- switch (GetMark(list)) {
- case SMALL_MARK:
- case MEDIUM_MARK:
- return GetListHeader(list)->ListSize;
- case LARGE_MARK:
- return GetLargeListHeader(list)->Capacity;
- default:
- Y_ABORT("Bad list address");
- }
- return 0;
- }
- static void SetPartListSize(void* list, size_t size) {
- switch (GetMark(list)) {
- case MEDIUM_MARK:
- *(reinterpret_cast<ui16*>(list) - 1) = size;
- break;
- case LARGE_MARK:
- GetLargeListHeader(list)->Size = size;
- break;
- default:
- Y_ABORT("Bad list address");
- }
- }
- static inline ui16 GetMark(const void* addr) {
- return *reinterpret_cast<const ui16*>(TAlignedPagePool::GetPageStart(addr));
- }
- protected:
- void FreeListPage(TListHeader* p);
- private:
- TAlignedPagePool& PagePool_;
- };
- template <typename TPrimary, typename TSecondary = TPrimary>
- class TListPool: public TListPoolBase {
- private:
- static constexpr size_t PoolCount = 1 + !std::is_same<TPrimary, TSecondary>::value;
- public:
- TListPool(TAlignedPagePool& pagePool)
- : TListPoolBase(pagePool)
- {}
- TListPool(const TListPool&) = delete;
- TListPool(TListPool&& other)
- : TListPoolBase(std::move(other))
- , Pools(std::move(other.Pools))
- {
- }
- ~TListPool() {
- for (auto& p: Pools) {
- for (auto& list: p.SmallPages) {
- Y_ABORT_UNLESS(list.Empty(), "%s", DebugInfo().data());
- }
- for (auto& list: p.MediumPages) {
- Y_ABORT_UNLESS(list.Empty(), "%s", DebugInfo().data());
- }
- Y_ABORT_UNLESS(p.FullPages.Empty(), "%s", DebugInfo().data());
- }
- }
- TListPool& operator =(const TListPool& other) = delete;
- TListPool& operator =(TListPool&& other) {
- TListPool(std::move(other)).Swap(*this);
- return *this;
- }
- template <typename T>
- T* GetList(size_t size) {
- static_assert(std::is_same<TPrimary, T>::value || std::is_same<TSecondary, T>::value, "Bad requested list type");
- static constexpr size_t PoolNdx = static_cast<size_t>(!std::is_same<TPrimary, T>::value);
- Y_ABORT_UNLESS(size >= 2);
- T* res = nullptr;
- if (Y_LIKELY(size <= TListPoolBase::GetMaxListSize<T>())) {
- if (Y_LIKELY(size <= MAX_SMALL_LIST_SIZE)) {
- res = GetSmallList<PoolNdx, T>(GetSmallListPage<PoolNdx, T>(size));
- } else {
- res = GetMediumList<PoolNdx, T>(GetMediumListPage<PoolNdx, T>(size), size);
- }
- }
- return res;
- }
- // Returns pointer to the new element
- template <typename T>
- T* IncrementList(T*& list) {
- size_t oldSize = TListPoolBase::GetPartListSize(list);
- if (oldSize + 1 <= TListPoolBase::GetListCapacity(list)) {
- TListPoolBase::SetPartListSize(list, oldSize + 1);
- return list + oldSize;
- }
- TLargeListHeader* header = nullptr;
- T* oldList = list;
- if (Y_LIKELY(LARGE_MARK != GetMark(oldList))) {
- list = GetList<T>(oldSize + 1);
- if (nullptr == list) {
- // Convert to large list
- header = GetLargeListPage<T>();
- list = header->GetList<T>();
- Y_ABORT_UNLESS(header->Capacity >= oldSize);
- header->Size = oldSize;
- }
- if (std::is_trivially_copyable<T>::value) {
- memcpy(list, oldList, sizeof(T) * oldSize);
- } else {
- for (size_t i = 0; i < oldSize; ++i) {
- ::WriteUnaligned<T>(list + i, ::ReadUnaligned<T>(oldList + i));
- }
- }
- ReturnList(oldList);
- if (nullptr == header) {
- return list + oldSize;
- } else if (header->Size < header->Capacity) {
- return header->GetList<T>() + header->Size++;
- }
- } else {
- header = GetLargeListHeader(list);
- }
- // Add new page to large list
- TLargeListHeader* newPage = GetLargeListPage<T>();
- ++newPage->Size;
- list = newPage->GetList<T>();
- header->Append(newPage);
- return list;
- }
- template <typename T>
- T* CloneList(const T* list) {
- if (Y_LIKELY(LARGE_MARK != GetMark(list))) {
- const size_t size = TListPoolBase::GetPartListSize(list);
- T* clonedList = GetList<T>(size);
- for (size_t i = 0; i < size; ++i) {
- new (clonedList + i) T(list[i]);
- }
- return clonedList;
- } else {
- TLargeListHeader* lastListHeader = nullptr;
- const TLargeListHeader* start = GetLargeListHeader(list)->Next();
- const TLargeListHeader* header = start;
- do {
- TLargeListHeader* newHeader = GetLargeListPage<T>();
- newHeader->Size = header->Size;
- T* newList = newHeader->GetList<T>();
- const T* list = header->GetList<T>();
- for (size_t i = 0; i < header->Size; ++i) {
- new (newList + i) T(list[i]);
- }
- if (lastListHeader) {
- lastListHeader->Append(newHeader);
- }
- lastListHeader = newHeader;
- header = header->Next();
- } while (header != start);
- return lastListHeader->GetList<T>();
- }
- }
- template <typename T>
- void ReturnList(T* list) {
- static_assert(std::is_same<TPrimary, T>::value || std::is_same<TSecondary, T>::value, "Bad returned list type");
- static constexpr size_t PoolNdx = static_cast<size_t>(!std::is_same<TPrimary, T>::value);
- switch (GetMark(list)) {
- case SMALL_MARK:
- ReturnSmallList<PoolNdx, T>(GetListHeader(list), list);
- break;
- case MEDIUM_MARK:
- ReturnMediumList<PoolNdx, T>(GetListHeader(list), list);
- break;
- case LARGE_MARK:
- ReturnLargeList<T>(list);
- break;
- default:
- Y_ABORT("Bad list address");
- }
- }
- void Swap(TListPool& other) {
- TListPoolBase::Swap(other);
- DoSwap(Pools, other.Pools);
- }
- void PrintStat(IOutputStream& out) const {
- size_t usedPages = 0;
- if (std::is_same<TPrimary, TSecondary>::value) {
- usedPages = Pools[0].PrintStat(TStringBuf(""), out);
- } else {
- usedPages = Pools[0].PrintStat(TStringBuf("Primary: "), out) + Pools[1].PrintStat(TStringBuf("Secondary: "), out);
- }
- GetPagePool().PrintStat(usedPages, out);
- }
- TString DebugInfo() const {
- if (std::is_same<TPrimary, TSecondary>::value) {
- return Pools[0].DebugInfo();
- } else {
- return TString().append("Primary:\n").append(Pools[0].DebugInfo()).append("Secondary:\n").append(Pools[1].DebugInfo());
- }
- }
- private:
- template <typename T>
- static void DestroyRange(T* s, T* e) {
- if (!std::is_trivially_destructible<T>::value) {
- while (s != e) {
- --e;
- e->~T();
- }
- }
- }
- template <size_t PoolNdx, typename T>
- TListHeader* GetSmallListPage(size_t size) {
- Y_ASSERT(size > 1 && size <= MAX_SMALL_LIST_SIZE);
- TListType& pages = Pools[PoolNdx].SmallPages[size - 2];
- if (!pages.Empty()) {
- return pages.Front()->As<TListHeader>();
- }
- ui16 listCount = GetSmallPageCapacity<T>(size);
- Y_ASSERT(listCount >= 2);
- TListHeader* header = new (GetPagePool().GetPage()) TListHeader(SMALL_MARK, size, listCount);
- pages.PushFront(&header->ListItem);
- return header;
- }
- template <size_t PoolNdx, typename T>
- TListHeader* GetMediumListPage(size_t size) {
- Y_ASSERT(size > MAX_SMALL_LIST_SIZE && size <= TListPoolBase::GetMaxListSize<T>());
- size_t index = MostSignificantBit((size - 1) >> MostSignificantBitCT(MAX_SMALL_LIST_SIZE));
- Y_ASSERT(index < Pools[PoolNdx].MediumPages.size());
- TListType& pages = Pools[PoolNdx].MediumPages[index];
- if (!pages.Empty()) {
- return pages.Front()->As<TListHeader>();
- }
- ui16 listCapacity = FastClp2(size);
- ui16 listCount = GetMediumPageCapacity<T>(listCapacity);
- Y_ASSERT(listCount >= 2);
- TListHeader* header = new (GetPagePool().GetPage()) TListHeader(MEDIUM_MARK, listCapacity, listCount);
- pages.PushFront(&header->ListItem);
- return header;
- }
- template <typename T>
- TLargeListHeader* GetLargeListPage() {
- TLargeListHeader* const header = new (GetPagePool().GetPage()) TLargeListHeader(GetLargePageCapacity<T>());
- return header;
- }
- template <size_t PoolNdx, typename T>
- T* GetSmallList(TListHeader* listHeader) {
- if (0 == listHeader->FreeLists) {
- return nullptr;
- }
- const bool last = (0 == --listHeader->FreeLists);
- // Always keep at least ui16 per list in order to track collection of free lists
- const size_t byteListSize = AlignUp<size_t>(sizeof(T) * listHeader->ListSize, sizeof(ui16));
- ui16* l = reinterpret_cast<ui16*>(reinterpret_cast<ui8*>(listHeader + 1) + byteListSize * listHeader->FreeListOffset);
- // Distinguish first (0) and repeatedly (0x8000u) used lists
- if ((*l) & 0x8000u) {
- listHeader->FreeListOffset = ((*l) & 0x7FFF);
- } else {
- ++listHeader->FreeListOffset;
- if (!last) {
- // Mark next free list as first used
- *reinterpret_cast<ui16*>(reinterpret_cast<ui8*>(listHeader + 1) + byteListSize * listHeader->FreeListOffset) = 0u;
- }
- }
- if (last) {
- listHeader->ListItem.Unlink();
- Pools[PoolNdx].FullPages.PushBack(&listHeader->ListItem);
- }
- return reinterpret_cast<T*>(l);
- }
- template <size_t PoolNdx, typename T>
- T* GetMediumList(TListHeader* listHeader, size_t size) {
- if (0 == listHeader->FreeLists) {
- return nullptr;
- }
- const bool last = (0 == --listHeader->FreeLists);
- const size_t byteListSize = sizeof(T) * listHeader->ListSize + sizeof(ui16);
- ui16* l = reinterpret_cast<ui16*>(reinterpret_cast<ui8*>(listHeader + 1) + byteListSize * listHeader->FreeListOffset);
- // Distinguish first (0) and repeatedly (0x8000u) used lists
- if ((*l) & 0x8000u) {
- listHeader->FreeListOffset = ((*l) & 0x7FFF);
- } else {
- ++listHeader->FreeListOffset;
- if (!last) {
- // Mark next free list as first used
- *reinterpret_cast<ui16*>(reinterpret_cast<ui8*>(listHeader + 1) + byteListSize * listHeader->FreeListOffset) = 0u;
- }
- }
- if (last) {
- listHeader->ListItem.Unlink();
- Pools[PoolNdx].FullPages.PushBack(&listHeader->ListItem);
- }
- // For medium pages store the list size ahead
- *l = size;
- ++l;
- return reinterpret_cast<T*>(l);
- }
- template <size_t PoolNdx, typename T>
- void ReturnSmallList(TListHeader* listHeader, T* list) {
- DestroyRange(list, list + listHeader->ListSize);
- const size_t byteListSize = AlignUp<size_t>(sizeof(T) * listHeader->ListSize, sizeof(ui16));
- Y_ASSERT((reinterpret_cast<ui8*>(list) - reinterpret_cast<ui8*>(listHeader + 1)) % byteListSize == 0);
- const ui64 offset = (reinterpret_cast<ui8*>(list) - reinterpret_cast<ui8*>(listHeader + 1)) / byteListSize;
- Y_ASSERT(offset < TAlignedPagePool::POOL_PAGE_SIZE);
- *reinterpret_cast<ui16*>(list) = listHeader->FreeListOffset | 0x8000u;
- listHeader->FreeListOffset = offset;
- ++listHeader->FreeLists;
- if (1 == listHeader->FreeLists) {
- listHeader->ListItem.Unlink(); // Remove from full list
- Pools[PoolNdx].SmallPages[listHeader->ListSize - 2].PushFront(&listHeader->ListItem); // Add to partially used
- } else if (GetSmallPageCapacity<T>(listHeader->ListSize) == listHeader->FreeLists) {
- listHeader->ListItem.Unlink(); // Remove from partially used
- FreeListPage(listHeader);
- }
- }
- template <size_t PoolNdx, typename T>
- void ReturnMediumList(TListHeader* listHeader, T* list) {
- ui16* l = reinterpret_cast<ui16*>(list) - 1;
- DestroyRange(list, list + *l);
- Y_ASSERT((reinterpret_cast<ui8*>(l) - reinterpret_cast<ui8*>(listHeader + 1)) % (listHeader->ListSize * sizeof(T) + sizeof(ui16)) == 0);
- ui64 offset = (reinterpret_cast<ui8*>(l) - reinterpret_cast<ui8*>(listHeader + 1)) / (listHeader->ListSize * sizeof(T) + sizeof(ui16));
- Y_ASSERT(offset < TAlignedPagePool::POOL_PAGE_SIZE);
- *l = listHeader->FreeListOffset | 0x8000u;
- listHeader->FreeListOffset = offset;
- ++listHeader->FreeLists;
- if (1 == listHeader->FreeLists) {
- listHeader->ListItem.Unlink(); // Remove from full list
- const size_t index = MostSignificantBit((listHeader->ListSize - 1) >> MostSignificantBitCT(MAX_SMALL_LIST_SIZE));
- Y_ASSERT(index < Pools[PoolNdx].MediumPages.size());
- Pools[PoolNdx].MediumPages[index].PushFront(&listHeader->ListItem); // Add to partially used
- } else if (GetMediumPageCapacity<T>(listHeader->ListSize) == listHeader->FreeLists) {
- listHeader->ListItem.Unlink(); // Remove from partially used
- FreeListPage(listHeader);
- }
- }
- template <typename T>
- void ReturnLargeList(T* list) {
- TLargeListHeader* header = GetLargeListHeader(list);
- while (!header->ListItem.Empty()) {
- TLargeListHeader* next = header->Next();
- DestroyRange(next->GetList<T>(), next->GetList<T>() + next->Size);
- next->ListItem.Unlink();
- next->~TLargeListHeader();
- GetPagePool().ReturnPage(next);
- }
- DestroyRange(header->GetList<T>(), header->GetList<T>() + header->Size);
- header->~TLargeListHeader();
- GetPagePool().ReturnPage(header);
- }
- protected:
- std::array<TUsedPages, PoolCount> Pools;
- };
- #pragma pack(push, 1)
- template <typename T>
- struct TNode {
- enum {
- FlagEmpty = 0,
- FlagSingle = 1,
- FlagList = 2,
- };
- ui8 Flag;
- union {
- ui8 D1;
- ui8 D2[Max<size_t>(sizeof(T), sizeof(T*))];
- } Storage;
- TNode()
- : Flag(FlagEmpty)
- {
- }
- TNode(const TNode& n)
- : Flag(n.Flag)
- {
- MemCopy(Storage.D2, n.Storage.D2, sizeof(Storage.D2));
- }
- TNode(TNode&& n)
- : Flag(n.Flag)
- {
- MemCopy(Storage.D2, n.Storage.D2, sizeof(Storage.D2));
- n.Flag = FlagEmpty;
- }
- TNode& operator=(const TNode& n) {
- Flag = n.Flag;
- MemCopy(Storage.D2, n.Storage.D2, sizeof(Storage.D2));
- return *this;
- }
- TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader> Iter() {
- if (FlagSingle == Flag) {
- return TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader>(reinterpret_cast<T*>(&Storage), reinterpret_cast<T*>(&Storage) + 1);
- } else if (FlagList == Flag) {
- return TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader>(*reinterpret_cast<T**>(&Storage));
- }
- return TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader>();
- }
- TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader> Iter() const {
- if (FlagSingle == Flag) {
- return TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader>(reinterpret_cast<const T*>(&Storage), reinterpret_cast<const T*>(&Storage) + 1);
- } else if (FlagList == Flag) {
- return TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader>(*reinterpret_cast<const T* const*>(&Storage));
- }
- return TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader>();
- }
- void Set(T val) {
- Flag = FlagSingle;
- ::WriteUnaligned<T>(&Storage, val);
- }
- void SetList(T* list) {
- Flag = FlagList;
- ::WriteUnaligned<T*>(&Storage, list);
- }
- T Get() const {
- Y_ABORT_UNLESS(FlagSingle == Flag);
- return ::ReadUnaligned<T>(&Storage);
- }
- T* GetList() {
- Y_ABORT_UNLESS(FlagList == Flag);
- return *reinterpret_cast<T**>(&Storage);
- }
- const T* GetList() const {
- Y_ABORT_UNLESS(FlagList == Flag);
- return *reinterpret_cast<const T* const*>(&Storage);
- }
- size_t Size() const {
- if (FlagEmpty == Flag) {
- return 0;
- } else if (FlagSingle == Flag) {
- return 1;
- } else {
- return TListPoolBase::GetFullListSize(GetList());
- }
- }
- void Clear() {
- if (FlagSingle == Flag) {
- reinterpret_cast<T*>(&Storage)->~T();
- }
- Flag = FlagEmpty;
- }
- };
- template <typename TKey, typename TValue>
- struct TKeyValuePair {
- using first_type = TKey;
- using second_type = TValue;
- TKeyValuePair() = default;
- TKeyValuePair(const TKeyValuePair&) = default;
- TKeyValuePair(TKeyValuePair&&) = default;
- TKeyValuePair(const std::pair<TKey, TValue>& p)
- : first(p.first)
- , second(p.second)
- {
- }
- TKeyValuePair(const TKey& k, const TValue& v)
- : first(k)
- , second(v)
- {
- }
- TKeyValuePair(TKey&& k, const TValue& v)
- : first(std::move(k))
- , second(v)
- {
- }
- TKeyValuePair(const TKey& k, TValue&& v)
- : first(k)
- , second(std::move(v))
- {
- }
- TKeyValuePair(TKey&& k, TValue&& v)
- : first(std::move(k))
- , second(std::move(v))
- {
- }
- TKeyValuePair& operator =(const TKeyValuePair&) = default;
- TKeyValuePair& operator =(TKeyValuePair&&) = default;
- TKey first;
- TValue second;
- };
- template <typename TKey, typename TValue>
- using TKeyNodePair = TKeyValuePair<TKey, TNode<TValue>>;
- #pragma pack(pop)
- template <typename TItemType,
- typename TKeyType,
- typename TKeyExtractor,
- typename TKeyHash,
- typename TKeyEqual,
- typename TSubItemType = TItemType
- >
- class TCompactHashBase {
- protected:
- using TItemNode = TNode<TItemType>;
- static_assert(sizeof(TItemNode) == 1 + Max<size_t>(sizeof(TItemType), sizeof(void*)), "Unexpected size");
- public:
- template <typename T>
- class TIteratorImpl {
- friend class TCompactHashBase;
- using TBucketIter = TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader>;
- // Full scan iterator
- TIteratorImpl(const TCompactHashBase* hash)
- : Hash(hash)
- , Bucket(0)
- , EndBucket(Hash->BucketsCount_)
- , Pos()
- {
- for (; Bucket < EndBucket; ++Bucket) {
- if (!Hash->IsEmptyBucket(Bucket)) {
- Pos = Hash->GetBucketIter(Bucket);
- break;
- }
- }
- }
- // Key iterator
- TIteratorImpl(const TCompactHashBase* hash, size_t bucket, const TBucketIter& pos)
- : Hash(hash)
- , Bucket(bucket)
- , EndBucket(bucket + 1)
- , Pos(pos)
- {
- }
- // Empty iterator
- TIteratorImpl() {
- }
- public:
- TIteratorImpl& operator=(const TIteratorImpl& rhs) {
- Hash = rhs.Hash;
- Bucket = rhs.Bucket;
- EndBucket = rhs.EndBucket;
- Pos = rhs.Pos;
- return *this;
- }
- bool Ok() const {
- return Bucket < EndBucket && Pos.Ok();
- }
- TIteratorImpl& operator++() {
- if (Bucket < EndBucket) {
- if ((++Pos).Ok()) {
- return *this;
- }
- for (++Bucket; Bucket < EndBucket; ++Bucket) {
- if (!Hash->IsEmptyBucket(Bucket)) {
- Pos = Hash->GetBucketIter(Bucket);
- break;
- }
- }
- }
- return *this;
- }
- T operator*() const {
- Y_ASSERT(Ok());
- return Pos.Get();
- }
- T Get() const {
- Y_ASSERT(Ok());
- return Pos.Get();
- }
- TIteratorImpl MakeCurrentKeyIter() const {
- Y_ASSERT(Ok());
- return TIteratorImpl(Hash, Bucket, TBucketIter(Pos.GetRaw(), Pos.GetRaw() + sizeof(T)));
- }
- private:
- const TCompactHashBase* Hash = nullptr;
- size_t Bucket = 0;
- size_t EndBucket = 0;
- TBucketIter Pos;
- };
- template <typename T>
- class TIteratorImpl<TKeyNodePair<TKeyType, T>> {
- friend class TCompactHashBase;
- using TBucketIter = TListPoolBase::TListIterator<const TKeyNodePair<TKeyType, T>, const TListPoolBase::TLargeListHeader>;
- using TValueIter = TListPoolBase::TListIterator<const T, const TListPoolBase::TLargeListHeader>;
- // Full scan iterator
- TIteratorImpl(const TCompactHashBase* hash)
- : Hash(hash)
- , Bucket(0)
- , EndBucket(Hash->BucketsCount_)
- {
- for (; Bucket < EndBucket; ++Bucket) {
- if (!Hash->IsEmptyBucket(Bucket)) {
- Pos = Hash->GetBucketIter(Bucket);
- SubPos = static_cast<const TKeyNodePair<TKeyType, T>*>(Pos.GetRaw())->second.Iter();
- break;
- }
- }
- }
- // Key iterator
- TIteratorImpl(const TCompactHashBase* hash, size_t bucket, const TBucketIter& pos)
- : Hash(hash)
- , Bucket(bucket)
- , EndBucket(bucket + 1)
- , Pos(pos)
- , SubPos(static_cast<const TKeyNodePair<TKeyType, T>*>(Pos.GetRaw())->second.Iter())
- {
- }
- // Empty iterator
- TIteratorImpl() {
- }
- public:
- bool Ok() const {
- return Bucket < EndBucket;
- }
- TIteratorImpl& operator++() {
- return Shift(false);
- }
- TIteratorImpl& NextKey() {
- return Shift(true);
- }
- TKeyType GetKey() const {
- Y_ASSERT(Ok());
- return Pos.Get().first;
- }
- T GetValue() const {
- Y_ASSERT(Ok());
- return SubPos.Get();
- }
- T operator*() const {
- return GetValue();
- }
- TIteratorImpl MakeCurrentKeyIter() const {
- Y_ASSERT(Ok());
- return TIteratorImpl(Hash, Bucket, TBucketIter(Pos.GetRaw(), static_cast<const ui8*>(Pos.GetRaw()) + sizeof(TKeyNodePair<TKeyType, T>)));
- }
- private:
- TIteratorImpl& Shift(bool nextKey) {
- Y_ASSERT(Bucket < EndBucket);
- Y_ASSERT(SubPos.Ok());
- if (!nextKey && (++SubPos).Ok()) {
- return *this;
- }
- Y_ASSERT(Pos.Ok());
- if ((++Pos).Ok()) {
- SubPos = static_cast<const TKeyNodePair<TKeyType, T>*>(Pos.GetRaw())->second.Iter();
- return *this;
- } else {
- SubPos = TValueIter();
- }
- for (++Bucket; Bucket < EndBucket; ++Bucket) {
- if (!Hash->IsEmptyBucket(Bucket)) {
- Pos = Hash->GetBucketIter(Bucket);
- SubPos = static_cast<const TKeyNodePair<TKeyType, T>*>(Pos.GetRaw())->second.Iter();
- break;
- }
- }
- return *this;
- }
- private:
- const TCompactHashBase* Hash = nullptr;
- size_t Bucket = 0;
- size_t EndBucket = 0;
- TBucketIter Pos;
- TValueIter SubPos;
- };
- public:
- using TIterator = TIteratorImpl<TItemType>;
- using TBucketIterator = TListPoolBase::TListIterator<TItemType, TListPoolBase::TLargeListHeader>;
- using TConstBucketIterator = TListPoolBase::TListIterator<const TItemType, const TListPoolBase::TLargeListHeader>;
- TCompactHashBase(TAlignedPagePool& pagePool, size_t size = 0, const TKeyExtractor& keyExtractor = TKeyExtractor(),
- const TKeyHash& keyHash = TKeyHash(), const TKeyEqual& keyEqual = TKeyEqual())
- : ListPool_(pagePool)
- , KeyExtractor_(keyExtractor)
- , KeyHash_(keyHash)
- , KeyEqual_(keyEqual)
- {
- AllocateBuckets(FindNearestPrime(size));
- }
- TCompactHashBase(const TCompactHashBase& other)
- : Size_(other.Size_)
- , UniqSize_(other.UniqSize_)
- , MaxLoadFactor_(other.MaxLoadFactor_)
- , ListPool_(other.ListPool_.GetPagePool())
- , KeyExtractor_(other.KeyExtractor_)
- , KeyHash_(other.KeyHash_)
- , KeyEqual_(other.KeyEqual_)
- {
- AllocateBuckets(other.BucketsCount_);
- for (size_t i = 0; i < other.BucketsCount_; ++i) {
- auto& b = other.Buckets_[i];
- if (TItemNode::FlagSingle == b.Flag) {
- TItemType item = b.Get();
- UnconditionalInsert(KeyExtractor_(item)).Set(CloneItem(item));
- } else if (TItemNode::FlagList == b.Flag) {
- for (auto it = b.Iter(); it.Ok(); ++it) {
- UnconditionalInsert(KeyExtractor_(it.Get())).Set(CloneItem(it.Get()));
- }
- }
- }
- }
- TCompactHashBase(TCompactHashBase&& other)
- : Size_(std::move(other.Size_))
- , UniqSize_(std::move(other.UniqSize_))
- , MaxLoadFactor_(std::move(other.MaxLoadFactor_))
- , Buckets_(std::move(other.Buckets_))
- , BucketsCount_(std::move(other.BucketsCount_))
- , BucketsMemory_(std::move(other.BucketsMemory_))
- , ListPool_(std::move(other.ListPool_))
- , KeyExtractor_(std::move(other.KeyExtractor_))
- , KeyHash_(std::move(other.KeyHash_))
- , KeyEqual_(std::move(other.KeyEqual_))
- {
- other.Buckets_ = nullptr;
- other.BucketsMemory_ = 0;
- other.BucketsCount_ = 0;
- other.Size_ = 0;
- other.UniqSize_ = 0;
- other.MaxLoadFactor_ = 1.f;
- }
- ~TCompactHashBase() {
- ClearImpl(true);
- }
- TCompactHashBase& operator= (const TCompactHashBase& other) {
- TCompactHashBase(other).Swap(*this);
- return *this;
- }
- TCompactHashBase& operator= (TCompactHashBase&& other) {
- TCompactHashBase(std::move(other)).Swap(*this);
- return *this;
- }
- void Clear() {
- ClearImpl(false);
- }
- bool Has(const TKeyType& key) const {
- auto& b = Buckets_[GetBucket(key)];
- auto res = FindPos(b, key);
- return res.Ok();
- }
- bool Empty() const {
- return Size_ == 0;
- }
- size_t Size() const {
- return Size_;
- }
- size_t UniqSize() const {
- return UniqSize_;
- }
- const TKeyExtractor& GetKeyExtractor() const {
- return KeyExtractor_;
- }
- const TKeyHash& GetKeyHash() const {
- return KeyHash_;
- }
- const TKeyEqual& GetKeyEqual() const {
- return KeyEqual_;
- }
- float GetMaxLoadFactor() const {
- return MaxLoadFactor_;
- }
- void SetMaxLoadFactor(float factor) {
- Y_ABORT_UNLESS(factor > 0);
- MaxLoadFactor_ = factor;
- }
- float GetLoadFactor() const {
- return float(UniqSize_) / float(BucketsCount_);
- }
- TAlignedPagePool& GetPagePool() const {
- return ListPool_.GetPagePool();
- }
- void Swap(TCompactHashBase& other) {
- DoSwap(Size_, other.Size_);
- DoSwap(UniqSize_, other.UniqSize_);
- DoSwap(MaxLoadFactor_, other.MaxLoadFactor_);
- DoSwap(Buckets_, other.Buckets_);
- DoSwap(BucketsCount_, other.BucketsCount_);
- DoSwap(BucketsMemory_, other.BucketsMemory_);
- DoSwap(ListPool_, other.ListPool_);
- DoSwap(KeyExtractor_, other.KeyExtractor_);
- DoSwap(KeyHash_, other.KeyHash_);
- DoSwap(KeyEqual_, other.KeyEqual_);
- }
- // Full scan
- TIterator Iterate() const {
- return TIterator(this);
- }
- // Key scan
- TIterator Find(const TKeyType& key) const {
- size_t bucket = GetBucket(key);
- auto& b = Buckets_[bucket];
- auto pos = FindPos(b, key);
- if (pos.Ok()) {
- return TIterator(this, bucket, pos);
- }
- return TIterator();
- }
- size_t Count(const TKeyType& key) const {
- size_t bucket = GetBucket(key);
- auto& b = Buckets_[bucket];
- auto pos = FindPos(b, key);
- if (pos.Ok()) {
- return ValueCount(pos.Get());
- }
- return 0;
- }
- size_t GetBucketCount() const {
- return BucketsCount_;
- }
- size_t GetBucket(const TKeyType& key) const {
- return NYql::VaryingHash(KeyHash_(key)) % BucketsCount_;
- }
- bool IsEmptyBucket(size_t bucket) const {
- Y_ASSERT(bucket < BucketsCount_);
- return TItemNode::FlagEmpty == Buckets_[bucket].Flag;
- }
- size_t GetBucketSize(size_t bucket) const {
- Y_ASSERT(bucket < BucketsCount_);
- return Buckets_[bucket].Size();
- }
- TConstBucketIterator GetBucketIter(size_t bucket) const {
- Y_ASSERT(bucket < BucketsCount_);
- const TItemNode& b = Buckets_[bucket];
- return b.Iter();
- }
- bool Rehash(size_t size) {
- if (double(size) / BucketsCount_ <= MaxLoadFactor_) {
- return false;
- }
- TItemNode* oldBuckets = Buckets_;
- size_t oldBucketsCount = BucketsCount_;
- size_t oldBucketsMemory = BucketsMemory_;
- AllocateBuckets(FindNearestPrime(ceil(double(size) / MaxLoadFactor_)));
- Y_DEFER {
- for (size_t i = 0; i < oldBucketsCount; ++i) {
- auto& b = oldBuckets[i];
- if (TItemNode::FlagList == b.Flag) {
- ListPool_.ReturnList(b.GetList());
- }
- b.Clear();
- }
- FreeBuckets(oldBuckets, oldBucketsMemory);
- };
- try {
- for (size_t i = 0; i < oldBucketsCount; ++i) {
- auto& b = oldBuckets[i];
- if (TItemNode::FlagSingle == b.Flag) {
- TItemType item = b.Get();
- UnconditionalInsert(KeyExtractor_(item)).Set(item);
- } else if (TItemNode::FlagList == b.Flag) {
- for (TBucketIterator it = b.Iter(); it.Ok(); ++it) {
- UnconditionalInsert(KeyExtractor_(it.Get())).Set(it.Get());
- }
- }
- }
- } catch (...) {
- DoSwap(oldBuckets, Buckets_);
- DoSwap(oldBucketsCount, BucketsCount_);
- DoSwap(oldBucketsMemory, BucketsMemory_);
- throw;
- }
- return true;
- }
- void PrintStat(IOutputStream& out) const {
- size_t empty = 0;
- size_t single = 0;
- size_t list = 0;
- for (size_t i = 0; i < BucketsCount_; ++i) {
- auto& b = Buckets_[i];
- if (TItemNode::FlagEmpty == b.Flag) {
- ++empty;
- } else if (TItemNode::FlagSingle == b.Flag) {
- ++single;
- } else {
- ++list;
- }
- }
- out << "Empty buckets: " << empty << Endl;
- out << "Single buckets: " << single << Endl;
- out << "List buckets: " << list << Endl;
- ListPool_.PrintStat(out);
- }
- protected:
- void ClearImpl(bool fromDtor) {
- for (size_t i = 0; i < BucketsCount_; ++i) {
- ClearNode(Buckets_[i]);
- }
- FreeBuckets(Buckets_, BucketsMemory_);
- Buckets_ = nullptr;
- BucketsCount_ = 0;
- BucketsMemory_ = 0;
- if (!fromDtor) {
- AllocateBuckets(FindNearestPrime(0));
- }
- Size_ = 0;
- UniqSize_ = 0;
- }
- void AllocateBuckets(size_t count) {
- auto bucketsMemory = Max(sizeof(TItemNode) * count, (size_t)TAlignedPagePool::POOL_PAGE_SIZE);
- Buckets_ = (TItemNode*)GetPagePool().GetBlock(bucketsMemory);
- BucketsCount_ = count;
- BucketsMemory_ = bucketsMemory;
- for (size_t i = 0; i < count; ++i) {
- new (&Buckets_[i]) TItemNode();
- }
- }
- void FreeBuckets(TItemNode* buckets, size_t memory) {
- if (!buckets) {
- return;
- }
- GetPagePool().ReturnBlock(buckets, memory);
- }
- TConstBucketIterator FindPos(const TNode<TItemType>& b, const TKeyType& key) const {
- if (TItemNode::FlagEmpty != b.Flag) {
- for (TConstBucketIterator it = b.Iter(); it.Ok(); ++it) {
- if (KeyEqual_(key, KeyExtractor_(it.Get()))) {
- return TConstBucketIterator(it.GetRaw(), static_cast<const TItemType*>(it.GetRaw()) + 1); // Limit iterator by current key only
- }
- }
- }
- return TConstBucketIterator();
- }
- TBucketIterator FindPos(TNode<TItemType>& b, const TKeyType& key) {
- if (TItemNode::FlagEmpty != b.Flag) {
- for (TBucketIterator it = b.Iter(); it.Ok(); ++it) {
- if (KeyEqual_(key, KeyExtractor_(it.Get()))) {
- return TBucketIterator(it.GetRaw(), static_cast<TItemType*>(it.GetRaw()) + 1); // Limit iterator by current key only
- }
- }
- }
- return TBucketIterator();
- }
- template <typename T>
- static size_t ValueCount(const TKeyNodePair<TKeyType, T>& item) {
- return item.second.Size();
- }
- template <typename T>
- static size_t ValueCount(const T& /*item*/) {
- return 1;
- }
- template <typename T>
- T CloneItem(const T& src) {
- return src;
- }
- template <typename T>
- TKeyNodePair<TKeyType, T> CloneItem(const TKeyNodePair<TKeyType, T>& src) {
- if (TNode<T>::FlagList == src.second.Flag) {
- TKeyNodePair<TKeyType, T> res(src.first, TNode<T>());
- res.second.SetList(ListPool_.template CloneList<T>(src.second.GetList()));
- return res;
- } else {
- return TKeyNodePair<TKeyType, T>(src.first, src.second);
- }
- }
- template <typename T>
- struct THasNodeValue : public std::false_type {};
- template <typename T>
- struct THasNodeValue<TKeyNodePair<TKeyType, T>> : public std::true_type {};
- template <typename T>
- void ClearNode(TNode<T>& b) {
- if (THasNodeValue<T>::value) {
- for (auto it = b.Iter(); it.Ok(); ++it) {
- ClearItem(*reinterpret_cast<T*>(it.GetRaw()));
- }
- }
- if (TNode<T>::FlagList == b.Flag) {
- ListPool_.ReturnList(b.GetList());
- }
- b.Clear();
- }
- template <typename T>
- void ClearItem(T& /*item*/) {
- }
- template <typename T>
- void ClearItem(TKeyNodePair<TKeyType, T>& item) {
- ClearNode(item.second);
- }
- std::pair<TBucketIterator, bool> InsertOrReplace(TKeyType key) {
- auto b = &Buckets_[GetBucket(key)];
- auto res = FindPos(*b, key);
- if (res.Ok()) {
- return {res, false};
- }
- if (Rehash(UniqSize_ + 1)) {
- // Update bucket after rehashing
- b = &Buckets_[GetBucket(key)];
- }
- TBucketIterator iter = ExpandBucket<TItemType>(*b);
- ++UniqSize_;
- ++Size_;
- return {iter, true};
- }
- TBucketIterator UnconditionalInsert(TKeyType key) {
- return ExpandBucket<TItemType>(Buckets_[GetBucket(key)]);
- }
- template <typename T>
- TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader> ExpandBucket(TNode<T>& b) {
- if (TNode<T>::FlagEmpty == b.Flag) {
- b.Flag = TNode<T>::FlagSingle;
- return b.Iter();
- } else if (TNode<T>::FlagSingle == b.Flag) {
- T* list = ListPool_.template GetList<T>(2);
- *list = b.Get();
- b.SetList(list);
- return TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader>(reinterpret_cast<ui8*>(list + 1), reinterpret_cast<ui8*>(list + 2));
- } else {
- T* list = b.GetList();
- T* pos = ListPool_.template IncrementList<T>(list);
- b.SetList(list);
- return TListPoolBase::TListIterator<T, TListPoolBase::TLargeListHeader>(reinterpret_cast<ui8*>(pos), reinterpret_cast<ui8*>(pos + 1));
- }
- }
- protected:
- size_t Size_ = 0;
- size_t UniqSize_ = 0;
- float MaxLoadFactor_ = 1.f;
- TItemNode* Buckets_ = nullptr;
- size_t BucketsCount_ = 0;
- size_t BucketsMemory_ = 0;
- TListPool<TItemType, TSubItemType> ListPool_;
- TKeyExtractor KeyExtractor_;
- TKeyHash KeyHash_;
- TKeyEqual KeyEqual_;
- };
- template <typename TKey,
- typename TValue,
- typename TKeyHash = THash<TKey>,
- typename TKeyEqual = TEqualTo<TKey>>
- class TCompactHash: public TCompactHashBase<TKeyValuePair<TKey, TValue>, TKey, TSelect1st, TKeyHash, TKeyEqual> {
- private:
- static_assert(std::is_trivially_destructible<TKey>::value
- && std::is_trivially_copy_assignable<TKey>::value
- && std::is_trivially_move_assignable<TKey>::value
- && std::is_trivially_copy_constructible<TKey>::value
- && std::is_trivially_move_constructible<TKey>::value
- , "Expected POD key type");
- static_assert(std::is_trivially_destructible<TValue>::value
- && std::is_trivially_copy_assignable<TValue>::value
- && std::is_trivially_move_assignable<TValue>::value
- && std::is_trivially_copy_constructible<TValue>::value
- && std::is_trivially_move_constructible<TValue>::value
- , "Expected POD value type");
- using TItem = TKeyValuePair<TKey, TValue>;
- using TBase = TCompactHashBase<TItem, TKey, TSelect1st, TKeyHash, TKeyEqual>;
- public:
- TCompactHash(TAlignedPagePool& pagePool, size_t size = 0, const TKeyHash& keyHash = TKeyHash(), const TKeyEqual& keyEqual = TKeyEqual())
- : TBase(pagePool, size, TSelect1st(), keyHash, keyEqual)
- {
- }
- TCompactHash(const TCompactHash& other)
- : TBase(other)
- {
- }
- TCompactHash(TCompactHash&& other)
- : TBase(std::move(other))
- {
- }
- TCompactHash& operator= (const TCompactHash& rhs) {
- TBase::operator =(rhs);
- return *this;
- }
- TCompactHash& operator= (TCompactHash&& rhs) {
- TBase::operator =(std::move(rhs));
- return *this;
- }
- bool Insert(TItem item) {
- auto res = TBase::InsertOrReplace(TBase::KeyExtractor_(item));
- res.first.Set(item);
- return res.second;
- }
- bool Insert(TKey key, TValue value) {
- auto res = TBase::InsertOrReplace(key);
- res.first.Set(TItem(key, value));
- return res.second;
- }
- bool InsertNew(TItem item) {
- auto res = TBase::InsertOrReplace(TBase::KeyExtractor_(item));
- if (res.second) {
- res.first.Set(item);
- }
- return res.second;
- }
- bool InsertNew(TKey key, TValue value) {
- auto res = TBase::InsertOrReplace(key);
- if (res.second) {
- res.first.Set(TItem(key, value));
- }
- return res.second;
- }
- };
- template <typename TKey,
- typename TValue,
- typename TKeyHash = THash<TKey>,
- typename TKeyEqual = TEqualTo<TKey>>
- class TCompactMultiHash: public TCompactHashBase<TKeyNodePair<TKey, TValue>, TKey, TSelect1st, TKeyHash, TKeyEqual, TValue> {
- private:
- static_assert(std::is_trivially_destructible<TKey>::value
- && std::is_trivially_copy_assignable<TKey>::value
- && std::is_trivially_move_assignable<TKey>::value
- && std::is_trivially_copy_constructible<TKey>::value
- && std::is_trivially_move_constructible<TKey>::value
- , "Expected POD key type");
- static_assert(std::is_trivially_destructible<TValue>::value
- && std::is_trivially_copy_assignable<TValue>::value
- && std::is_trivially_move_assignable<TValue>::value
- && std::is_trivially_copy_constructible<TValue>::value
- && std::is_trivially_move_constructible<TValue>::value
- , "Expected POD value type");
- using TUserItem = std::pair<TKey, TValue>;
- using TStoreItem = TKeyNodePair<TKey, TValue>;
- using TBase = TCompactHashBase<TStoreItem, TKey, TSelect1st, TKeyHash, TKeyEqual, TValue>;
- static_assert(sizeof(TStoreItem) == sizeof(TKey) + sizeof(TNode<TValue>), "Unexpected size");
- public:
- TCompactMultiHash(TAlignedPagePool& pagePool, size_t size = 0, const TKeyHash& keyHash = TKeyHash(), const TKeyEqual& keyEqual = TKeyEqual())
- : TBase(pagePool, size, TSelect1st(), keyHash, keyEqual)
- {
- }
- TCompactMultiHash(const TCompactMultiHash& other)
- : TBase(other)
- {
- }
- TCompactMultiHash(TCompactMultiHash&& other)
- : TBase(std::move(other))
- {
- }
- TCompactMultiHash& operator= (const TCompactMultiHash& rhs) {
- TBase::operator =(rhs);
- return *this;
- }
- TCompactMultiHash& operator= (TCompactMultiHash&& rhs) {
- TBase::operator =(std::move(rhs));
- return *this;
- }
- void Insert(const TUserItem& item) {
- GetOrInsert(item.first).Set(item.second);
- }
- void Insert(TKey key, TValue value) {
- GetOrInsert(key).Set(value);
- }
- protected:
- template <typename TKeyType>
- TListPoolBase::TListIterator<TValue, TListPoolBase::TLargeListHeader> GetOrInsert(TKeyType key) {
- auto res = TBase::InsertOrReplace(key);
- if (res.second) {
- res.first.Set(TStoreItem(key, TNode<TValue>()));
- }
- auto valueIter = TBase::template ExpandBucket<TValue>(reinterpret_cast<TStoreItem*>(res.first.GetRaw())->second);
- if (!res.second) {
- ++TBase::Size_;
- }
- return valueIter;
- }
- };
- template <typename TKey,
- typename TKeyHash = THash<TKey>,
- typename TKeyEqual = TEqualTo<TKey>>
- class TCompactHashSet: public TCompactHashBase<TKey, TKey, TIdentity, TKeyHash, TKeyEqual> {
- private:
- static_assert(std::is_trivially_destructible<TKey>::value
- && std::is_trivially_copy_assignable<TKey>::value
- && std::is_trivially_move_assignable<TKey>::value
- && std::is_trivially_copy_constructible<TKey>::value
- && std::is_trivially_move_constructible<TKey>::value
- , "Expected POD key type");
- using TBase = TCompactHashBase<TKey, TKey, TIdentity, TKeyHash, TKeyEqual>;
- public:
- TCompactHashSet(TAlignedPagePool& pagePool, size_t size = 0, const TKeyHash& keyHash = TKeyHash(), const TKeyEqual& keyEqual = TKeyEqual())
- : TBase(pagePool, size, TIdentity(), keyHash, keyEqual)
- {
- }
- TCompactHashSet(const TCompactHashSet& other)
- : TBase(other)
- {
- }
- TCompactHashSet(TCompactHashSet&& other)
- : TBase(std::move(other))
- {
- }
- TCompactHashSet& operator= (const TCompactHashSet& rhs) {
- TBase::operator =(rhs);
- return *this;
- }
- TCompactHashSet& operator= (TCompactHashSet&& rhs) {
- TBase::operator =(std::move(rhs));
- return *this;
- }
- bool Insert(TKey key) {
- auto res = TBase::InsertOrReplace(key);
- if (res.second) {
- res.first.Set(key);
- }
- return res.second;
- }
- };
- } // NCHash
- } // NKikimr
|