[kazehakase-svn] [3406] 2008-02-26 Shunichi Fuji <palgl****@gmail*****>

Back to archive index

svnno****@sourc***** svnno****@sourc*****
Tue Feb 26 22:05:09 JST 2008


Revision: 3406
          http://svn.sourceforge.jp/cgi-bin/viewcvs.cgi?root=kazehakase&view=rev&rev=3406
Author:   pal_gene
Date:     2008-02-26 22:05:08 +0900 (Tue, 26 Feb 2008)

Log Message:
-----------
 2008-02-26  Shunichi Fuji  <palgl****@gmail*****>
 	* modules/search/kz-hyper-estraier-search.c:
	-don't leak at some points
	-make to avoid memory realocation
	-general optimization

Modified Paths:
--------------
    kazehakase/trunk/ChangeLog
    kazehakase/trunk/module/search/kz-hyper-estraier-search.c

Modified: kazehakase/trunk/ChangeLog
===================================================================
--- kazehakase/trunk/ChangeLog	2008-02-25 21:14:49 UTC (rev 3405)
+++ kazehakase/trunk/ChangeLog	2008-02-26 13:05:08 UTC (rev 3406)
@@ -1,5 +1,11 @@
 2008-02-26  Shunichi Fuji  <palgl****@gmail*****>
 	* modules/search/kz-hyper-estraier-search.c:
+	-don't leak at some points
+	-make to avoid memory realocation
+	-general optimization
+
+2008-02-26  Shunichi Fuji  <palgl****@gmail*****>
+	* modules/search/kz-hyper-estraier-search.c:
 	Don't call g_free before using variable.
 
 2008-02-26  Shunichi Fuji  <palgl****@gmail*****>

Modified: kazehakase/trunk/module/search/kz-hyper-estraier-search.c
===================================================================
--- kazehakase/trunk/module/search/kz-hyper-estraier-search.c	2008-02-25 21:14:49 UTC (rev 3405)
+++ kazehakase/trunk/module/search/kz-hyper-estraier-search.c	2008-02-26 13:05:08 UTC (rev 3406)
@@ -268,7 +268,7 @@
 	int *results, n_results, i;
 	gchar *except_word, *tmp;
 	gchar **texts;
-	GString *html, *phrase;
+	GString *html, *phrase, *desc_str;
 	gint num_summary = 128, max_results = 20, half_of_summary;
 	KzHyperEstraierSearch *he_search;
 
@@ -280,16 +280,16 @@
 
 	cond = est_cond_new();
 	texts = g_strsplit(text, " ", -1);
-	tmp = g_strjoinv(" " ESTOPISECT " ", texts);
 
 	highlights = cblistopen();
 	for (i = 0; texts[i]; i++)
 	{
 		cblistpush(highlights, texts[i], -1);
 	}
-
+	tmp = g_strjoinv(" " ESTOPISECT " ", texts);
 	g_strfreev(texts);
 	phrase = g_string_new(tmp);
+	g_free(tmp);
 
 	except_word = KZ_CONF_GET_STR("History", "except_keyword");
 	if (except_word && *except_word)
@@ -314,7 +314,8 @@
 
 	results = est_mtdb_search(he_search->db, cond, &n_results, NULL);
 
-	html = g_string_sized_new(0);
+	desc_str = g_string_sized_new(num_summary*2 - 1);		/* almost typical in num_summary*2 */
+	html = g_string_sized_new((num_summary*2+512)*max_results);	/* typical in num_summary*2*max_results */
 
 	g_string_append(html, DTD"\n");
 	g_string_append(html, "<html>\n");
@@ -326,56 +327,55 @@
 	for (i = 0; i < MIN(n_results, max_results); i++)
 	{
 		ESTDOC *doc;
-		const gchar *title, *uri, *date;
-		gchar *cache_str = NULL;
-		const gchar *cache_link, *thumb_uri, *thumb_filename;
+		const gchar *title, *date, *cache_link;
+		gchar *cache_str, *thumb_filename, *uri, *thumb_uri;
 		gchar *desc, **desc_highlight;
 		guint desc_len, desc_i;
-		GString *desc_str;
 
 		doc = est_mtdb_get_doc(he_search->db, results[i], 0);
 		if (!doc)
 			continue;
 
 		cache_link = est_doc_attr(doc, ESTDATTRURI);
+		if (g_file_test(cache_link+strlen("file://"), G_FILE_TEST_EXISTS))
+		{
+			gchar *escaped_cache_link;
+			escaped_cache_link = kz_uri_encode_last_component(cache_link);
+			cache_str = g_strdup_printf("<span class=\"cache\"><a href=\"%s\">cache</a>"
+			                            "</span>\n", escaped_cache_link);
+			g_free(escaped_cache_link);
+		}
+		else
+		{
+			cache_str = g_strdup("");
+		}		
 		uri = create_uri_from_filename(cache_link +
 					       strlen("file://") +
 					       he_search->cache_path_len +
 					       strlen(G_DIR_SEPARATOR_S));
+
 		thumb_filename =
 			egg_pixbuf_get_thumb_filename(uri,
 						      EGG_PIXBUF_THUMB_LARGE);
 		thumb_uri = g_strdup_printf("history-search:?image=%s",
 					    thumb_filename);
+		g_free(thumb_filename);
+
 		title = est_doc_attr(doc, ESTDATTRTITLE);
-		if(title == NULL || strlen(title) == 0) title = "[NO TITLE]";
+		if(!title || !*title) title = "[NO TITLE]";
 		date = est_doc_attr(doc, ESTDATTRMDATE);
 
 		desc = est_doc_make_snippet(doc, highlights, num_summary,
 					    half_of_summary, half_of_summary);
-
-		if (g_file_test(cache_link+strlen("file://"), G_FILE_TEST_EXISTS))
-		{
-			gchar *escaped_cache_link;
-			escaped_cache_link =
-				kz_uri_encode_last_component(cache_link);
-			cache_str = g_strdup_printf("<span class=\"cache\"><a href=\"%s\">cache</a></span>\n", escaped_cache_link);
-			g_free(escaped_cache_link);
-		}
-		else
-		{
-			cache_str = g_strdup("");
-		}
-		
 		/* make highlight keyword */
 		desc_highlight = g_strsplit_set(desc, "\n", 0);
-		desc_str = g_string_sized_new(strlen(desc));
-		g_free(desc);
+		free(desc);		/* don't g_free because born from estraier func */
 		desc_len = g_strv_length(desc_highlight);
+		desc_str->len = 0;	/* reset contain length */
 		for(desc_i = 0; desc_i < desc_len; desc_i++) {
 		    char *found = strchr(desc_highlight[desc_i], '\t');
 		    if (found) {
-			*found = '\0'; 
+			*found = '\0';
 			g_string_append(desc_str, "<b>");
 			g_string_append(desc_str, desc_highlight[desc_i]);
 			g_string_append(desc_str, "</b>");
@@ -384,22 +384,23 @@
 		    }
 		}
 		g_strfreev(desc_highlight);
-		desc = g_string_free(desc_str, FALSE);
 		
 		g_string_append_printf(html,
 				       CONTENT,
 				       uri,
 				       title,
 				       thumb_uri, /* thumbnail */
-				       desc,
+				       desc_str->str,
 				       uri,
 				       cache_str,
 				       date);
+		g_free(uri);
 		g_free(cache_str);
-		g_free(desc);
+		g_free(thumb_uri);
 		est_doc_delete(doc);
 	}
 	free(results);
+	g_string_free(desc_str, TRUE);
 
 	cblistclose(highlights);
 	est_cond_delete(cond);




More information about the Kazehakase-cvs mailing list
Back to archive index