From: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
To: Mark Brown <broonie@opensource.wolfsonmicro.com>
Cc: linux-kernel@vger.kernel.org
Subject: [PATCH] regmap: Optimize the lookup path to use binary search
Date: Mon, 3 Oct 2011 10:50:14 +0100 [thread overview]
Message-ID: <1317635414-31082-1-git-send-email-dp@opensource.wolfsonmicro.com> (raw)
Since there are more lookups than insertions in a typical
scenario, optimize the linear search into a binary search. For
this to work, we need to keep reg_defaults sorted upon
insertions, for now be lazy and use sort().
Signed-off-by: Dimitris Papastamos <dp@opensource.wolfsonmicro.com>
---
drivers/base/regmap/regcache.c | 29 ++++++++++++++++++++++++-----
1 files changed, 24 insertions(+), 5 deletions(-)
diff --git a/drivers/base/regmap/regcache.c b/drivers/base/regmap/regcache.c
index f32c08c..b10e38f 100644
--- a/drivers/base/regmap/regcache.c
+++ b/drivers/base/regmap/regcache.c
@@ -12,6 +12,7 @@
#include <linux/slab.h>
#include <trace/events/regmap.h>
+#include <linux/sort.h>
#include "internal.h"
@@ -356,14 +357,30 @@ unsigned int regcache_get_val(const void *base, unsigned int idx,
int regcache_lookup_reg(struct regmap *map, unsigned int reg)
{
- unsigned int i;
-
- for (i = 0; i < map->num_reg_defaults; i++)
- if (map->reg_defaults[i].reg == reg)
- return i;
+ unsigned int min, max, index;
+
+ min = 0;
+ max = map->num_reg_defaults - 1;
+ do {
+ index = (min + max) / 2;
+ if (map->reg_defaults[index].reg == reg)
+ return index;
+ if (map->reg_defaults[index].reg < reg)
+ min = index + 1;
+ else
+ max = index;
+ } while (min <= max);
return -1;
}
+static int regcache_insert_cmp(const void *a, const void *b)
+{
+ const struct reg_default *_a = a;
+ const struct reg_default *_b = b;
+
+ return _a->reg - _b->reg;
+}
+
int regcache_insert_reg(struct regmap *map, unsigned int reg,
unsigned int val)
{
@@ -378,5 +395,7 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg,
map->num_reg_defaults++;
map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
map->reg_defaults[map->num_reg_defaults - 1].def = val;
+ sort(map->reg_defaults, map->num_reg_defaults,
+ sizeof(struct reg_default), regcache_insert_cmp, NULL);
return 0;
}
--
1.7.6.4
next reply other threads:[~2011-10-03 9:50 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-03 9:50 Dimitris Papastamos [this message]
2011-10-03 12:18 ` [PATCH] regmap: Optimize the lookup path to use binary search Mark Brown
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=1317635414-31082-1-git-send-email-dp@opensource.wolfsonmicro.com \
--to=dp@opensource.wolfsonmicro.com \
--cc=broonie@opensource.wolfsonmicro.com \
--cc=linux-kernel@vger.kernel.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox