From 605c4608d94da06a4a5225e461da30f47adf2d9f Mon Sep 17 00:00:00 2001 From: LibretroAdmin Date: Mon, 1 Aug 2022 22:14:26 +0200 Subject: [PATCH] Optimize fill_pathname_join - avoid the call to fill_pathname_slash() which would have an implicit strlen cost --- libretro-common/file/file_path.c | 18 ++++++++++++++++-- libretro-common/include/file/file_path.h | 4 ++-- 2 files changed, 18 insertions(+), 4 deletions(-) diff --git a/libretro-common/file/file_path.c b/libretro-common/file/file_path.c index 0b2f953788..fef13b5a39 100644 --- a/libretro-common/file/file_path.c +++ b/libretro-common/file/file_path.c @@ -910,11 +910,25 @@ void fill_pathname_resolve_relative(char *out_path, size_t fill_pathname_join(char *out_path, const char *dir, const char *path, size_t size) { + size_t len = 0; if (out_path != dir) - strlcpy(out_path, dir, size); + len = strlcpy(out_path, dir, size); if (*out_path) - fill_pathname_slash(out_path, size); + { + const char *last_slash = find_last_slash(out_path); + if (last_slash) + { + /* Try to preserve slash type. */ + if (last_slash != (out_path + len - 1)) + { + out_path[len] = last_slash[0]; + out_path[len+1] = '\0'; + } + } + else + strlcat(out_path, PATH_DEFAULT_SLASH(), size); + } return strlcat(out_path, path, size); } diff --git a/libretro-common/include/file/file_path.h b/libretro-common/include/file/file_path.h index 17ca9ded3c..8613e8c9ff 100644 --- a/libretro-common/include/file/file_path.h +++ b/libretro-common/include/file/file_path.h @@ -446,12 +446,12 @@ void fill_pathname_resolve_relative(char *out_path, const char *in_refpath, * @size : size of output path * * Joins a directory (@dir) and path (@path) together. - * Makes sure not to get two consecutive slashes + * Makes sure not to get two consecutive slashes * between directory and path. * * Hidden non-leaf function cost: * - calls strlcpy - * - calls fill_pathname_slash() + * - calls find_last_slash() * - calls strlcat **/ size_t fill_pathname_join(char *out_path, const char *dir,