aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorPřemysl Janouch <p.janouch@gmail.com>2016-09-28 04:17:08 +0200
committerPřemysl Janouch <p.janouch@gmail.com>2016-09-28 04:17:08 +0200
commita063328ac8460834ccc7e64de714731f752fdc12 (patch)
tree9409c47db4b3f274e5ca7c51eff6f95e999b12b7
parentf453b9dd43b57444bd24cb16b5b0023ea2c4cf9b (diff)
downloadtdv-a063328ac8460834ccc7e64de714731f752fdc12.tar.gz
tdv-a063328ac8460834ccc7e64de714731f752fdc12.tar.xz
tdv-a063328ac8460834ccc7e64de714731f752fdc12.zip
Fix performance regression
Avoid stepping through the entire dictionary When looking for the first match with an empty common prefix.
-rw-r--r--src/stardict.c6
1 files changed, 4 insertions, 2 deletions
diff --git a/src/stardict.c b/src/stardict.c
index 1010ee1..19068d8 100644
--- a/src/stardict.c
+++ b/src/stardict.c
@@ -942,11 +942,13 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
#define PREFIX(i) stardict_longest_common_collation_prefix \
(sd, word, g_array_index (index, StardictIndexEntry, i).name)
+ // We need to take care not to step through the entire dictionary
+ // if not a single character matches, because it can be quite costly
if (sd->priv->collator)
{
GArray *collated = sd->priv->collated_index;
size_t probe, best = PREFIX (g_array_index (collated, guint32, imin));
- while (imin > 0 && (probe =
+ while (best && imin > 0 && (probe =
PREFIX (g_array_index (collated, guint32, imin - 1))) >= best)
{
best = probe;
@@ -958,7 +960,7 @@ stardict_dict_search (StardictDict *sd, const gchar *word, gboolean *success)
// XXX: only looking for _better_ backward matches here, since the
// fallback common prefix searching algorithm doesn't ignore case
size_t probe, best = PREFIX (imin);
- while (imin > 0 && (probe = PREFIX (imin - 1)) > best)
+ while (best && imin > 0 && (probe = PREFIX (imin - 1)) > best)
{
best = probe;
imin--;