From f3aba255f6d69ddbf255b33d0eb0f391908e60a8 Mon Sep 17 00:00:00 2001 From: Yong He Date: Wed, 12 Jul 2023 19:55:41 -0700 Subject: Pool inst worklists and hashsets to avoid rehashing. (#2982) Co-authored-by: Yong He --- source/slang/slang-container-pool.h | 93 +++++++++++++++++++++++++++++++++++++ 1 file changed, 93 insertions(+) create mode 100644 source/slang/slang-container-pool.h (limited to 'source/slang/slang-container-pool.h') diff --git a/source/slang/slang-container-pool.h b/source/slang/slang-container-pool.h new file mode 100644 index 000000000..addeb1607 --- /dev/null +++ b/source/slang/slang-container-pool.h @@ -0,0 +1,93 @@ +#ifndef SLANG_CONTAINER_POOL_H +#define SLANG_CONTAINER_POOL_H + +#include "../core/slang-list.h" +#include "../core/slang-dictionary.h" +#include "../core/slang-virtual-object-pool.h" + +// A pool to allow reuse of common types of containers to avoid +// frequent resizing and rehashing. + +namespace Slang +{ +static const int kContainerPoolSize = 1024; + +template +struct ObjectPool +{ + ObjectPool(int maxElementCount) + { + m_pool.initPool(maxElementCount); + m_objects.setCount(maxElementCount); + } + + T* getObject() + { + auto id = m_pool.alloc(1); + if (id == -1) + SLANG_ABORT_COMPILATION("container pool allocation failure."); + return &m_objects[id]; + } + + void freeObject(T* object) + { + auto id = (int)(object - m_objects.getBuffer()); + m_pool.free(id, 1); + } + + VirtualObjectPool m_pool; + List m_objects; +}; + +struct ContainerPool +{ + ObjectPool> m_listPool; + ObjectPool> m_dictionaryPool; + ObjectPool> m_hashSetPool; + + ContainerPool() + :m_listPool(kContainerPoolSize), m_dictionaryPool(kContainerPoolSize), m_hashSetPool(kContainerPoolSize) + {} + + template + List* getList() + { + return (List*)m_listPool.getObject(); + } + + template + Dictionary* getDictionary() + { + return (Dictionary*)m_dictionaryPool.getObject(); + } + + template + HashSet* getHashSet() + { + return (HashSet*)m_hashSetPool.getObject(); + } + + template + void free(List* list) + { + list->clear(); + m_listPool.freeObject((List*)list); + } + + template + void free(Dictionary* dict) + { + dict->clear(); + m_dictionaryPool.freeObject((Dictionary*)dict); + } + + template + void free(HashSet* set) + { + set->clear(); + m_hashSetPool.freeObject((HashSet*)set); + } +}; +} + +#endif -- cgit v1.2.3