filechooser: schedule icon queries for visible rows, newest first

Each row that became visible started its own icon/thumbnail query right
away, without limit and without cancellation.  Scrolling through a large
folder queued hundreds of queries in GIO's shared thread pool; rows
scrolled past were still served before the rows now on screen.  On a
hung network share every such query blocks a pool thread, so after some
scrolling even listing a different folder had to wait for free threads.

Icon queries now go through a small scheduler:

- at most 16 queries run at a time (enough to serve a screenful in one
  round on a high-latency share, see below), further requests wait;
- waiting requests are served newest first, so rows on screen win over
  rows that were scrolled past;
- after scrolling (coalesced per main loop iteration) waiting requests
  for rows more than one page away are dropped and running ones are
  cancelled; such rows are queried again when they are shown;
- the queue holds at most 256 requests, and requests end when their
  model goes away (weak reference) or the chooser is disposed.

With 20 ms emulated latency per file system call, the visible rows after
fast scrolling get their icons as quickly as before (51 ms); lower limits
of 4 or 8 parallel queries made that 152 ms and 102 ms.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_01F6aiMGWu4tXHcuSSC9DHi2
This commit is contained in:
Daemonratte 2026-10-13 12:48:10 +02:00
commit 3ebf5c3689
2 changed files with 270 additions and 30 deletions

View file

@ -408,6 +408,7 @@ static void browse_files_center_selected_row (GtkFileChooserDefault *impl);
static void view_mode_set (GtkFileChooserDefault *impl, ViewMode view_mode);
static void load_set_model (GtkFileChooserDefault *impl);
static void icon_requests_cancel_all (GtkFileChooserDefault *impl);
static void visible_range_invalidate (GtkFileChooserDefault *impl);
static gboolean visible_range_get (GtkFileChooserDefault *impl,
gint *first,
@ -7129,6 +7130,8 @@ cancel_all_operations (GtkFileChooserDefault *impl)
g_clear_object (&impl->shortcuts_current_folder_cancellable);
}
icon_requests_cancel_all (impl);
search_stop_searching (impl, TRUE);
recent_stop_loading (impl);
}
@ -8137,44 +8140,281 @@ copy_attribute (GFileInfo *to, GFileInfo *from, const char *attribute)
g_file_info_set_attribute (to, attribute, type, value);
}
static void
file_system_model_got_thumbnail (GObject *object, GAsyncResult *res, gpointer data)
/* Icons and thumbnail paths are queried lazily, only for rows that are
* (nearly) visible. At most ICON_REQUESTS_MAX_RUNNING queries run at a
* time and the newest request is served first, so after scrolling the rows
* on screen get their icons before rows that were passed over. Requests
* for rows that left the view are dropped or cancelled, which matters on
* slow shares where each query takes a while. */
#define ICON_REQUESTS_MAX_RUNNING 16
#define ICON_REQUESTS_MAX_QUEUED 256
#define ICON_QUERIED_ATTRIBUTE "filechooser::icon_queried"
typedef struct
{
GtkFileChooserDefault *impl; /* owns a reference */
GtkFileSystemModel *model; /* weak; NULL once the model is disposed */
GFile *file;
GCancellable *cancellable;
} IconRequest;
static void icon_requests_pump (GtkFileChooserDefault *impl);
static void
icon_request_model_gone (gpointer data,
GObject *where_the_model_was)
{
IconRequest *req = data;
req->model = NULL;
g_cancellable_cancel (req->cancellable);
}
static void
icon_request_free (IconRequest *req)
{
if (req->model)
g_object_weak_unref (G_OBJECT (req->model), icon_request_model_gone, req);
g_object_unref (req->cancellable);
g_object_unref (req->file);
g_object_unref (req->impl);
g_slice_free (IconRequest, req);
}
/* Allows the row to be queried again later. */
static void
icon_request_forget (IconRequest *req)
{
GtkFileSystemModel *model = data; /* might be unreffed if operation was cancelled */
GFile *file = G_FILE (object);
GFileInfo *queried, *info;
GtkTreeIter iter;
if (req->model &&
_gtk_file_system_model_get_iter_for_file (req->model, &iter, req->file))
{
GFileInfo *info = _gtk_file_system_model_get_info (req->model, &iter);
if (info)
g_file_info_remove_attribute (info, ICON_QUERIED_ATTRIBUTE);
}
}
static void
icon_request_done (GObject *object,
GAsyncResult *res,
gpointer data)
{
IconRequest *req = data;
GtkFileChooserDefault *impl = req->impl;
GFile *file = G_FILE (object);
GFileInfo *queried;
queried = g_file_query_info_finish (file, res, NULL);
if (queried == NULL)
return;
GDK_THREADS_ENTER ();
/* now we know model is valid */
impl->icon_requests_running = g_list_remove (impl->icon_requests_running, req);
/* file was deleted */
if (!_gtk_file_system_model_get_iter_for_file (model, &iter, file))
if (req->model == NULL)
;
else if (g_cancellable_is_cancelled (req->cancellable) || queried == NULL)
{
g_object_unref (queried);
GDK_THREADS_LEAVE ();
return;
/* Cancelled because the row left the view: query again when it is
* shown. Failed queries are not repeated. */
if (g_cancellable_is_cancelled (req->cancellable))
icon_request_forget (req);
}
else
{
GtkTreeIter iter;
/* The file may have been removed meanwhile. */
if (_gtk_file_system_model_get_iter_for_file (req->model, &iter, file))
{
GFileInfo *info;
info = g_file_info_dup (_gtk_file_system_model_get_info (req->model, &iter));
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
_gtk_file_system_model_update_file (req->model, file, info);
g_object_unref (info);
}
}
info = g_file_info_dup (_gtk_file_system_model_get_info (model, &iter));
if (queried)
g_object_unref (queried);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAIL_PATH);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_THUMBNAILING_FAILED);
copy_attribute (info, queried, G_FILE_ATTRIBUTE_STANDARD_ICON);
icon_request_free (req);
_gtk_file_system_model_update_file (model, file, info);
g_object_unref (info);
g_object_unref (queried);
if (impl->icon_requests)
icon_requests_pump (impl);
GDK_THREADS_LEAVE ();
}
static void
icon_requests_pump (GtkFileChooserDefault *impl)
{
while (g_list_length (impl->icon_requests_running) < ICON_REQUESTS_MAX_RUNNING &&
!g_queue_is_empty (impl->icon_requests))
{
IconRequest *req = g_queue_pop_head (impl->icon_requests);
if (req->model == NULL)
{
icon_request_free (req);
continue;
}
impl->icon_requests_running = g_list_prepend (impl->icon_requests_running, req);
g_file_query_info_async (req->file,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
G_FILE_ATTRIBUTE_STANDARD_ICON,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT,
req->cancellable,
icon_request_done,
req);
}
}
static void
icon_request_add (GtkFileChooserDefault *impl,
GtkFileSystemModel *model,
GFile *file)
{
IconRequest *req;
if (impl->icon_requests == NULL)
impl->icon_requests = g_queue_new ();
req = g_slice_new (IconRequest);
req->impl = g_object_ref (impl);
req->model = model;
req->file = g_object_ref (file);
req->cancellable = g_cancellable_new ();
g_object_weak_ref (G_OBJECT (model), icon_request_model_gone, req);
g_queue_push_head (impl->icon_requests, req);
/* Bound the queue; the oldest requests are the least likely to still
* be on screen. */
while (g_queue_get_length (impl->icon_requests) > ICON_REQUESTS_MAX_QUEUED)
{
IconRequest *old = g_queue_pop_tail (impl->icon_requests);
icon_request_forget (old);
icon_request_free (old);
}
icon_requests_pump (impl);
}
/* Is the row of @req's file within the visible rows plus one page? */
static gboolean
icon_request_still_wanted (GtkFileChooserDefault *impl,
IconRequest *req)
{
GtkTreeIter iter;
GtkTreePath *path;
gint first, last, margin, row;
if (req->model == NULL ||
GTK_TREE_MODEL (req->model) != impl->current_model ||
!_gtk_file_system_model_get_iter_for_file (req->model, &iter, req->file))
return FALSE;
if (!visible_range_get (impl, &first, &last))
return TRUE;
path = gtk_tree_model_get_path (impl->current_model, &iter);
row = gtk_tree_path_get_indices (path)[0];
gtk_tree_path_free (path);
margin = last - first + 1;
return row >= first - margin && row <= last + margin;
}
static gboolean
icon_requests_prune_cb (gpointer data)
{
GtkFileChooserDefault *impl = data;
GList *l, *next;
GDK_THREADS_ENTER ();
impl->icon_requests_prune_id = 0;
if (impl->icon_requests)
{
for (l = impl->icon_requests->head; l; l = next)
{
IconRequest *req = l->data;
next = l->next;
if (!icon_request_still_wanted (impl, req))
{
icon_request_forget (req);
g_queue_delete_link (impl->icon_requests, l);
icon_request_free (req);
}
}
}
for (l = impl->icon_requests_running; l; l = l->next)
{
IconRequest *req = l->data;
if (!icon_request_still_wanted (impl, req))
g_cancellable_cancel (req->cancellable);
}
GDK_THREADS_LEAVE ();
return FALSE;
}
static void
icon_requests_queue_prune (GtkFileChooserDefault *impl)
{
if (impl->icon_requests_prune_id != 0)
return;
if ((impl->icon_requests == NULL || g_queue_is_empty (impl->icon_requests)) &&
impl->icon_requests_running == NULL)
return;
/* Coalesce all scroll events of one main loop iteration. */
impl->icon_requests_prune_id =
gdk_threads_add_idle_full (G_PRIORITY_DEFAULT_IDLE,
icon_requests_prune_cb, impl, NULL);
}
static void
icon_requests_cancel_all (GtkFileChooserDefault *impl)
{
GList *l;
if (impl->icon_requests_prune_id != 0)
{
g_source_remove (impl->icon_requests_prune_id);
impl->icon_requests_prune_id = 0;
}
if (impl->icon_requests)
{
g_queue_free_full (impl->icon_requests, (GDestroyNotify) icon_request_free);
impl->icon_requests = NULL;
}
/* Running requests free themselves when they complete. */
for (l = impl->icon_requests_running; l; l = l->next)
g_cancellable_cancel (((IconRequest *) l->data)->cancellable);
}
static gboolean
get_visible_range (GtkTreePath **start, GtkTreePath **end,
GtkFileChooserDefault *impl)
@ -8232,11 +8472,14 @@ visible_range_get (GtkFileChooserDefault *impl,
return TRUE;
}
static void icon_requests_queue_prune (GtkFileChooserDefault *impl);
static void
visible_range_adjustment_cb (GtkAdjustment *adjustment,
GtkFileChooserDefault *impl)
{
visible_range_invalidate (impl);
icon_requests_queue_prune (impl);
}
static void
@ -8366,18 +8609,10 @@ file_system_model_set (GtkFileSystemModel *model,
if (file_visible)
{
/* #2 start loading standard icon (callback will be handled by #1) */
if (!g_file_info_has_attribute (info, "filechooser::icon_queried"))
if (!g_file_info_has_attribute (info, ICON_QUERIED_ATTRIBUTE))
{
g_file_info_set_attribute_boolean (info, "filechooser::icon_queried", TRUE);
g_file_query_info_async (file,
G_FILE_ATTRIBUTE_THUMBNAIL_PATH ","
G_FILE_ATTRIBUTE_THUMBNAILING_FAILED ","
G_FILE_ATTRIBUTE_STANDARD_ICON,
G_FILE_QUERY_INFO_NONE,
G_PRIORITY_DEFAULT,
_gtk_file_system_model_get_cancellable (model),
file_system_model_got_thumbnail,
model);
g_file_info_set_attribute_boolean (info, ICON_QUERIED_ATTRIBUTE, TRUE);
icon_request_add (impl, model, file);
}
}
return FALSE;

View file

@ -240,6 +240,11 @@ struct _GtkFileChooserDefault
gint visible_range_first;
gint visible_range_last;
guint visible_range_valid : 1;
/* Icon/thumbnail queries for visible rows (see icon_request_add) */
GQueue *icon_requests; /* waiting, newest first */
GList *icon_requests_running;
guint icon_requests_prune_id;
gint icon_view_scale_visible_overscan;
ViewMode view_mode;