mirror of
https://github.com/libretro/RetroArch.git
synced 2026-11-23 09:49:34 -10:00
* dir_list: expose the sort comparators dir_list_sort() and dir_list_sort_ignore_ext() each pick one of four static qsort comparators. dir_list_sort_cmp() now hands the chosen comparator to callers that need the identical ordering through a different sorting strategy - concretely, a menu-side incremental sort that must produce the same listing order as today's blocking qsort while doing bounded work per invocation. The two sort entry points route through the selector, so there is exactly one source of the ordering and no behaviour change. Two listings sorted with the same comparator agree everywhere except among entries that compare equal (case-insensitive twins in one directory), whose relative order qsort never specified in the first place; the header says so. Verified: strict C89 syntax pass; full Linux build links; the dirwalk oracle added alongside the first consumer asserts listing parity against dir_list_sort() across every flag combination the menu uses. * menu: add menu_dirwalk, a budgeted cancellable directory walk Every interactive displaylist that lists a directory - the file browser's three dir_list_initialize variants plus its sort, and the four playlist-directory walks - performs the enumeration and the sort synchronously on the thread that drives the UI, a stall proportional to directory size. menu_dirwalk is the mechanism the follow-up commits route those sites through; this commit adds the module, its build wiring, and its oracle, with no callers yet. A request first runs the walk synchronously under the shared per-frame I/O window (tasks/task_nbio_slice.c): dir_list_iter consults the budget between entries with a one-entry floor, so small and medium directories - the overwhelming case - complete on the spot with the identical single enumeration and single sort as today, merely capped. A directory too large for the window continues on a silent retro_task and, when Threaded Tasks is on, leaves the main thread entirely; completion fires a registered refresh callback (the task_menu_explore hand-off pattern), and the repopulation that callback triggers re-issues the identical request and consumes the finished listing. Nothing is enumerated or sorted twice. One walk is pending at a time, identity is the full parameter set plus a consumer tag, a superseding request or menu_dirwalk_cancel() ends the previous walk, and a generation counter makes late completions of cancelled walks no-ops. Sorting turned out to be the larger half of the stall: qsort of a 40k-entry listing costs ~15ms of main-thread time, measured as an 18.5ms worst gather when the sort ran whole in the handler. Above a synchronous threshold the sort is therefore resumable too - fixed runs qsorted one or more per window, then bottom-up merge passes with the budget consulted every batch of copied elements, ordered by the exact comparator dir_list_sort_cmp() exposes. A walk that fits the window but produces an oversized listing defers just the sort. Scratch allocation failure falls back to the one-shot qsort: a stall is a lesser evil than a lost listing. The oracle (samples/menu/dirwalk, wired into Linux-samples-tasks) compiles the shipping TU with shrunken sort constants and provides the clock itself: real time for the pacing and bench lanes, a fixed virtual step for deterministic window exhaustion in the deferral, cancel and threaded lanes. Parity asserts a consumed listing equals dir_list_new() plus the same sort across the seven flag/sort combinations the displaylist callers use, on a flat 40k-file fixture (the walk is non-recursive; flat is what a large ROM directory looks like). Pacing: worst main-thread stall 4.17-4.25ms across three runs against a 16ms gate, versus 18.5ms before the resumable sort. Bench: best-of-three wall clock to consumed listing 34.2-35.6ms versus 28.6-29.0ms blocking (ratio 1.18-1.24, gate 1.35) - the honest price of per-entry budget checks and the merge's copy traffic, paid only on the deferred path; a regression to a second enumeration or a quadratic measures 2x and up. The cancel lane supersedes and cancels mid-walk and drains stale completions with LeakSanitizer judging teardown; the threaded lane is the TSan target for the worker-handler to main-callback hand-off. Verified: strict C89 syntax pass on the new TU; full Linux build links; oracle three consecutive plain passes plus ASan+UBSan+LSan and TSan runs with zero reports, CI step literal- replayed locally. * menu_displaylist: drive the file browser through menu_dirwalk filebrowser_parse walked and sorted its directory synchronously on the thread that drives the UI - three dir_list_initialize variants (subsystem extensions, MANUAL_SCAN_DAT/SIDELOAD without compressed files, and the default) plus a dir_list_sort - so opening a large content directory froze the menu for as long as the walk took. The three variants now route through menu_dirwalk_request with the dirs-first sort, and the outcomes map exactly onto the existing control flow: DONE moves the completed heap listing into the local string_list the append loop reads (the loop itself is untouched), with the module's sort making the local dir_list_sort call redundant for that path; the archive branch keeps its own listing and its own sort, unchanged - an in-memory enumeration after the archive read is a different cost class. PENDING appends a single inert "Loading" placeholder and jumps to the parent-directory prepend, so backing out stays possible while the walk continues in the background - off the main thread entirely when Threaded Tasks is on. FAILED falls into the same directory-not-found entry a failed dir_list_initialize produced before. The hand-off back is the explore-task pattern: the registered refresh callback marks ENTRIES_NEED_REFRESH | PREVENT_POPULATE on the main thread, the repopulation re-issues the identical request, and menu_dirwalk hands over the finished sorted listing - one enumeration, one sort, ever. The list's contents change exactly once, at that refresh, so cursor and scroll state are never invalidated mid-fill. Navigating to a different directory supersedes the pending walk through request identity; RARCH_MENU_CTL_DEINIT cancels outright, and the generation guard makes any late completion a no-op. Small and medium directories complete inside the synchronous budgeted pass and never see any of this: same single walk, same single sort, same single tick, no placeholder, no refresh. Verified: full Linux build links; strict declaration-placement gate clean on both touched TUs; dirwalk oracle re-swept after the tag enum addition - plain, ASan+UBSan+LSan and TSan all pass with zero reports. Manual smoke on Ozone/XMB/RGUI (large directory open, placeholder, refresh-to-full-list, navigate-away mid-walk) still owed on real hardware before the series goes upstream. * menu_displaylist: budget the playlist-directory walks The four remaining directory walks in interactive displaylists all enumerate the playlist directory synchronously: the Playlists tab (menu_displaylist_parse_playlists, dir_list_initialize + a sort-ignore-ext), and the three DIR_LIST_COLLECTIONS listings behind add-to-playlist, the playlist manager and the thumbnail downloader (dir_list_new_special + a dirs-first sort each). A playlist directory is rarely huge, but every one of these runs on the thread that drives the UI on every visit, and after the file browser's conversion they were the last unbounded dir_list_new* calls on any interactive menu path except the core list, which is converted separately. Each site now issues the equivalent menu_dirwalk request - the tab keeps its NULL-extension, directories-included walk with the ignore-extension sort; the three COLLECTIONS sites request exactly what DIR_LIST_COLLECTIONS resolved to, "lpl" with no directories and no compressed files, dirs-first sort - under its own consumer tag. DONE hands the module-sorted listing to the untouched consumption loop (the in-loop sort calls are gone with the walks that needed them); PENDING appends the same inert "Loading" placeholder the file browser shows and returns, with the refresh callback rebuilding the list on completion; a failed walk leaves the same early-out or NULL-list path each site already had. All five converted consumers share the single pending slot by design - the menu shows one list at a time, and a request from a different list supersedes whatever was in flight. Verified: full Linux build links; strict declaration-placement gate clean on the TU; anchored replacements verified unique before application. Manual smoke of the four lists rides with the file-browser smoke already owed on real hardware. * menu_displaylist: budget the core-list directory walk menu_displaylist_parse_cores slipped through the first inventory because it builds its listing with dir_list_append rather than dir_list_new - same unbounded synchronous walk of the cores directory plus a dirs-first sort on the thread that drives the UI, behind "Load Core". A cores directory is usually a few hundred entries, so this is the fast path almost always; the conversion buys the bound, not a visible change. The common path now issues the equivalent menu_dirwalk request (extension filter from the displaylist info, directories included, no compressed files, dirs-first sort) under its own tag, with the usual outcome mapping: DONE hands the module-sorted listing to the untouched loop and skips the local sort; PENDING shows the inert placeholder after the parent-directory entry so backing out works; a failed walk lands in the same directory-not-found entry a failed append produced. UWP keeps its one-shot construction and local sort unchanged - it folds the optional core packages into the same listing, a platform special case of a handful of directories that menu_dirwalk's single-directory contract does not cover. Verified: full Linux build links (object and binary mtimes checked against the source); strict declaration-placement gate clean on the TU; anchored replacements verified unique before application. * materialui: document why the icon-list walk stays synchronous The Phase 2 plan flagged materialui's playlist-directory enumeration for conversion pending investigation of how hot the path is. Investigated: it runs at driver init and on RESET_HORIZONTAL_LIST only - not per navigation - over the playlist directory, and both call sites expect the icon list populated on return (RESET_HORIZONTAL_LIST destroys, refreshes and re-uploads textures in one synchronous sequence). Deferring it would also put an init-time walk into menu_dirwalk's single pending slot, where the first displaylist build would supersede it and the icon list would silently never fill, since the driver has no refresh channel to re-enter through. A cold path over a small directory, with a correctness hazard and a driver rework as the price of conversion: it stays blocking, and the comment at the site says so for the next audit that greps dir_list_new_special. * menu/qt: playlist first-touch through the cache Audit of every interactive playlist_init call in the menu and the Qt UI, per the Phase 2 plan. One menu site and four Qt sites parsed a playlist from disk synchronously on the thread that drives the UI; three convert to cached-first, one converts with mutation through the cached instance, and one stays with its reason recorded at the site. The menu site (menu_displaylist_parse_database_entry) reads the database playlist for CRC matching and is reached from that very playlist, so the cached instance is the common case: reuse it when playlist_get_conf_path matches, parse otherwise, free only what was parsed. Qt's PlaylistModel::getPlaylistItems is the first-touch hitch the plan targets - the grid view parsed the whole file on the UI thread at every open. It now borrows the cached playlist when the paths match; every field the loop reads deep-copies into QStrings, so nothing outlives the borrow. The single-entry update and delete-index handlers adopt the cached-mutate template the file already uses for core association (mutate the object the menu reads, write to disk, free only if loaded), replacing a full parse per one-entry edit. The bulk add-files dialog stays on a private parse: its cancel path discards the half-modified playlist by freeing it unwritten, which a borrowed cached instance cannot offer - the comment at the site says so. The remaining first-ever-open of a huge playlist with a cold cache still parses whole; a resumable rjson playlist parse is Phase 2.5 scope, per the plan, and the cached path covers the in-session requirement. The nine menu_displaylist_build_list calls under ui_qt_widgets.cpp were re-checked during this audit: all settings enumerations, no directory or playlist I/O behind any of them, so no Qt displaylist conversion is needed - the CoA's Qt exposure was the playlist parses handled here. Verified: full Linux build links with HAVE_QT=1 (ui_qt_widgets.cpp compiled clean) and with the menu TU under the strict declaration- placement gate; anchored replacements verified unique before application. * menu_displaylist: flag over-budget builds in debug builds The watchdog the Phase 2 plan closes with: with Threaded Tasks off, every displaylist build runs on the thread that drives the UI, so any build past frame scale is a user-visible hitch of exactly the kind the preceding commits converted away. Debug builds now time menu_displaylist_ctl end to end - the driver list_push runs inside a declaration initializer, so the existing entry point became a thin wrapper around the renamed internal - and log a warning with the displaylist type and the measured time when a build on a non-threaded queue exceeds 16 ms. A newly added blocking walk, a cold-cache playlist parse, or a large archive listing identifies itself in the log instead of surviving as an anonymous stutter. Threaded queues are exempt: their heavy lifting is off-thread by construction, and the main-thread callback cost is already bounded by the same window the tasks step under. Release builds compile to the plain internal call. Verified: full Linux build links (object mtime checked against the source); strict declaration-placement gate clean both with and without DEBUG defined. --------- Co-authored-by: LibretroAdmin <[email protected]>
150 lines
5.9 KiB
C
150 lines
5.9 KiB
C
/* RetroArch - A frontend for libretro.
|
|
* Copyright (C) 2011-2021 - Daniel De Matteis
|
|
*
|
|
* RetroArch is free software: you can redistribute it and/or modify it under the terms
|
|
* of the GNU General Public License as published by the Free Software Found-
|
|
* ation, either version 3 of the License, or (at your option) any later version.
|
|
*
|
|
* RetroArch is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
|
|
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
|
|
* PURPOSE. See the GNU General Public License for more details.
|
|
*
|
|
* You should have received a copy of the GNU General Public License along with RetroArch.
|
|
* If not, see <http://www.gnu.org/licenses/>.
|
|
*/
|
|
|
|
#ifndef __MENU_DIRWALK_H
|
|
#define __MENU_DIRWALK_H
|
|
|
|
#include <stdlib.h>
|
|
#include <boolean.h>
|
|
|
|
#include <lists/string_list.h>
|
|
|
|
#include <retro_common_api.h>
|
|
|
|
RETRO_BEGIN_DECLS
|
|
|
|
/* Budgeted, cancellable directory enumeration for interactive list
|
|
* building.
|
|
*
|
|
* The displaylist walk sites all share one shape: enumerate a single
|
|
* directory, sort, append to the menu list - and all of them do it
|
|
* blocking on the thread that drives the UI. This module gives that
|
|
* shape a budget without changing its result:
|
|
*
|
|
* - A request first runs the walk synchronously under one shared
|
|
* I/O window (task_nbio_slice_*). Small and medium directories -
|
|
* the overwhelming case - complete on the spot and the caller
|
|
* proceeds exactly as before: same single enumeration, same
|
|
* single sort, cost merely capped.
|
|
* - A directory too large for the window continues on a task
|
|
* (moving off the UI thread entirely when Threaded Tasks is on).
|
|
* On completion the registered refresh callback fires; the
|
|
* repopulation it triggers re-issues the same request and
|
|
* receives the finished, sorted list. Nothing is enumerated or
|
|
* sorted twice.
|
|
*
|
|
* One pending walk exists at a time (the menu shows one list at a
|
|
* time); a request with a different identity cancels the previous
|
|
* walk. All entry points below are main-thread only - the same
|
|
* thread that builds displaylists and runs task callbacks - which is
|
|
* what makes the module lock-free: the task handler owns its walk
|
|
* state exclusively while running, and hand-off happens in the
|
|
* task's main-thread completion callback. */
|
|
|
|
enum menu_dirwalk_status
|
|
{
|
|
MENU_DIRWALK_FAILED = -1, /* directory unreadable or allocation
|
|
failure: show the existing error
|
|
entry, nothing is pending */
|
|
MENU_DIRWALK_PENDING = 0, /* walk continues in the background:
|
|
show a placeholder; the refresh
|
|
callback fires on completion */
|
|
MENU_DIRWALK_DONE = 1 /* *out_list holds the completed,
|
|
sorted list; ownership transfers
|
|
to the caller (string_list_free) */
|
|
};
|
|
|
|
/* Consumer tags: which displaylist a completed walk belongs to.
|
|
* Part of the request identity and handed to the refresh callback. */
|
|
enum menu_dirwalk_tag
|
|
{
|
|
MENU_DIRWALK_TAG_NONE = 0,
|
|
MENU_DIRWALK_TAG_FILEBROWSER,
|
|
MENU_DIRWALK_TAG_PLAYLISTS,
|
|
MENU_DIRWALK_TAG_ADD_TO_PLAYLIST,
|
|
MENU_DIRWALK_TAG_PLAYLIST_MANAGER,
|
|
MENU_DIRWALK_TAG_PL_THUMBNAILS,
|
|
MENU_DIRWALK_TAG_CORES
|
|
};
|
|
|
|
enum menu_dirwalk_sort
|
|
{
|
|
MENU_DIRWALK_SORT_NONE = 0,
|
|
MENU_DIRWALK_SORT_DIR_FIRST, /* dir_list_sort(list, true) */
|
|
MENU_DIRWALK_SORT_IGNORE_EXT /* dir_list_sort_ignore_ext(list, true) */
|
|
};
|
|
|
|
/**
|
|
* menu_dirwalk_request:
|
|
* @dir : directory to enumerate (must be non-NULL).
|
|
* @ext : extension filter as dir_list_new() takes it,
|
|
* or NULL.
|
|
* @include_dirs : include directories in the listing.
|
|
* @include_hidden : include hidden files and directories.
|
|
* @include_compressed : include compressed files even when not in @ext.
|
|
* @sort_mode : sort applied once, on completion.
|
|
* @tag : caller-chosen consumer tag, passed to the
|
|
* refresh callback so the repopulation path can
|
|
* tell which list finished.
|
|
* @out_list : on MENU_DIRWALK_DONE receives the completed
|
|
* sorted list; untouched otherwise.
|
|
*
|
|
* Identity is the full parameter set (@dir, @ext, flags, @sort_mode,
|
|
* @tag). Re-issuing the identical request while its walk is pending
|
|
* returns MENU_DIRWALK_PENDING without restarting; issuing it after
|
|
* the walk completed returns MENU_DIRWALK_DONE exactly once and
|
|
* consumes the result. A request with any other identity cancels
|
|
* whatever was pending first.
|
|
**/
|
|
enum menu_dirwalk_status menu_dirwalk_request(
|
|
const char *dir, const char *ext,
|
|
bool include_dirs, bool include_hidden, bool include_compressed,
|
|
enum menu_dirwalk_sort sort_mode, unsigned tag,
|
|
struct string_list **out_list);
|
|
|
|
/**
|
|
* menu_dirwalk_cancel:
|
|
*
|
|
* Cancels any pending walk and discards any completed-but-unconsumed
|
|
* result. Called when the user navigates away from the list a walk
|
|
* was feeding, and from menu teardown. Safe when nothing is pending.
|
|
**/
|
|
void menu_dirwalk_cancel(void);
|
|
|
|
/**
|
|
* menu_dirwalk_pending:
|
|
*
|
|
* True while a walk is in flight or a completed result awaits
|
|
* consumption. For the displaylist watchdog and tests.
|
|
**/
|
|
bool menu_dirwalk_pending(void);
|
|
|
|
/**
|
|
* menu_dirwalk_set_refresh_cb:
|
|
*
|
|
* Registers the function fired (on the main thread, from the task
|
|
* completion callback) when a background walk finishes - normally
|
|
* the hook that marks menu entries for refresh. Fires on both
|
|
* success and failure of the walk; the re-issued request tells the
|
|
* caller which. Pass NULL to unregister. This indirection keeps
|
|
* the module free of menu-state dependencies, which is what lets
|
|
* the sample oracle drive it standalone.
|
|
**/
|
|
void menu_dirwalk_set_refresh_cb(void (*cb)(unsigned tag));
|
|
|
|
RETRO_END_DECLS
|
|
|
|
#endif
|