IconTheme: Make icon lookups faster
authorAlexander Larsson <alexl@redhat.com>
Mon, 3 Feb 2020 09:35:45 +0000 (10:35 +0100)
committerAlexander Larsson <alexl@redhat.com>
Mon, 3 Feb 2020 11:49:26 +0000 (12:49 +0100)
commite4170661b9228cb3e09dbc1cd789e15ef44f4ef5
tree53d828f449bc0161fcaf71160da0bbfed8693634
parent0a8d7603ea7448d80e5d1c39bdeb050d7ca926d8
IconTheme: Make icon lookups faster

Traditionally the icon lookup for a theme has been:

lookup (icon_name, size):
  best_directory = NULL
  forearch theme
    foreach directory in theme
       if dir_size_matches (directory, size) && dir_has_icon (directory, icon-name)
         best_directory = chose_best_size_dir (best_directory, directory)

if best_directory
  return icon from best_directory

However, it turns out that there are a lot of subdirectories which have the same
size, as they differ only in the (essentially useless) "context" value. For example
the "16x16/apps" subdirectory is essentially the same as the "16x16/actions" one.

So, instead rathern than keeping all the directories as separate we store the
all the directories with the same size as a single entity (DirSize) and the
icon lookup in that DirSize looks up not only which suffix to use for that icon
but also which subdir it is in.

Additionally we keep a hashtable with all icon names that are
available in the entire theme (i.e. all DirSizes), which allows use
both to store each icon name only once, but also to do a quick
negative lookup and early exit in case we're looking up an icon that
doesn't exist. This is pretty common because we often look up sets of
icons like "image-png-symbolic", "image-png", "image", expecting some
to fail.

This brings down the time of the initial css validation from 20msec to 15msec for
me when running icon-factory.
gtk/gtkicontheme.c