Files
RetroArch/menu/menu_str.h
LibretroAdmin 39a9be716c menu: share node fullpaths instead of copying one per entry
Every row of a list is inserted with the same fullpath -- it is the path
of the menu stack top, one string for the whole list -- and both xmb and
ozone strdup()ed it per row. A playlist paid a malloc() and a full path
copy tens of thousands of times for a string neither driver ever writes
to; node->fullpath is read in exactly one place per driver and never
mutated.

menu_str_ref()/menu_str_unref() hand out a reference-counted shared
copy. The reference is a pointer to the characters rather than to the
header, so node->fullpath stays a plain char* and every read site is
untouched; only the four allocate-and-release sites change. The
consequence is that a fullpath must be released with menu_str_unref()
and never with free().

The one-entry cache compares content rather than the source pointer. The
caller's string lives in a list entry that can be freed and another
allocated at the same address between builds, which a pointer compare
takes for a hit and hands back the previous list's path. A strcmp costs
a fraction of the malloc() and copy it avoids.

Lives in its own translation unit rather than in menu_driver.c so that
it can be tested without linking the frontend.

Measured on a 100k-entry list build, one variant per process: ~116 ->
~89 ns/entry, 11.6 -> 8.9 ms.

test/menu/test_menu_str.c covers sharing across distinct buffers with
equal content, the same address with different content, release in
forward and reverse order, NULL and empty input, repeated flushes, and
alternating strings that defeat the cache. ASan+UBSan+LSan and TSan both
10/10. Re-keying the cache on the source pointer fails two of them,
including the stale-cache case, where the second caller is handed the
first string.
2026-08-30 00:41:30 +02:00

57 lines
2.2 KiB
C

/* Copyright (C) 2010-2026 The RetroArch team
*
* ---------------------------------------------------------------------------------------
* The following license statement only applies to this file (menu_str.h).
* ---------------------------------------------------------------------------------------
*
* Permission is hereby granted, free of charge,
* to any person obtaining a copy of this software and associated documentation files (the "Software"),
* to deal in the Software without restriction, including without limitation the rights to
* use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
* and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
* INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
* WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#ifndef _MENU_STR_H
#define _MENU_STR_H
#include <retro_common_api.h>
RETRO_BEGIN_DECLS
/**
* Take a reference to a shared copy of @s.
*
* The menu drivers use this for node fullpaths, which are one string
* per list repeated across every row. The result points at the
* characters, so it is used as an ordinary C string, but it MUST be
* released with menu_str_unref() rather than free().
*
* @return The shared string, or NULL if @s was NULL or on OOM.
*/
char *menu_str_ref(const char *s);
/**
* Drop one reference taken with menu_str_ref(). NULL is accepted and
* ignored, matching free().
*/
void menu_str_unref(char *s);
/**
* Drop the internal one-entry cache's own reference. Called at menu
* teardown so the last shared string does not outlive the drivers.
*/
void menu_str_cache_flush(void);
RETRO_END_DECLS
#endif