Compare commits

...

2 Commits

Author SHA1 Message Date
Přemysl Eric Janouch a5ebc697ad
Do not restart all thumbnailers on new entries
This had the potential to create tons of unnecessary processes
doing the same job.

The change only covers moving or linking, not copying.
2024-01-30 02:34:05 +01:00
Přemysl Eric Janouch 9ca18f52d5
Clean up thumbnailing 2024-01-30 02:16:17 +01:00
1 changed files with 43 additions and 30 deletions

View File

@ -92,7 +92,8 @@ struct _FivBrowser {
Thumbnailer *thumbnailers; ///< Parallelized thumbnailers
size_t thumbnailers_len; ///< Thumbnailers array size
GQueue thumbnailers_queue; ///< Queued up Entry pointers
GQueue thumbnailers_queue_1; ///< Queued up Entry pointers, hi-prio
GQueue thumbnailers_queue_2; ///< Queued up Entry pointers, lo-prio
GdkCursor *pointer; ///< Cached pointer cursor
cairo_pattern_t *glow; ///< CAIRO_FORMAT_A8 mask for corners
@ -742,7 +743,7 @@ thumbnailer_reprocess_entry(FivBrowser *self, GBytes *output, Entry *entry)
if ((flags & FIV_IO_SERIALIZE_LOW_QUALITY)) {
cairo_surface_set_user_data(entry->thumbnail, &fiv_thumbnail_key_lq,
(void *) (intptr_t) 1, NULL);
g_queue_push_tail(&self->thumbnailers_queue, entry);
g_queue_push_tail(&self->thumbnailers_queue_2, entry);
}
entry_set_surface_user_data(entry);
@ -796,13 +797,21 @@ on_thumbnailer_ready(GObject *object, GAsyncResult *res, gpointer user_data)
thumbnailer_next(t);
}
// TODO(p): Try to keep the minions alive (stdout will be a problem).
static gboolean
thumbnailer_next(Thumbnailer *t)
{
// TODO(p): Try to keep the minions alive (stdout will be a problem).
// Already have something to do, not a failure.
if (t->target)
return TRUE;
// They could have been removed via post-reload changes in the model.
FivBrowser *self = t->self;
if (!(t->target = g_queue_pop_head(&self->thumbnailers_queue)))
return FALSE;
do {
if (!(t->target = g_queue_pop_head(&self->thumbnailers_queue_1)) &&
!(t->target = g_queue_pop_head(&self->thumbnailers_queue_2)))
return FALSE;
} while (t->target->removed);
// Case analysis:
// - We haven't found any thumbnail for the entry at all
@ -839,7 +848,8 @@ thumbnailer_next(Thumbnailer *t)
static void
thumbnailers_abort(FivBrowser *self)
{
g_queue_clear(&self->thumbnailers_queue);
g_queue_clear(&self->thumbnailers_queue_1);
g_queue_clear(&self->thumbnailers_queue_2);
for (size_t i = 0; i < self->thumbnailers_len; i++) {
Thumbnailer *t = self->thumbnailers + i;
@ -855,35 +865,35 @@ thumbnailers_abort(FivBrowser *self)
}
static void
thumbnailers_start(FivBrowser *self)
thumbnailers_enqueue(FivBrowser *self, Entry *entry)
{
thumbnailers_abort(self);
if (!self->model)
return;
GQueue lq = G_QUEUE_INIT;
for (guint i = 0; i < self->entries->len; i++) {
Entry *entry = self->entries->pdata[i];
if (entry->removed)
continue;
if (!entry->removed) {
if (entry->icon)
g_queue_push_tail(&self->thumbnailers_queue, entry);
g_queue_push_tail(&self->thumbnailers_queue_1, entry);
else if (cairo_surface_get_user_data(
entry->thumbnail, &fiv_thumbnail_key_lq))
g_queue_push_tail(&lq, entry);
}
while (!g_queue_is_empty(&lq)) {
g_queue_push_tail_link(
&self->thumbnailers_queue, g_queue_pop_head_link(&lq));
g_queue_push_tail(&self->thumbnailers_queue_2, entry);
}
}
static void
thumbnailers_deploy(FivBrowser *self)
{
for (size_t i = 0; i < self->thumbnailers_len; i++) {
if (!thumbnailer_next(self->thumbnailers + i))
break;
}
}
static void
thumbnailers_restart(FivBrowser *self)
{
thumbnailers_abort(self);
for (guint i = 0; i < self->entries->len; i++)
thumbnailers_enqueue(self, self->entries->pdata[i]);
thumbnailers_deploy(self);
}
// --- Boilerplate -------------------------------------------------------------
G_DEFINE_TYPE_EXTENDED(FivBrowser, fiv_browser, GTK_TYPE_WIDGET, 0,
@ -1004,7 +1014,7 @@ set_item_size(FivBrowser *self, FivThumbnailSize size)
g_hash_table_remove_all(self->thumbnail_cache);
reload_thumbnails(self);
thumbnailers_start(self);
thumbnailers_restart(self);
g_object_notify_by_pspec(
G_OBJECT(self), browser_properties[PROP_THUMBNAIL_SIZE]);
@ -1872,7 +1882,8 @@ fiv_browser_init(FivBrowser *self)
g_malloc0_n(self->thumbnailers_len, sizeof *self->thumbnailers);
for (size_t i = 0; i < self->thumbnailers_len; i++)
self->thumbnailers[i].self = self;
g_queue_init(&self->thumbnailers_queue);
g_queue_init(&self->thumbnailers_queue_1);
g_queue_init(&self->thumbnailers_queue_2);
set_item_size(self, FIV_THUMBNAIL_SIZE_NORMAL);
self->show_labels = FALSE;
@ -1917,8 +1928,9 @@ on_model_reloaded(FivIoModel *model, FivBrowser *self)
fiv_browser_select(self, selected_uri);
g_free(selected_uri);
// Restarting thumbnailers is critical, because they keep Entry pointers.
reload_thumbnails(self);
thumbnailers_start(self);
thumbnailers_restart(self);
}
static void
@ -1933,8 +1945,8 @@ on_model_changed(FivIoModel *model, FivIoModelEntry *old, FivIoModelEntry *new,
g_ptr_array_add(self->entries, entry);
reload_one_thumbnail(self, entry);
// TODO(p): Try to add to thumbnailer queue if already started.
thumbnailers_start(self);
thumbnailers_enqueue(self, entry);
thumbnailers_deploy(self);
return;
}
@ -1960,8 +1972,9 @@ on_model_changed(FivIoModel *model, FivIoModelEntry *old, FivIoModelEntry *new,
// so that there's no jumping around. Or, a bit more properly,
// move the thumbnail cache entry to the new URI.
reload_one_thumbnail(self, found);
// TODO(p): Try to add to thumbnailer queue if already started.
thumbnailers_start(self);
// TODO(p): Rather cancel the entry in any running thumbnailer,
// remove it from queues, and _enqueue() + _deploy().
thumbnailers_restart(self);
} else {
found->removed = TRUE;
gtk_widget_queue_draw(GTK_WIDGET(self));