Commit c08604b8 authored by Dimitris Papastamos's avatar Dimitris Papastamos Committed by Mark Brown

regmap: Optimize the lookup path to use binary search

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: default avatarDimitris Papastamos <dp@opensource.wolfsonmicro.com>
Signed-off-by: default avatarMark Brown <broonie@opensource.wolfsonmicro.com>
parent ac77a765
...@@ -12,6 +12,7 @@ ...@@ -12,6 +12,7 @@
#include <linux/slab.h> #include <linux/slab.h>
#include <trace/events/regmap.h> #include <trace/events/regmap.h>
#include <linux/sort.h>
#include "internal.h" #include "internal.h"
...@@ -356,14 +357,30 @@ unsigned int regcache_get_val(const void *base, unsigned int idx, ...@@ -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) int regcache_lookup_reg(struct regmap *map, unsigned int reg)
{ {
unsigned int i; unsigned int min, max, index;
for (i = 0; i < map->num_reg_defaults; i++) min = 0;
if (map->reg_defaults[i].reg == reg) max = map->num_reg_defaults - 1;
return i; 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; 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, int regcache_insert_reg(struct regmap *map, unsigned int reg,
unsigned int val) unsigned int val)
{ {
...@@ -378,5 +395,7 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg, ...@@ -378,5 +395,7 @@ int regcache_insert_reg(struct regmap *map, unsigned int reg,
map->num_reg_defaults++; map->num_reg_defaults++;
map->reg_defaults[map->num_reg_defaults - 1].reg = reg; map->reg_defaults[map->num_reg_defaults - 1].reg = reg;
map->reg_defaults[map->num_reg_defaults - 1].def = val; 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; return 0;
} }
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment