ALSA: control: Use xarray for faster lookups
PATCH v2 1/1 ALSA: control: Use xarray for faster lookups
The control elements are managed in a single linked list and we traverse the whole list for matching each numid or ctl id per every inquiry of a control element. This is OK-ish for a small number of elements but obviously it doesn't scale. Especially the matching with the ctl id takes time because it checks each field of the snd_ctl_id element, e.g. the name string is matched with strcmp().
This patch adds the hash tables with Xarray for improving the lookup speed of a control element. There are two xarray tables added to the card; one for numid and another for ctl id. For the numid, we use the numid as the index, while for the ctl id, we calculate a hash key.
The lookup is done via a single xa_load() execution. As long as the given control element is found on the Xarray table, that's fine, we can give back a quick lookup result. The problem is when no entry hits on the table, and for this case, we have a slight optimization. Namely, the driver checks whether we had a collision on Xarray table, and do a fallback search (linear lookup of the full entries) only if a hash key collision happened beforehand.
So, in theory, the inquiry for a non-existing element might take still time even with this patch in a worst case, but this must be pretty rare.
The feature is enabled via CONFIG_SND_CTL_FAST_LOOKUP, which is turned on as default. For simplicity, the option can be turned off only when CONFIG_EXPERT is set ("You are expert? Then you manage 1000 knobs").
---
So this is a revised patch for the previous RFC (see the link above), as there seems real demands for the fast lookup of control elements. A minor improvement from the previous version is the avoidance of fallback in the case of hash non-collision.
include/sound/core.h | 6 ++ sound/core/Kconfig | 9 +++ sound/core/control.c | 180 +++++++++++++++++++++++++++++++++++-------- sound/core/init.c | 4 + 4 files changed, 167 insertions(+), 32 deletions(-)
include/sound/core.h +6 -0
@@ -14,6 +14,7 @@#include <linux/pm.h> /* pm_message_t */ #include <linux/stringify.h> #include <linux/printk.h> +#include <linux/xarray.h> /* number of supported soundcards */ #ifdef CONFIG_SND_DYNAMIC_MINORS
@@ -103,6 +104,11 @@ struct snd_card {size_t user_ctl_alloc_size;// current memory allocation by user controls. struct list_head controls;/* all controls for this card */ struct list_head ctl_files;/* active control files */ +#ifdef CONFIG_SND_CTL_FAST_LOOKUP +struct xarray ctl_numids;/* hash table for numids */ +struct xarray ctl_hash;/* hash table for ctl id matching */ +bool ctl_hash_collision;/* ctl_hash collision seen? */ +#endif struct snd_info_entry *proc_root;/* root for soundcard specific files */ struct proc_dir_entry *proc_root_link;/* number link to real id */
sound/core/Kconfig +9 -0
@@ -154,6 +154,15 @@ config SND_VERBOSE_PRINTK You don't need this unless you're debugging ALSA. +config SND_CTL_FAST_LOOKUP + bool "Fast lookup of control elements" if EXPERT + default y + help + This option enables the faster lookup of control elements. + It will consume more memory because of the additional Xarray. + If you want to choose the memory footprint over the performance + inevitably, turn this off. + config SND_DEBUG bool "Debug" help
sound/core/control.c +148 -32
240 lines not on this page · raw message
sound/core/init.c +4 -0
@@ -310,6 +310,10 @@ static int snd_card_init(struct snd_card *card, struct device *parent,rwlock_init (&card->ctl_files_rwlock);INIT_LIST_HEAD (&card->controls);INIT_LIST_HEAD (&card->ctl_files); +#ifdef CONFIG_SND_CTL_FAST_LOOKUP +xa_init (&card->ctl_numids); +xa_init (&card->ctl_hash); +#endif spin_lock_init (&card->files_lock);INIT_LIST_HEAD (&card->files_list);mutex_init (&card->memory_mutex);
Signature · 1 line
2.35.3