Repository: google/filament
File: filament/backend/include/private/backend/HandleAllocator.h (C/C++)

Code before:
#include <utils/Allocator.h>
#include <utils/CString.h>
#include <utils/Log.h>
#include <utils/Panic.h>
#include <utils/compiler.h>
#include <utils/debug.h>
#include <utils/ostream.h>

#include <tsl/robin_map.h>

#include <atomic>
#include <cstddef>
#include <exception>
#include <mutex>
#include <type_traits>
#include <unordered_map>
#include <utility>

#include <stddef.h>
#include <stdint.h>

#define HandleAllocatorGL   HandleAllocator<32,  96, 184>    // ~4520 / pool / MiB
#define HandleAllocatorVK   HandleAllocator<64, 160, 312>    // ~1820 / pool / MiB
#define HandleAllocatorMTL  HandleAllocator<32,  64, 552>    // ~1660 / pool / MiB
// TODO Update WGSL values
#define HandleAllocatorWGSL HandleAllocator<552, 552, 552>    // ~1660 / pool / MiB

namespace filament::backend {

// This is used to not duplicate the code for the Tags management
class DebugTag {
public:
    DebugTag();
    void writePoolHandleTag(HandleBase::HandleId key, utils::CString&& tag) noexcept;
    void writeHeapHandleTag(HandleBase::HandleId key, utils::CString&& tag) noexcept;
    utils::CString findHandleTag(HandleBase::HandleId key) const noexcept;

private:
    // This is used to associate a tag to a handle. mDebugTags is only written the in the main
    // driver thread, but it can be accessed from any thread, because it's called from handle_cast<>
    // which is used by synchronous calls.
    mutable utils::Mutex mDebugTagLock;
    tsl::robin_map<HandleBase::HandleId, utils::CString> mDebugTags;
};

/*
 * A utility class to efficiently allocate and manage Handle<>
 */
template<size_t P0, size_t P1, size_t P2>
class HandleAllocator : public DebugTag {
public:

Review comment (suggestion):
it will be better if we use different values here. Purpose here is to create different kind of memory buckets: small, medium and large. We can update it with real values once all handles are done.

Code after:
#include <utils/Allocator.h>
#include <utils/CString.h>
#include <utils/Log.h>
#include <utils/Panic.h>
#include <utils/compiler.h>
#include <utils/debug.h>
#include <utils/ostream.h>

#include <tsl/robin_map.h>

#include <atomic>
#include <cstddef>
#include <exception>
#include <mutex>
#include <type_traits>
#include <unordered_map>
#include <utility>

#include <stddef.h>
#include <stdint.h>

#define HandleAllocatorGL   HandleAllocator<32,  96, 184>    // ~4520 / pool / MiB
#define HandleAllocatorVK   HandleAllocator<64, 160, 312>    // ~1820 / pool / MiB
#define HandleAllocatorMTL  HandleAllocator<32,  64, 552>    // ~1660 / pool / MiB
// TODO WebGPU examine right size of handles
#define HandleAllocatorWGPU HandleAllocator<64, 160, 552>    // ~1820 / pool / MiB

namespace filament::backend {

// This is used to not duplicate the code for the Tags management
class DebugTag {
public:
    DebugTag();
    void writePoolHandleTag(HandleBase::HandleId key, utils::CString&& tag) noexcept;
    void writeHeapHandleTag(HandleBase::HandleId key, utils::CString&& tag) noexcept;
    utils::CString findHandleTag(HandleBase::HandleId key) const noexcept;

private:
    // This is used to associate a tag to a handle. mDebugTags is only written the in the main
    // driver thread, but it can be accessed from any thread, because it's called from handle_cast<>
    // which is used by synchronous calls.
    mutable utils::Mutex mDebugTagLock;
    tsl::robin_map<HandleBase::HandleId, utils::CString> mDebugTags;
};

/*
 * A utility class to efficiently allocate and manage Handle<>
 */
template<size_t P0, size_t P1, size_t P2>
class HandleAllocator : public DebugTag {
public: