Repository: shader-slang/slang
File: tools/render-test/slang-test-device-cache.cpp (C++)

Code before:
        deviceCache.erase(oldestIt);
    }
}

Slang::ComPtr<rhi::IDevice> DeviceCache::acquireDevice(const rhi::DeviceDesc& desc)
{
    // Skip caching for CUDA devices due to crashes
    if (desc.deviceType == rhi::DeviceType::CUDA)
    {
        Slang::ComPtr<rhi::IDevice> device;
        auto result = rhi::getRHI()->createDevice(desc, device.writeRef());
        if (SLANG_SUCCEEDED(result))
            return device;
        return nullptr;
    }

    std::lock_guard<std::mutex> lock(getMutex());
    auto& deviceCache = getDeviceCache();
    auto& nextCreationOrder = getNextCreationOrder();

    // Create cache key
    DeviceCacheKey key;
    key.deviceType = desc.deviceType;
    key.enableValidation = desc.enableValidation;
    key.enableRayTracingValidation = desc.enableRayTracingValidation;
    key.profileName = desc.slang.targetProfile ? desc.slang.targetProfile : "";

    // Add required features to key
    for (int i = 0; i < desc.requiredFeatureCount; ++i)
    {
        key.requiredFeatures.push_back(desc.requiredFeatures[i]);
    }
    std::sort(key.requiredFeatures.begin(), key.requiredFeatures.end());

    // Evict oldest device if we've reached the limit
    evictOldestDeviceIfNeeded();

    // Check if we have a cached device
    auto it = deviceCache.find(key);
    if (it != deviceCache.end())
    {
        // Return the cached device - COM reference counting handles the references
        return it->second.device;
    }

    // Create new device
    Slang::ComPtr<rhi::IDevice> device;
    auto result = rhi::getRHI()->createDevice(desc, device.writeRef());
    if (SLANG_FAILED(result))
    {
        return nullptr;

Review comment (suggestion):
I think this line can either be,
```
key.profileName = desc.slang.targetProfile ? desc.slang.targetProfile : "Unknown";
```
or
```
key.profileName = desc.slang.targetProfile;
```

Code after:
        deviceCache.erase(oldestIt);
    }
}

SlangResult DeviceCache::acquireDevice(const rhi::DeviceDesc& desc, rhi::IDevice** outDevice)
{
    if (!outDevice)
        return SLANG_E_INVALID_ARG;

    *outDevice = nullptr;

    // Skip caching for CUDA devices due to crashes
    if (desc.deviceType == rhi::DeviceType::CUDA)
    {
        return rhi::getRHI()->createDevice(desc, outDevice);
    }

    std::lock_guard<std::mutex> lock(getMutex());
    auto& deviceCache = getDeviceCache();
    auto& nextCreationOrder = getNextCreationOrder();

    // Create cache key
    DeviceCacheKey key;
    key.deviceType = desc.deviceType;
    key.enableValidation = desc.enableValidation;
    key.enableRayTracingValidation = desc.enableRayTracingValidation;
    key.profileName = desc.slang.targetProfile ? desc.slang.targetProfile : "Unknown";

    // Add required features to key
    for (int i = 0; i < desc.requiredFeatureCount; ++i)
    {
        key.requiredFeatures.push_back(desc.requiredFeatures[i]);
    }
    std::sort(key.requiredFeatures.begin(), key.requiredFeatures.end());

    // Evict oldest device if we've reached the limit
    evictOldestDeviceIfNeeded();

    // Check if we have a cached device
    auto it = deviceCache.find(key);
    if (it != deviceCache.end())
    {
        // Return the cached device - COM reference counting handles the references
        *outDevice = it->second.device.get();
        if (*outDevice)
        {
            (*outDevice)->addRef();
            return SLANG_OK;
        }
    }