mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-22 10:50:15 -10:00
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.