Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Return an std::string_view when possible for vk::to_string functions. #2092

Open
Wunkolo opened this issue Mar 5, 2025 · 2 comments
Open

Comments

@Wunkolo
Copy link
Contributor

Wunkolo commented Mar 5, 2025

Functions that return the name of plain enums(not bit-flag enums and such that require building a new string of active bits) are just returning pointers to plaintext strings residing in .rdata, but they currently requiring allocation of a new std::string in all cases:

VULKAN_HPP_INLINE std::string to_string( StructureType value )
{
switch ( value )
{
case StructureType::eApplicationInfo : return "ApplicationInfo";
case StructureType::eInstanceCreateInfo : return "InstanceCreateInfo";
case StructureType::eDeviceQueueCreateInfo : return "DeviceQueueCreateInfo";
case StructureType::eDeviceCreateInfo : return "DeviceCreateInfo";
case StructureType::eSubmitInfo : return "SubmitInfo";
case StructureType::eMemoryAllocateInfo : return "MemoryAllocateInfo";
case StructureType::eMappedMemoryRange : return "MappedMemoryRange";
case StructureType::eBindSparseInfo : return "BindSparseInfo";
case StructureType::eFenceCreateInfo : return "FenceCreateInfo";
case StructureType::eSemaphoreCreateInfo : return "SemaphoreCreateInfo";
case StructureType::eEventCreateInfo : return "EventCreateInfo";
case StructureType::eQueryPoolCreateInfo : return "QueryPoolCreateInfo";

These enums in particular could be returning a std::string_view(since C++17) utilizing string-view literals so that the length of the string is known at compile-time rather than costing an additional strlen and avoids an additional string-allocation at run-time like so:

 VULKAN_HPP_INLINE std::string_view to_string( StructureType value ) 
 { 
   using namespace std::string_view_literals
   switch ( value ) 
   { 
     case StructureType::eApplicationInfo                                     : return "ApplicationInfo"sv; 
     case StructureType::eInstanceCreateInfo                                  : return "InstanceCreateInfo"sv; 
     case StructureType::eDeviceQueueCreateInfo                               : return "DeviceQueueCreateInfo"sv; 
     case StructureType::eDeviceCreateInfo                                    : return "DeviceCreateInfo"sv; 
...

With this pattern, no additional memory has to be allocated unless the user chooses to cast it to an std::string later.

@asuessenbach
Copy link
Contributor

Thanks for this suggestion.
Will address it soon.

@asuessenbach
Copy link
Contributor

asuessenbach commented Mar 13, 2025

@Wunkolo Unfortunately, all those vk::to_string functions do not only return a constant string, but have some default path returning hex-formatted value. Therefore, they can't return a std::string_view.
I realized, though, that those functions could be marked with VULKAN_HPP_CONSTEXPR_20. That is, with C++20 and above, they are constexpr, which is at least a bit.

-> #2101.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants