From a063328ac8460834ccc7e64de714731f752fdc12 Mon Sep 17 00:00:00 2001
From: Přemysl Janouch
Date: Wed, 28 Sep 2016 04:17:08 +0200
Subject: Fix performance regression
Avoid stepping through the entire dictionary
When looking for the first match with an empty common prefix.
---
src/stardict.c | 6 ++++--
1 file changed, 4 insertions(+), 2 deletions(-)
(limited to 'src')
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--;
--
cgit v1.2.3-70-g09d2