微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

std::set(rb-tree) 和 std::unordered_set(hashtable) 占用多少堆栈内存?

如何解决std::set(rb-tree) 和 std::unordered_set(hashtable) 占用多少堆栈内存?

考虑到 comparer、hasher 和 allocator 是无状态的,rbtreehashtable 的结构体应该是:

template</*...*/>
struct RBTree {
    Node* header;
    size_t count;
    
    [[no_unique_address]] KeyComparer keyComparer;
    [[no_unique_address]] KeyExtracter keyOf;
    [[no_unique_address]] NodeAllocator nodeAlloc;
};

template</*...*/>
struct HashTable {
    size_t* buckets;
    Node* items;

    size_t capacity;
    size_t count;
    size_t usedBucketCount;

    [[no_unique_address]] KeyHasher keyHasher;
    [[no_unique_address]] KeyExtracter keyOf;
    [[no_unique_address]] NodeAllocator nodeAlloc;
};


static_assert(sizeof(RBTree</*...*/>) == 16);
static_assert(sizeof(HashTable</*...*/> == 40));

但实际上:

constexpr auto sizeOf_set = sizeof(std::set<double>);
// MSVC (std:c++ latest) Debug: 24
// MSVC (std:c++ latest) Release: 16
// clang 10.0.0 (-std=c++20) -O0,-O3: 48

constexpr auto sizeOf_unorderedSet = sizeof(std::unordered_set<double>);
// MSVC (std:c++ latest) Debug: 80
// MSVC (std:c++ latest) Release: 64
// clang 10.0.0 (-std=c++20) -O0,-O3: 56

对于rbtree,MSVC(发布)的结果符合我的预期。 (但为什么 48 是叮当声???) 对于hashtable,我完全糊涂了...

我想知道我是否遗漏了一些必要的东西......

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。