diff mbox series

[FFmpeg-devel,2/8] avutil/vulkan_loader: Avoid redundant strings and relocations

Message ID AS8P250MB074448B08B87D35C791ABA3F8F5C2@AS8P250MB0744.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit e9b4f4461f5156fed5514d5310682a0ae6a35230
Headers show
Series [FFmpeg-devel,1/8] avutil/vulkan: Don't autoinclude vulkan_loader.h | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt March 3, 2024, 6:42 p.m. UTC
There are three possible names for the functions requested;
they only differ in an extension: "", "EXT" or "KHR".
Yet vk_load_info contained pointers to all these strings.
This is wasteful and this commit changes it to avoid
the latter two strings. This saves 6353B of strings,
1776 B of .data.rel.ro as well as 5328 B due to the removed
relocations (corresponding to 2 * 111 removed pointers)
in lavc/vulkan_decode.o alone (ff_vk_load_functions()
is inlined in lavfi/vulkan_filter.c, lavu/hwcontext_vulkan.c
and lavc_vulkan_decode.c, so the savings are three times
this for shared builds; for static builds, the number may
be smaller depending upon whether strings are deduplicated).

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavutil/vulkan_loader.h | 25 +++++++++++++++++--------
 1 file changed, 17 insertions(+), 8 deletions(-)
diff mbox series

Patch

diff --git a/libavutil/vulkan_loader.h b/libavutil/vulkan_loader.h
index f88722f28f..07b6316089 100644
--- a/libavutil/vulkan_loader.h
+++ b/libavutil/vulkan_loader.h
@@ -19,6 +19,9 @@ 
 #ifndef AVUTIL_VULKAN_LOADER_H
 #define AVUTIL_VULKAN_LOADER_H
 
+#include <stdio.h>
+
+#include "avassert.h"
 #include "vulkan_functions.h"
 
 /* Macro to turn a function name into a loader struct */
@@ -28,7 +31,7 @@ 
         req_dev,                                         \
         offsetof(FFVulkanFunctions, name),               \
         ext_flag,                                        \
-        { "vk"#name, "vk"#name"EXT", "vk"#name"KHR" }    \
+        "vk"#name,                                       \
     },
 
 static inline uint64_t ff_vk_extensions_to_mask(const char * const *extensions,
@@ -98,7 +101,7 @@  static inline int ff_vk_load_functions(AVHWDeviceContext *ctx,
         int req_dev;
         size_t struct_offset;
         FFVulkanExtensions ext_flag;
-        const char *names[3];
+        const char *name;
     } vk_load_info[] = {
         FN_LIST(PFN_LOAD_INFO)
 #ifdef _WIN32
@@ -108,6 +111,8 @@  static inline int ff_vk_load_functions(AVHWDeviceContext *ctx,
 
     for (int i = 0; i < FF_ARRAY_ELEMS(vk_load_info); i++) {
         const struct FunctionLoadInfo *load = &vk_load_info[i];
+        static const char extensions[][4] = { "", "EXT", "KHR" };
+        const char *name = load->name;
         PFN_vkVoidFunction fn;
 
         if (load->req_dev  && !has_dev)
@@ -115,15 +120,19 @@  static inline int ff_vk_load_functions(AVHWDeviceContext *ctx,
         if (load->req_inst && !has_inst)
             continue;
 
-        for (int j = 0; j < FF_ARRAY_ELEMS(load->names); j++) {
-            const char *name = load->names[j];
+        for (int j = 0; j < FF_ARRAY_ELEMS(extensions); j++) {
+            char ext_name[128];
+            av_unused int n;
+
+            n = snprintf(ext_name, sizeof(ext_name), "%s%s", name, extensions[j]);
+            av_assert1(n < sizeof(ext_name));
 
             if (load->req_dev)
-                fn = vk->GetDeviceProcAddr(hwctx->act_dev, name);
+                fn = vk->GetDeviceProcAddr(hwctx->act_dev, ext_name);
             else if (load->req_inst)
-                fn = hwctx->get_proc_addr(hwctx->inst, name);
+                fn = hwctx->get_proc_addr(hwctx->inst, ext_name);
             else
-                fn = hwctx->get_proc_addr(NULL, name);
+                fn = hwctx->get_proc_addr(NULL, ext_name);
 
             if (fn)
                 break;
@@ -131,7 +140,7 @@  static inline int ff_vk_load_functions(AVHWDeviceContext *ctx,
 
         if (!fn && ((extensions_mask &~ FF_VK_EXT_NO_FLAG) & load->ext_flag)) {
             av_log(ctx, AV_LOG_ERROR, "Loader error, function \"%s\" indicated "
-                   "as supported, but got NULL function pointer!\n", load->names[0]);
+                   "as supported, but got NULL function pointer!\n", name);
             return AVERROR_EXTERNAL;
         }