[Kazehakase-devel 1706] Re: URL補完

Zurück zum Archiv-Index

Hidetaka Iwai tyuyu****@sings*****
2004年 10月 24日 (日) 01:12:21 JST


岩井@札幌 です

Hiroyuki Ikezoe <poinc****@ikezo*****> wrote:
Message-ID: <20041****@ikezo*****>

> > > あと、これ異常に遅いんですけどうちだけ?
> > 
> > profile を見ると、egg_regex_match() が大量に呼び出されるので遅くなって
> > いるように見えます。が、ひらがなでマッチさせたときに以上に遅くなったり
> > して、良くわかりません orz
> 
> completion_funcが呼び出される前に正規表現を作っておいて、
> completion_funcの中ではそいつを使いまわす、ってことができればなんとかな
> りそうですね。

あまり賢くないですが、GHashTable を使って egg_regex を cache してみま
した。ちょっとは速くなると思いますが、いかがでしょう?

--
 Hidetaka Iwai
 tyuyu****@sings*****
-------------- next part --------------
? compile
? src/gmon.out
Index: src/actions/kz-history-action.c
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/actions/kz-history-action.c,v
retrieving revision 1.13
diff -u -r1.13 kz-history-action.c
--- src/actions/kz-history-action.c	23 Oct 2004 12:07:30 -0000	1.13
+++ src/actions/kz-history-action.c	23 Oct 2004 16:07:39 -0000
@@ -21,6 +21,7 @@
 #include "kz-history-action.h"
 #include "kazehakase.h"
 #include "eggregex.h"
+#include "migemo.h"
 
 #include <string.h>
 #include <glib/gi18n.h>
@@ -60,8 +61,9 @@
 						      GtkWidget        *proxy);
 
 static KzEntryActionClass *parent_class = NULL;
-
+static GHashTable *completion_regex_cache = NULL;
 static gint history_action_signals[LAST_SIGNAL] = { 0 };
+static char* previous_key = NULL;
 
 KZ_OBJECT_GET_TYPE(kz_history_action, "KzHistoryAction", KzHistoryAction,
 		   kz_history_action_class_init, kz_history_action_init,
@@ -311,11 +313,8 @@
 	char *item = NULL;
 	gboolean ret = FALSE;
 	GtkTreeModel *model;
-#if USE_MIGEMO
-	char *matched = NULL;
-	gboolean use_migemo;
-#endif /* USE_MIGEMO */
-
+	GError *e = NULL;
+	EggRegex *egg_regex = NULL;
 	model = gtk_entry_completion_get_model (completion);
 
 	gtk_tree_model_get (model, iter,
@@ -324,47 +323,77 @@
 
 	if (!item) return ret;
 
-#if USE_MIGEMO
-	KZ_CONF_GET("Global", "use_migemo", use_migemo, BOOL);
-
-	if (use_migemo)
+	if (!completion_regex_cache)
 	{
+		completion_regex_cache = g_hash_table_new(g_direct_hash,
+							  g_direct_equal);
+	}
+	egg_regex = g_hash_table_lookup(completion_regex_cache, key);
 
-#warning FIXME! implement more proper migemo API
+	if(!egg_regex)
+	{
+#if USE_MIGEMO
+		gboolean use_migemo;
+#endif /* USE_MIGEMO */
 
-		matched = migemo_get_matched_text(item, key);
-		if (matched)
+		if(previous_key && (previous_key != key))
 		{
-			g_free(matched);
-			ret = TRUE;
+			g_hash_table_remove(completion_regex_cache, previous_key);
+			previous_key = NULL;
 		}
-	}
-	else
+#if USE_MIGEMO
+		KZ_CONF_GET("Global", "use_migemo", use_migemo, BOOL);
+		if (use_migemo)
+		{
+			gchar* regex = migemo_get_regex(key);
+			if (regex)
+			{
+				egg_regex = egg_regex_new(regex,
+							  EGG_REGEX_MULTILINE |
+							  EGG_REGEX_EXTENDED,
+							  0, &e);
+				if (e)
+				{
+					g_warning("%s", e->message);
+					g_error_free(e);
+					e = NULL;
+				}
+				g_free(regex);
+			}
+		}
+		else
 #endif /* USE_MIGEMO */
-	{
-		EggRegex *egg_regex;
-		GError *e = NULL;
-
-		egg_regex = egg_regex_new(key,
-					  EGG_REGEX_CASELESS |
-					  EGG_REGEX_MULTILINE |
-					  EGG_REGEX_EXTENDED,
-					  0, &e);
-
-		if (e)
 		{
-			g_error_free(e);
-			goto END;
+			egg_regex = egg_regex_new(key,
+						  EGG_REGEX_MULTILINE |
+						  EGG_REGEX_EXTENDED,
+						  0, &e);
+
+			if (e)
+			{
+				g_warning("%s", e->message);
+				g_error_free(e);
+				e = NULL;
 
+			}
 		}
 
-		if (egg_regex_match(egg_regex, item, -1, 0) > 0)
+                /* reference */
+		if(egg_regex)
+		{
+			g_hash_table_insert(completion_regex_cache, (char*)key, egg_regex);
+			previous_key = (char*)key;
+		}
+		else
 		{
-			ret = TRUE;
+			goto END;
 		}
-		egg_regex_free(egg_regex);
 	}
 
+	if (egg_regex_match(egg_regex, item, -1, 0) > 0)
+	{
+		ret = TRUE;
+	}
  END:
 	g_free (item);
 	return ret;
Index: src/utils/migemo.c
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/utils/migemo.c,v
retrieving revision 1.12
diff -u -r1.12 migemo.c
--- src/utils/migemo.c	30 Sep 2004 00:06:08 -0000	1.12
+++ src/utils/migemo.c	23 Oct 2004 16:08:14 -0000
@@ -166,4 +166,69 @@
 	return matched_text;
 }
 
+gchar *
+migemo_get_regex(const gchar *text)
+{
+	GIOChannel *in;
+	GIOChannel *out;
+	gchar *write_buf = NULL, *read_buf = NULL;
+	gchar *euc_text = NULL;
+	gchar *regex;
+	gsize bytes;
+	GError *e = NULL;
+	
+	g_return_val_if_fail(text, NULL);
+
+	if (!migemo && !migemo_init())
+		return NULL;
+
+	euc_text = g_convert(text, strlen(text),
+			     "EUC-JP", "UTF-8",
+			     NULL, NULL, &e);
+	if (e)
+	{
+		g_error_free(e);
+		return NULL;
+	}
+	in  = g_io_channel_unix_new(migemo->input);
+	out = g_io_channel_unix_new(migemo->output);
+	
+	g_io_channel_set_encoding(in, NULL, NULL);
+	g_io_channel_set_encoding(out, NULL, NULL);
+	
+	write_buf = g_strconcat (euc_text, "\n", NULL);
+	g_free(euc_text);
+
+	g_io_channel_write_chars(in,
+				 write_buf,
+				 strlen(write_buf),
+				 &bytes,
+				 NULL);
+	g_io_channel_flush(in, NULL);
+	g_free(write_buf);
+	
+	g_io_channel_read_line(out,
+			       &read_buf,
+			       &bytes,
+			       NULL,
+			       &e);
+	g_io_channel_unref(in);
+	g_io_channel_unref(out);
+
+	if (e)
+	{
+		g_warning("%s", e->message);
+		g_error_free(e);
+		e = NULL;
+	}
+
+	if (!read_buf) return NULL;
+	regex = g_convert(read_buf, bytes,
+			  "UTF-8", "EUC-JP",
+			  NULL, NULL, NULL);
+	g_free(read_buf);
+
+	return regex;
+}
+
 #endif /* USE_MIGEMO */
Index: src/utils/migemo.h
===================================================================
RCS file: /cvsroot/kazehakase/kazehakase/src/utils/migemo.h,v
retrieving revision 1.8
diff -u -r1.8 migemo.h
--- src/utils/migemo.h	30 Jul 2004 07:16:20 -0000	1.8
+++ src/utils/migemo.h	23 Oct 2004 16:08:14 -0000
@@ -49,6 +49,7 @@
 void     migemo_exit                 (void);
 gchar   *migemo_get_matched_text     (const gchar *body,
 				      const gchar *text);
+gchar   *migemo_get_regex            (const gchar *text);
 
 G_END_DECLS
 


Kazehakase-devel メーリングリストの案内
Zurück zum Archiv-Index