Skip to content

Instantly share code, notes, and snippets.

@jweinst1
Created January 25, 2026 01:02
Show Gist options
  • Select an option

  • Save jweinst1/952054c3d13461a04715db3c5c751356 to your computer and use it in GitHub Desktop.

Select an option

Save jweinst1/952054c3d13461a04715db3c5c751356 to your computer and use it in GitHub Desktop.
slower page partitioned hash insert.
static inline bool insertToSpecPage(uint32_t* page, uint32_t hash, const size_t mask) {
size_t spot_i = hash & mask;
for (int i = 0; i <= mask; ++i)
{
if (page[i] == 0) {
page[i] = hash;
return true;
}
spot_i = (spot_i + 1) & mask;
}
return false;
}
static inline void insertToPages(uint32_t** pages, uint32_t hash, const size_t page_size_mask, const size_t page_mask) {
uint32_t page_i = hash & page_size_mask;
while (true) {
uint32_t* chosenPage = pages[page_i];
if (insertToSpecPage(chosenPage, hash, page_mask))
return;
page_i = (page_i + 1) & page_size_mask;
}
}
static void readTestMultiPage() {
static constexpr size_t testSize = 4000000;
static constexpr size_t testPageSize = 1024;
static constexpr size_t pageSizeMask = testPageSize - 1;
const long page_size = sysconf(_SC_PAGESIZE);
const size_t entryCount = (page_size * testPageSize) / sizeof(uint32_t);
const size_t entryCountPerPage = (page_size / sizeof(uint32_t));
const size_t entryCountPerPageMask = entryCountPerPage - 1;
std::puts("Partitioned Page Test");
std::printf("page size %ld entry per page %zu, total entries %zu\n", page_size, entryCountPerPage, entryCount);
uint32_t* pages[testPageSize] = {nullptr};
for (int i = 0; i < testPageSize; ++i)
{
pages[i] = (uint32_t*)allocateTestPage(1);
}
// inserts
IdGen gens;
std::vector<uint32_t> nums;
for (int i = 0; i < testSize; ++i)
{
nums.push_back(gens.next() & std::numeric_limits<uint32_t>::max());
}
auto startInsert = std::chrono::high_resolution_clock::now();
for (int i = 0; i < testSize; ++i)
{
uint32_t theKey = nums[i];
insertToPages(pages, theKey, pageSizeMask, entryCountPerPageMask);
}
auto endInsert = std::chrono::high_resolution_clock::now();
std::cout << "hash insert " << std::chrono::duration_cast<std::chrono::microseconds>(endInsert - startInsert).count() << "us\n";
}
int main(int argc, char const *argv[])
{
long page_size = sysconf(_SC_PAGESIZE);
std::printf("%ld\n", page_size);
//writeMappedTest_1();
//writeMappedTest_2();
//writeMappedTest_3();
readTestMultiPage();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment