From a65afd75342682ae5600afb79e20591c2d193d66 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:12:26 +0200 Subject: [PATCH 1/6] SysInfo caching for Linux --- openVulkanoCpp/Host/Linux/SystemInfo.cpp | 102 ++++++++++++++++------- 1 file changed, 73 insertions(+), 29 deletions(-) diff --git a/openVulkanoCpp/Host/Linux/SystemInfo.cpp b/openVulkanoCpp/Host/Linux/SystemInfo.cpp index 708bf4c..41d396f 100644 --- a/openVulkanoCpp/Host/Linux/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Linux/SystemInfo.cpp @@ -73,7 +73,12 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - return ReadMemInfo("MemTotal"); + static size_t ram; + if (ram == 0) + { + ram = ReadMemInfo("MemTotal"); + } + return ram; } size_t SystemInfo::GetSystemRamAvailable() @@ -98,27 +103,47 @@ namespace OpenVulkano size_t SystemInfo::GetAppVirtualMemoryMax() { - rlimit limit; - if (getrlimit(RLIMIT_AS, &limit) == 0) + static size_t vramMax; + if (vramMax == 0) { - return limit.rlim_cur; + rlimit limit; + if (getrlimit(RLIMIT_AS, &limit) == 0) + { + vramMax = limit.rlim_cur; + } + Logger::PERF->error("Failed to query max application memory"); } - Logger::PERF->error("Failed to query max application memory"); - return 0; + return vramMax; } std::string SystemInfo::GetUserName() { - char* name = getlogin(); - if (!name) return "unknown"; - return name; + static std::string userName; + if (userName.emtpy()) + { + char* name = getlogin(); + if (!name) + { + userName = "unknown"; + } + else + { + userName = name; + } + } + return userName; } std::string SystemInfo::GetHostName() { - char hostname[HOST_NAME_MAX + 1]; - gethostname(hostname, HOST_NAME_MAX + 1); - return hostname; + static std::string hostName; + if (hostName.emtpy()) + { + char hostname[HOST_NAME_MAX + 1]; + gethostname(hostname, HOST_NAME_MAX + 1); + hostName = hostname; + } + return hostName; } std::string SystemInfo::GetDeviceName() @@ -128,30 +153,44 @@ namespace OpenVulkano std::string SystemInfo::GetDeviceVendorName() { - std::ifstream dmiStream("/sys/class/dmi/id/board_vendor"); - if (!dmiStream) + static std::string vendor; + if (vendor.emtpy()) { - Logger::PERF->error("Failed to read /sys/class/dmi/id/board_vendor"); - return "Unknown"; + std::ifstream dmiStream("/sys/class/dmi/id/board_vendor"); + if (!dmiStream) + { + Logger::PERF->error("Failed to read /sys/class/dmi/id/board_vendor"); + vendor = "Unknown"; + } + else + { + std::string motherboardName; + std::getline(dmiStream, motherboardName); + vendor = motherboardName; + } } - - std::string motherboardName; - std::getline(dmiStream, motherboardName); - return motherboardName; + return vendor; } std::string SystemInfo::GetDeviceModelName() { - std::ifstream dmiStream("/sys/class/dmi/id/board_name"); - if (!dmiStream) + static std::string modelName; + if (modelName.emtpy()) { - Logger::PERF->error("Failed to read /sys/class/dmi/id/board_name"); - return "Unknown"; + std::ifstream dmiStream("/sys/class/dmi/id/board_name"); + if (!dmiStream) + { + Logger::PERF->error("Failed to read /sys/class/dmi/id/board_name"); + modelName = "Unknown"; + } + else + { + std::string motherboardName; + std::getline(dmiStream, motherboardName); + modelName = motherboardName; + } } - - std::string motherboardName; - std::getline(dmiStream, motherboardName); - return motherboardName; + return modelName; } namespace @@ -275,7 +314,12 @@ namespace OpenVulkano size_t SystemInfo::GetCpuThreadCount() { - return std::thread::hardware_concurrency(); + static size_t threadCount; + if (threadCount == 0) + { + threadCount = std::thread::hardware_concurrency(); + } + return threadCount; } int32_t SystemInfo::GetCpuTemperature() From 68befaace9a1d464d81cf7fde85fc6a80f228b66 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:12:37 +0200 Subject: [PATCH 2/6] SysInfo caching for MacOS --- openVulkanoCpp/Host/MacOS/SystemInfo.mm | 67 ++++++++++++++++++++----- 1 file changed, 54 insertions(+), 13 deletions(-) diff --git a/openVulkanoCpp/Host/MacOS/SystemInfo.mm b/openVulkanoCpp/Host/MacOS/SystemInfo.mm index ad149f1..1d0711f 100644 --- a/openVulkanoCpp/Host/MacOS/SystemInfo.mm +++ b/openVulkanoCpp/Host/MacOS/SystemInfo.mm @@ -24,7 +24,12 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - return [NSProcessInfo processInfo].physicalMemory; + static size_t ram; + if (ram == 0) + { + ram = [NSProcessInfo processInfo].physicalMemory; + } + return ram; } size_t SystemInfo::GetSystemRamAvailable() @@ -67,12 +72,22 @@ namespace OpenVulkano std::string SystemInfo::GetHostName() { - return [[NSProcessInfo processInfo].hostName UTF8String]; + static std::string hostName; + if (hostName.empty()) + { + hostName = [[NSProcessInfo processInfo].hostName UTF8String]; + } + return hostName; } std::string SystemInfo::GetDeviceName() { - return "Mac"; //TODO + static std::string devName; + if (devName.empty()) + { + devName = "Mac"; //TODO + } + return devName; } std::string SystemInfo::GetDeviceVendorName() @@ -82,9 +97,14 @@ namespace OpenVulkano std::string SystemInfo::GetDeviceModelName() { - struct utsname systemInfo; - uname(&systemInfo); - return systemInfo.machine; + static std::string machine; + if (machine.empty()) + { + struct utsname systemInfo; + uname(&systemInfo); + machine = systemInfo.machine; + } + return machine; } std::string SystemInfo::GetOsName() @@ -94,15 +114,26 @@ namespace OpenVulkano OsVersion SystemInfo::GetOsVersion() { - NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; - return { static_cast(osVersion.majorVersion), static_cast(osVersion.minorVersion), static_cast(osVersion.patchVersion), 0 }; + static OsVersion osVersion; + if (osVersion.major == 0 && osVersion.minor == 0) + { + NSOperatingSystemVersion sysVersion = [NSProcessInfo processInfo].operatingSystemVersion; + osVersion = { static_cast(sysVersion.majorVersion), static_cast(sysVersion.minorVersion), + static_cast(sysVersion.patchVersion), 0 }; + } + return osVersion; } std::string SystemInfo::GetOsNameHumanReadable() { - std::stringstream name; - name << GetOsName() << ' ' << GetOsVersion().major; - return name.str(); + static std::string hrName; + if (hrName.empty()) + { + std::stringstream name; + name << GetOsName() << ' ' << GetOsVersion().major; + hrName = name.str(); + } + return hrName; } DeviceType SystemInfo::GetDeviceType() @@ -112,12 +143,22 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - return [NSProcessInfo processInfo].processorCount; + static size_t coreCount; + if (coreCount == 0) + { + coreCount = [NSProcessInfo processInfo].processorCount; + } + return coreCount; } size_t SystemInfo::GetCpuThreadCount() { - return [NSProcessInfo processInfo].activeProcessorCount; + static size_t procCount; + if (procCount == 0) + { + procCount = [NSProcessInfo processInfo].activeProcessorCount; + } + return procCount; } int32_t SystemInfo::GetCpuTemperature() From 95f76e959916b849eb3f5c28839140238490aa98 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:12:45 +0200 Subject: [PATCH 3/6] SysInfo caching for Windows --- openVulkanoCpp/Host/Windows/SystemInfo.cpp | 254 ++++++++++++--------- 1 file changed, 149 insertions(+), 105 deletions(-) diff --git a/openVulkanoCpp/Host/Windows/SystemInfo.cpp b/openVulkanoCpp/Host/Windows/SystemInfo.cpp index 4d61fef..7832435 100644 --- a/openVulkanoCpp/Host/Windows/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Windows/SystemInfo.cpp @@ -253,6 +253,80 @@ namespace OpenVulkano return res; } + std::string GetHumanReadableOSName() + { + NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW); + OSVERSIONINFOEXW info; + *(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); + info.dwOSVersionInfoSize = sizeof(info); + RtlGetVersion(&info); + if (info.wProductType == VER_NT_WORKSTATION) + { + if (info.dwMajorVersion == 10) + { + if (info.dwBuildNumber >= 22000) + return "Windows 11"; + return "Windows 10"; + } + else if (info.dwMajorVersion == 6) + { + switch (info.dwMinorVersion) + { + case 0: return "Windows Vista"; + case 1: return "Windows 7"; + case 2: return "Windows 8"; + case 3: return "Windows 8.1"; + } + } + + return "Windows " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + } + else + { + if (info.dwMajorVersion == 10) + { + switch (info.dwBuildNumber) + { + case 14393: + return "Windows Server 2016"; + case 16299: + return "Windows Server 1709"; + case 17134: + return "Windows Server 1803"; + case 17763: + return "Windows Server 2019"; + case 18362: + return "Windows Server 1903"; + case 18363: + return "Windows Server 1909"; + case 19041: + return "Windows Server 2004"; + case 19042: + return "Windows Server 20H2"; + case 20348: + return "Windows Server 2022"; + case 25398: + return "Windows Server 23H2"; + case 26100: + return "Windows Server 2025"; + default: + return "Windows Server"; + } + } + else if (info.dwMajorVersion == 6) + { + switch (info.dwMinorVersion) + { + case 0: return "Windows Server 2008"; + case 1: return "Windows Server 2008 R2"; + case 2: return "Windows Server 2012"; + case 3: return "Windows Server 2012 R2"; + } + } + + return "Windows Server " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + } + } } size_t SystemInfo::GetSystemRam() @@ -287,37 +361,62 @@ namespace OpenVulkano std::string SystemInfo::GetUserName() { - char username[UNLEN+1]; - DWORD username_len = UNLEN+1; - ::GetUserNameA(username, &username_len); - return username; + static std::string userName; + if (userName.empty()) + { + char username[UNLEN + 1]; + DWORD username_len = UNLEN + 1; + ::GetUserNameA(username, &username_len); + userName = username; + } + return userName; } std::string SystemInfo::GetHostName() { - char hostname[UNLEN+1]; - gethostname(hostname, UNLEN+1); - return hostname; + static std::string hostName; + if (hostName.empty()) + { + char hostname[UNLEN+1]; + gethostname(hostname, UNLEN+1); + hostName = hostname; + } + return hostName; } std::string SystemInfo::GetDeviceName() { - char computerName[UNLEN+1]; - DWORD computerName_len = UNLEN+1; - GetComputerNameA(computerName, &computerName_len); - return computerName; + static std::string devName; + if (devName.empty()) + { + char computerName[UNLEN+1]; + DWORD computerName_len = UNLEN+1; + GetComputerNameA(computerName, &computerName_len); + devName = computerName; + } + return devName; } std::string SystemInfo::GetDeviceVendorName() { - std::optional res = GetWMIProperty("Manufacturer"); - return res ? *res : "Unknown"; + static std::string vendorName; + if (vendorName.empty()) + { + std::optional res = GetWMIProperty("Manufacturer"); + vendorName = res ? *res : "Unknown"; + } + return vendorName; } std::string SystemInfo::GetDeviceModelName() { - std::optional res = GetWMIProperty("Model"); - return res ? *res : "Unknown"; + static std::string deviceModelName; + if (deviceModelName.empty()) + { + std::optional res = GetWMIProperty("Model"); + deviceModelName = res ? *res : "Unknown"; + } + return deviceModelName; } std::string SystemInfo::GetOsName() @@ -327,87 +426,27 @@ namespace OpenVulkano OsVersion SystemInfo::GetOsVersion() { - NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); - OSVERSIONINFOEXW info; - *(FARPROC*) &RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); - info.dwOSVersionInfoSize = sizeof(info); - RtlGetVersion(&info); - return { (int)info.dwMajorVersion, (int)info.dwMinorVersion, 0, (int)info.dwBuildNumber }; + static OsVersion osVersion; + if (osVersion.major == 0 && osVersion.minor == 0) + { + NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); + OSVERSIONINFOEXW info; + *(FARPROC*) &RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); + info.dwOSVersionInfoSize = sizeof(info); + RtlGetVersion(&info); + osVersion = { (int)info.dwMajorVersion, (int)info.dwMinorVersion, 0, (int)info.dwBuildNumber }; + } + return osVersion; } std::string SystemInfo::GetOsNameHumanReadable() { - NTSTATUS(WINAPI *RtlGetVersion)(LPOSVERSIONINFOEXW); - OSVERSIONINFOEXW info; - *(FARPROC*)&RtlGetVersion = GetProcAddress(GetModuleHandleA("ntdll"), "RtlGetVersion"); - info.dwOSVersionInfoSize = sizeof(info); - RtlGetVersion(&info); - if (info.wProductType == VER_NT_WORKSTATION) + static std::string osName; + if (osName.empty()) { - if (info.dwMajorVersion == 10) - { - if (info.dwBuildNumber >= 22000) - return "Windows 11"; - return "Windows 10"; - } - else if (info.dwMajorVersion == 6) - { - switch (info.dwMinorVersion) - { - case 0: return "Windows Vista"; - case 1: return "Windows 7"; - case 2: return "Windows 8"; - case 3: return "Windows 8.1"; - } - } - - return "Windows " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); - } - else - { - if (info.dwMajorVersion == 10) - { - switch (info.dwBuildNumber) - { - case 14393: - return "Windows Server 2016"; - case 16299: - return "Windows Server 1709"; - case 17134: - return "Windows Server 1803"; - case 17763: - return "Windows Server 2019"; - case 18362: - return "Windows Server 1903"; - case 18363: - return "Windows Server 1909"; - case 19041: - return "Windows Server 2004"; - case 19042: - return "Windows Server 20H2"; - case 20348: - return "Windows Server 2022"; - case 25398: - return "Windows Server 23H2"; - case 26100: - return "Windows Server 2025"; - default: - return "Windows Server"; - } - } - else if (info.dwMajorVersion == 6) - { - switch (info.dwMinorVersion) - { - case 0: return "Windows Server 2008"; - case 1: return "Windows Server 2008 R2"; - case 2: return "Windows Server 2012"; - case 3: return "Windows Server 2012 R2"; - } - } - - return "Windows Server " + std::to_string(info.dwMajorVersion) + "." + std::to_string(info.dwMinorVersion); + osName = GetHumanReadableOSName(); } + return osName; } DeviceType SystemInfo::GetDeviceType() @@ -417,23 +456,28 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - DWORD bufferSize = 0; - GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize); - - std::vector buf(bufferSize); - SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast(buf.data()); - GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, info, &bufferSize); - - size_t physProcessorCount = 0; - BYTE* start = buf.data(); - BYTE* end = buf.data() + bufferSize; - while (start < end) + static size_t coreCount; + if (coreCount == 0) { - SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* current = reinterpret_cast(start); - physProcessorCount++; - start += current->Size; + DWORD bufferSize = 0; + GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, nullptr, &bufferSize); + + std::vector buf(bufferSize); + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* info = reinterpret_cast(buf.data()); + GetLogicalProcessorInformationEx(LOGICAL_PROCESSOR_RELATIONSHIP::RelationProcessorCore, info, &bufferSize); + + size_t physProcessorCount = 0; + BYTE* start = buf.data(); + BYTE* end = buf.data() + bufferSize; + while (start < end) + { + SYSTEM_LOGICAL_PROCESSOR_INFORMATION_EX* current = reinterpret_cast(start); + physProcessorCount++; + start += current->Size; + } + coreCount = physProcessorCount; } - return physProcessorCount; + return coreCount; } size_t SystemInfo::GetCpuThreadCount() From 15cc73f5e800d444b052ec8b9e8ad604ffd8f4c5 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:12:53 +0200 Subject: [PATCH 4/6] SysInfo caching for iOS --- openVulkanoCpp/Host/iOS/SystemInfo.mm | 87 +++++++++++++++++++++------ 1 file changed, 69 insertions(+), 18 deletions(-) diff --git a/openVulkanoCpp/Host/iOS/SystemInfo.mm b/openVulkanoCpp/Host/iOS/SystemInfo.mm index 6034484..e3ee351 100644 --- a/openVulkanoCpp/Host/iOS/SystemInfo.mm +++ b/openVulkanoCpp/Host/iOS/SystemInfo.mm @@ -27,7 +27,12 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - return [NSProcessInfo processInfo].physicalMemory; + static size_t ram; + if (ram == 0) + { + ram = [NSProcessInfo processInfo].physicalMemory; + } + return ram; } size_t SystemInfo::GetSystemRamAvailable() @@ -70,12 +75,22 @@ namespace OpenVulkano std::string SystemInfo::GetHostName() { - return [[NSProcessInfo processInfo].hostName UTF8String]; + static std::string hostName; + if (hostName.empty()) + { + hostName = [[NSProcessInfo processInfo].hostName UTF8String]; + } + return hostName; } std::string SystemInfo::GetDeviceName() { - return [[[UIDevice currentDevice] name] UTF8String]; + static std::string devName; + if (devName.empty()) + { + devName = [[[UIDevice currentDevice] name] UTF8String]; + } + return devName; } std::string SystemInfo::GetDeviceVendorName() @@ -98,43 +113,79 @@ namespace OpenVulkano std::string SystemInfo::GetOsName() { - return [[[UIDevice currentDevice] systemName] UTF8String]; + static std::string osName; + if (osName.empty()) + { + osName = [[[UIDevice currentDevice] systemName] UTF8String]; + } + return osName; } OsVersion SystemInfo::GetOsVersion() { - NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; - return { static_cast(osVersion.majorVersion), static_cast(osVersion.minorVersion), static_cast(osVersion.patchVersion), 0 }; + static OsVersion osv; + if (osv.major == 0 && osv.minor == 0) + { + NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; + osv = { static_cast(osVersion.majorVersion), static_cast(osVersion.minorVersion), + static_cast(osVersion.patchVersion), 0 }; + } + return osv; } std::string SystemInfo::GetOsNameHumanReadable() { - OsVersion osVersion = GetOsVersion(); - return fmt::format("{} {}.{}", GetOsName(), osVersion.major, osVersion.minor); + static std::string hrName; + if (hrName.empty()) + { + OsVersion osVersion = GetOsVersion(); + hrName = fmt::format("{} {}.{}", GetOsName(), osVersion.major, osVersion.minor); + } + return hrName; } DeviceType SystemInfo::GetDeviceType() { - switch ([UIDevice currentDevice].userInterfaceIdiom) + static DeviceType devType = DeviceType::Unknown; + if (devType == DeviceType::Unknown) { - case UIUserInterfaceIdiomPhone: return DeviceType::Phone; - case UIUserInterfaceIdiomPad: return DeviceType::Tablet; - case UIUserInterfaceIdiomTV: return DeviceType::TV; - case UIUserInterfaceIdiomMac: return DeviceType::PC; - case UIUserInterfaceIdiomVision: return DeviceType::VR; - default: break; + switch ([UIDevice currentDevice].userInterfaceIdiom) + { + case UIUserInterfaceIdiomPhone: + devType = DeviceType::Phone; + case UIUserInterfaceIdiomPad: + devType = DeviceType::Tablet; + case UIUserInterfaceIdiomTV: + devType = DeviceType::TV; + case UIUserInterfaceIdiomMac: + devType = DeviceType::PC; + case UIUserInterfaceIdiomVision: + devType = DeviceType::VR; + default: + break; + } } - return DeviceType::Unknown; + return devType; } size_t SystemInfo::GetCpuCoreCount() { - return [NSProcessInfo processInfo].processorCount; + static size_t coreCount; + if (coreCount == 0) + { + coreCount = [NSProcessInfo processInfo].processorCount; + } + return coreCount; } size_t SystemInfo::GetCpuThreadCount() { - return [NSProcessInfo processInfo].activeProcessorCount; + static size_t threadCount; + if (threadCount == 0) + { + threadCount = [NSProcessInfo processInfo].activeProcessorCount; + } + return threadCount; } int32_t SystemInfo::GetCpuTemperature() From 90440b3f34eb6402cc29ad3987afce36573ede19 Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 15:31:20 +0200 Subject: [PATCH 5/6] Fixed a bunch of typos in linux's sysinfo --- openVulkanoCpp/Host/Linux/SystemInfo.cpp | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/openVulkanoCpp/Host/Linux/SystemInfo.cpp b/openVulkanoCpp/Host/Linux/SystemInfo.cpp index 41d396f..64ad209 100644 --- a/openVulkanoCpp/Host/Linux/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Linux/SystemInfo.cpp @@ -119,7 +119,7 @@ namespace OpenVulkano std::string SystemInfo::GetUserName() { static std::string userName; - if (userName.emtpy()) + if (userName.empty()) { char* name = getlogin(); if (!name) @@ -137,7 +137,7 @@ namespace OpenVulkano std::string SystemInfo::GetHostName() { static std::string hostName; - if (hostName.emtpy()) + if (hostName.empty()) { char hostname[HOST_NAME_MAX + 1]; gethostname(hostname, HOST_NAME_MAX + 1); @@ -154,7 +154,7 @@ namespace OpenVulkano std::string SystemInfo::GetDeviceVendorName() { static std::string vendor; - if (vendor.emtpy()) + if (vendor.empty()) { std::ifstream dmiStream("/sys/class/dmi/id/board_vendor"); if (!dmiStream) @@ -175,7 +175,7 @@ namespace OpenVulkano std::string SystemInfo::GetDeviceModelName() { static std::string modelName; - if (modelName.emtpy()) + if (modelName.empty()) { std::ifstream dmiStream("/sys/class/dmi/id/board_name"); if (!dmiStream) From 947faebe9c2b2c3e8256e6aafded5d7ec4ef1e4d Mon Sep 17 00:00:00 2001 From: Vladyslav Baranovskyi Date: Fri, 20 Dec 2024 16:10:10 +0200 Subject: [PATCH 6/6] Removed redundant if statements where possible --- openVulkanoCpp/Host/Linux/SystemInfo.cpp | 12 ++----- openVulkanoCpp/Host/MacOS/SystemInfo.mm | 41 +++++----------------- openVulkanoCpp/Host/Windows/SystemInfo.cpp | 10 ++---- openVulkanoCpp/Host/iOS/SystemInfo.mm | 38 ++++---------------- 4 files changed, 20 insertions(+), 81 deletions(-) diff --git a/openVulkanoCpp/Host/Linux/SystemInfo.cpp b/openVulkanoCpp/Host/Linux/SystemInfo.cpp index 64ad209..a47e77e 100644 --- a/openVulkanoCpp/Host/Linux/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Linux/SystemInfo.cpp @@ -73,11 +73,7 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - static size_t ram; - if (ram == 0) - { - ram = ReadMemInfo("MemTotal"); - } + static const size_t ram = ReadMemInfo("MemTotal"); return ram; } @@ -314,11 +310,7 @@ namespace OpenVulkano size_t SystemInfo::GetCpuThreadCount() { - static size_t threadCount; - if (threadCount == 0) - { - threadCount = std::thread::hardware_concurrency(); - } + static const size_t threadCount = std::thread::hardware_concurrency(); return threadCount; } diff --git a/openVulkanoCpp/Host/MacOS/SystemInfo.mm b/openVulkanoCpp/Host/MacOS/SystemInfo.mm index 1d0711f..b5d4d41 100644 --- a/openVulkanoCpp/Host/MacOS/SystemInfo.mm +++ b/openVulkanoCpp/Host/MacOS/SystemInfo.mm @@ -7,6 +7,7 @@ #include "Host/SystemInfo.hpp" #include "Base/Logger.hpp" #include +#include #include #include @@ -24,11 +25,7 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - static size_t ram; - if (ram == 0) - { - ram = [NSProcessInfo processInfo].physicalMemory; - } + static const size_t ram = [NSProcessInfo processInfo].physicalMemory; return ram; } @@ -72,21 +69,13 @@ namespace OpenVulkano std::string SystemInfo::GetHostName() { - static std::string hostName; - if (hostName.empty()) - { - hostName = [[NSProcessInfo processInfo].hostName UTF8String]; - } + static const std::string hostName = [[NSProcessInfo processInfo].hostName UTF8String]; return hostName; } std::string SystemInfo::GetDeviceName() { - static std::string devName; - if (devName.empty()) - { - devName = "Mac"; //TODO - } + static const std::string devName = "Mac"; //TODO return devName; } @@ -114,7 +103,7 @@ namespace OpenVulkano OsVersion SystemInfo::GetOsVersion() { - static OsVersion osVersion; + static OsVersion osVersion = {}; if (osVersion.major == 0 && osVersion.minor == 0) { NSOperatingSystemVersion sysVersion = [NSProcessInfo processInfo].operatingSystemVersion; @@ -126,13 +115,7 @@ namespace OpenVulkano std::string SystemInfo::GetOsNameHumanReadable() { - static std::string hrName; - if (hrName.empty()) - { - std::stringstream name; - name << GetOsName() << ' ' << GetOsVersion().major; - hrName = name.str(); - } + static const std::string hrName = fmt::format("{} {}", GetOsName(), GetOsVersion().major); return hrName; } @@ -143,21 +126,13 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - static size_t coreCount; - if (coreCount == 0) - { - coreCount = [NSProcessInfo processInfo].processorCount; - } + static const size_t coreCount = [NSProcessInfo processInfo].processorCount; return coreCount; } size_t SystemInfo::GetCpuThreadCount() { - static size_t procCount; - if (procCount == 0) - { - procCount = [NSProcessInfo processInfo].activeProcessorCount; - } + static size_t procCount = [NSProcessInfo processInfo].activeProcessorCount; return procCount; } diff --git a/openVulkanoCpp/Host/Windows/SystemInfo.cpp b/openVulkanoCpp/Host/Windows/SystemInfo.cpp index 7832435..2f4eecc 100644 --- a/openVulkanoCpp/Host/Windows/SystemInfo.cpp +++ b/openVulkanoCpp/Host/Windows/SystemInfo.cpp @@ -426,7 +426,7 @@ namespace OpenVulkano OsVersion SystemInfo::GetOsVersion() { - static OsVersion osVersion; + static OsVersion osVersion = {}; if (osVersion.major == 0 && osVersion.minor == 0) { NTSTATUS(WINAPI * RtlGetVersion)(LPOSVERSIONINFOEXW); @@ -441,11 +441,7 @@ namespace OpenVulkano std::string SystemInfo::GetOsNameHumanReadable() { - static std::string osName; - if (osName.empty()) - { - osName = GetHumanReadableOSName(); - } + static const std::string osName = GetHumanReadableOSName(); return osName; } @@ -456,7 +452,7 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - static size_t coreCount; + static size_t coreCount = 0; if (coreCount == 0) { DWORD bufferSize = 0; diff --git a/openVulkanoCpp/Host/iOS/SystemInfo.mm b/openVulkanoCpp/Host/iOS/SystemInfo.mm index e3ee351..af0c922 100644 --- a/openVulkanoCpp/Host/iOS/SystemInfo.mm +++ b/openVulkanoCpp/Host/iOS/SystemInfo.mm @@ -27,11 +27,7 @@ namespace OpenVulkano size_t SystemInfo::GetSystemRam() { - static size_t ram; - if (ram == 0) - { - ram = [NSProcessInfo processInfo].physicalMemory; - } + static const size_t ram = [NSProcessInfo processInfo].physicalMemory; return ram; } @@ -75,21 +71,13 @@ namespace OpenVulkano std::string SystemInfo::GetHostName() { - static std::string hostName; - if (hostName.empty()) - { - hostName = [[NSProcessInfo processInfo].hostName UTF8String]; - } + static const std::string hostName = [[NSProcessInfo processInfo].hostName UTF8String]; return hostName; } std::string SystemInfo::GetDeviceName() { - static std::string devName; - if (devName.empty()) - { - devName = [[[UIDevice currentDevice] name] UTF8String]; - } + static const std::string devName = [[[UIDevice currentDevice] name] UTF8String]; return devName; } @@ -113,17 +101,13 @@ namespace OpenVulkano std::string SystemInfo::GetOsName() { - static std::string osName; - if (osName.empty()) - { - osName = [[[UIDevice currentDevice] systemName] UTF8String]; - } + static const std::string osName = [[[UIDevice currentDevice] systemName] UTF8String]; return osName; } OsVersion SystemInfo::GetOsVersion() { - static OsVersion osv; + static OsVersion osv = {}; if (osv.major == 0 && osv.minor == 0) { NSOperatingSystemVersion osVersion = [NSProcessInfo processInfo].operatingSystemVersion; @@ -170,21 +154,13 @@ namespace OpenVulkano size_t SystemInfo::GetCpuCoreCount() { - static size_t coreCount; - if (coreCount == 0) - { - coreCount = [NSProcessInfo processInfo].processorCount; - } + static const size_t coreCount = [NSProcessInfo processInfo].processorCount; return coreCount; } size_t SystemInfo::GetCpuThreadCount() { - static size_t threadCount; - if (threadCount == 0) - { - threadCount = [NSProcessInfo processInfo].activeProcessorCount; - } + static const size_t threadCount = [NSProcessInfo processInfo].activeProcessorCount; return threadCount; }