[tracker/rss-enclosures] libtracker-miner: Try to avoid some unneeded heap allocations



commit a81d8befdb85229d180afe1b45b4e877bbc1774a
Author: Aleksander Morgado <aleksander lanedo com>
Date:   Wed Oct 20 18:10:54 2010 +0200

    libtracker-miner: Try to avoid some unneeded heap allocations

 .../tracker-miner-fs-processing-pool.c             |   58 +++++++++---
 .../tracker-miner-fs-processing-pool.h             |   20 +++--
 src/libtracker-miner/tracker-miner-fs.c            |   95 ++++++++++----------
 3 files changed, 103 insertions(+), 70 deletions(-)
---
diff --git a/src/libtracker-miner/tracker-miner-fs-processing-pool.c b/src/libtracker-miner/tracker-miner-fs-processing-pool.c
index 03eb319..0214dfd 100644
--- a/src/libtracker-miner/tracker-miner-fs-processing-pool.c
+++ b/src/libtracker-miner/tracker-miner-fs-processing-pool.c
@@ -132,7 +132,9 @@ struct _TrackerProcessingTask {
 	/* The file being processed */
 	GFile *file;
 	/* The FULL sparql to be updated in the store */
-	gchar *sparql;
+	TrackerSparqlBuilder *sparql;
+	gchar *sparql_string;
+
 	/* The context of the task */
 	gpointer context;
 	/* The context deallocation method, if any */
@@ -201,7 +203,10 @@ tracker_processing_task_free (TrackerProcessingTask *task)
 	    task->context_free_func) {
 		task->context_free_func (task->context);
 	}
-	g_free (task->sparql);
+	if (task->sparql) {
+		g_object_unref (task->sparql);
+	}
+	g_free (task->sparql_string);
 	g_object_unref (task->file);
 	g_slice_free (TrackerProcessingTask, task);
 }
@@ -235,10 +240,31 @@ tracker_processing_task_set_context (TrackerProcessingTask *task,
 
 void
 tracker_processing_task_set_sparql (TrackerProcessingTask *task,
-                                    gchar                 *sparql)
+                                    TrackerSparqlBuilder  *sparql)
+{
+	if (task->sparql) {
+		g_object_unref (task->sparql);
+	}
+	if (task->sparql_string) {
+		g_free (task->sparql_string);
+		task->sparql_string = NULL;
+	}
+	task->sparql = g_object_ref (sparql);
+}
+
+void
+tracker_processing_task_set_sparql_string (TrackerProcessingTask *task,
+                                           gchar                 *sparql_string)
 {
-	g_free (task->sparql);
-	task->sparql = g_strdup (sparql);
+	if (task->sparql) {
+		g_object_unref (task->sparql);
+		task->sparql = NULL;
+	}
+	if (task->sparql_string) {
+		g_free (task->sparql_string);
+	}
+	/* We take ownership of the input string! */
+	task->sparql_string = sparql_string;
 }
 
 
@@ -411,7 +437,7 @@ tracker_processing_pool_push_wait_task (TrackerProcessingPool *pool,
 
 
 	trace ("(Processing Pool) Pushed WAIT task %p for file '%s'",
-               task, task->file_uri);
+	       task, task->file_uri);
 
 	/* Push a new task in WAIT status (so just add it to the tasks queue,
 	 * and don't process it. */
@@ -436,7 +462,7 @@ tracker_processing_pool_sparql_update_cb (GObject      *object,
 	task = user_data;
 
 	trace ("(Processing Pool) Finished update of task %p for file '%s'",
-               task, task->file_uri);
+	       task, task->file_uri);
 
 	/* Before calling user-provided callback, REMOVE the task from the pool;
 	 * as the user-provided callback may actually modify the pool again */
@@ -464,7 +490,7 @@ tracker_processing_pool_sparql_update_array_cb (GObject      *object,
 	sparql_array = user_data;
 
 	trace ("(Processing Pool) Finished array-update of tasks %p",
-               sparql_array);
+	       sparql_array);
 
 	sparql_array_errors = tracker_sparql_connection_update_array_finish (TRACKER_SPARQL_CONNECTION (object),
 	                                                                     result,
@@ -531,11 +557,13 @@ tracker_processing_pool_buffer_flush (TrackerProcessingPool *pool)
 		g_queue_push_head (pool->tasks[TRACKER_PROCESSING_TASK_STATUS_PROCESSING], task);
 
 		/* Add original string, not a duplicate */
-		sparql_array[i] = task->sparql;
+		sparql_array[i] = (task->sparql ?
+		                   (gchar *) tracker_sparql_builder_get_result (task->sparql) :
+		                   task->sparql_string);
 	}
 
 	trace ("(Processing Pool) Flushing array-update of tasks %p with %u items",
-               pool->sparql_buffer, pool->sparql_buffer->len);
+	       pool->sparql_buffer, pool->sparql_buffer->len);
 
 	tracker_sparql_connection_update_array_async (pool->connection,
 	                                              sparql_array,
@@ -569,7 +597,7 @@ tracker_processing_pool_push_ready_task (TrackerProcessingPool
 	GList *previous;
 
 	/* The task MUST have a proper SPARQL here */
-	g_assert (task->sparql != NULL);
+	g_assert (task->sparql != NULL || task->sparql_string != NULL);
 
 	/* First, check if the task was already added as being WAITING */
 	previous = g_queue_find (pool->tasks[TRACKER_PROCESSING_TASK_STATUS_WAIT], task);
@@ -590,7 +618,7 @@ tracker_processing_pool_push_ready_task (TrackerProcessingPool
 	 * flush previous buffer (if any) and then the new update */
 	if (!buffer || pool->limit[TRACKER_PROCESSING_TASK_STATUS_READY] == 1) {
 		trace ("(Processing Pool) Pushed READY/PROCESSING task %p for file '%s'",
-                       task, task->file_uri);
+		       task, task->file_uri);
 
 		/* Flush previous */
 		tracker_processing_pool_buffer_flush (pool);
@@ -601,7 +629,9 @@ tracker_processing_pool_push_ready_task (TrackerProcessingPool
 
 		/* And update the new one */
 		tracker_sparql_connection_update_async (pool->connection,
-		                                        task->sparql,
+		                                        (task->sparql ?
+		                                         tracker_sparql_builder_get_result (task->sparql) :
+		                                         task->sparql_string),
 		                                        G_PRIORITY_DEFAULT,
 		                                        NULL,
 		                                        tracker_processing_pool_sparql_update_cb,
@@ -632,7 +662,7 @@ tracker_processing_pool_push_ready_task (TrackerProcessingPool
 		}
 
 		trace ("(Processing Pool) Pushed READY task %p for file '%s' into array %p",
-                       task, task->file_uri, pool->sparql_buffer);
+		       task, task->file_uri, pool->sparql_buffer);
 
 		/* Add task to array */
 		g_ptr_array_add (pool->sparql_buffer, task);
diff --git a/src/libtracker-miner/tracker-miner-fs-processing-pool.h b/src/libtracker-miner/tracker-miner-fs-processing-pool.h
index 8b3503a..eb4ac9c 100644
--- a/src/libtracker-miner/tracker-miner-fs-processing-pool.h
+++ b/src/libtracker-miner/tracker-miner-fs-processing-pool.h
@@ -36,15 +36,17 @@ typedef void  (* TrackerProcessingPoolTaskFinishedCallback) (TrackerProcessingTa
                                                              const GError          *error);
 
 
-TrackerProcessingTask *tracker_processing_task_new         (GFile          *file);
-void                   tracker_processing_task_free        (TrackerProcessingTask *task);
-GFile                 *tracker_processing_task_get_file    (TrackerProcessingTask *task);
-gpointer               tracker_processing_task_get_context (TrackerProcessingTask *task);
-void                   tracker_processing_task_set_context (TrackerProcessingTask *task,
-                                                            gpointer               context,
-                                                            GFreeFunc              context_free_func);
-void                   tracker_processing_task_set_sparql  (TrackerProcessingTask *task,
-                                                            gchar                 *sparql);
+TrackerProcessingTask *tracker_processing_task_new               (GFile          *file);
+void                   tracker_processing_task_free              (TrackerProcessingTask *task);
+GFile                 *tracker_processing_task_get_file          (TrackerProcessingTask *task);
+gpointer               tracker_processing_task_get_context       (TrackerProcessingTask *task);
+void                   tracker_processing_task_set_context       (TrackerProcessingTask *task,
+                                                                  gpointer               context,
+                                                                  GFreeFunc              context_free_func);
+void                   tracker_processing_task_set_sparql        (TrackerProcessingTask *task,
+                                                                  TrackerSparqlBuilder  *sparql);
+void                   tracker_processing_task_set_sparql_string (TrackerProcessingTask *task,
+                                                                  gchar                 *sparql_string);
 
 
 TrackerProcessingPool *tracker_processing_pool_new                   (TrackerSparqlConnection *connection,
diff --git a/src/libtracker-miner/tracker-miner-fs.c b/src/libtracker-miner/tracker-miner-fs.c
index 5ad8033..5e84319 100644
--- a/src/libtracker-miner/tracker-miner-fs.c
+++ b/src/libtracker-miner/tracker-miner-fs.c
@@ -578,8 +578,8 @@ tracker_miner_fs_init (TrackerMinerFS *object)
 
 	/* Create processing pool */
 	priv->processing_pool = tracker_processing_pool_new (tracker_miner_get_connection (TRACKER_MINER (object)),
-                                                             DEFAULT_WAIT_POOL_LIMIT,
-                                                             DEFAULT_READY_POOL_LIMIT);
+	                                                     DEFAULT_WAIT_POOL_LIMIT,
+	                                                     DEFAULT_READY_POOL_LIMIT);
 
 	/* Set up the crawlers now we have config and hal */
 	priv->crawler = tracker_crawler_new ();
@@ -726,11 +726,11 @@ fs_set_property (GObject      *object,
 		break;
 	case PROP_WAIT_POOL_LIMIT:
 		tracker_processing_pool_set_wait_limit (fs->private->processing_pool,
-                                                        g_value_get_uint (value));
+		                                        g_value_get_uint (value));
 		break;
 	case PROP_READY_POOL_LIMIT:
 		tracker_processing_pool_set_ready_limit (fs->private->processing_pool,
-                                                         g_value_get_uint (value));
+		                                         g_value_get_uint (value));
 		break;
 	case PROP_MTIME_CHECKING:
 		fs->private->mtime_checking = g_value_get_boolean (value);
@@ -1546,8 +1546,6 @@ item_add_or_update_cb (TrackerMinerFS        *fs,
 			item_queue_handlers_set_up (fs);
 		}
 	} else {
-		gchar *full_sparql;
-
 		if (ctxt->urn) {
 			gboolean attribute_update_only;
 
@@ -1559,6 +1557,8 @@ item_add_or_update_cb (TrackerMinerFS        *fs,
 			         attribute_update_only ? " (attributes only)" : "");
 
 			if (!attribute_update_only) {
+				gchar *full_sparql;
+
 				/* update, delete all statements inserted by miner
 				 * except for rdf:type statements as they could cause implicit deletion of user data */
 				full_sparql = g_strdup_printf ("DELETE { GRAPH <%s> { <%s> ?p ?o } } "
@@ -1568,30 +1568,29 @@ item_add_or_update_cb (TrackerMinerFS        *fs,
 				                               TRACKER_MINER_FS_GRAPH_URN,
 				                               ctxt->urn,
 				                               tracker_sparql_builder_get_result (ctxt->builder));
+
+				/* Note that set_sparql_string() takes ownership of the passed string */
+				tracker_processing_task_set_sparql_string (task, full_sparql);
 			} else {
 				/* Do not drop graph if only updating attributes, the SPARQL builder
 				 * will already contain the necessary DELETE statements for the properties
 				 * being updated */
-				full_sparql = g_strdup (tracker_sparql_builder_get_result (ctxt->builder));
+				tracker_processing_task_set_sparql (task, ctxt->builder);
 			}
 		} else {
 			g_debug ("Creating new item '%s'", uri);
-
-			/* new file */
-			full_sparql = g_strdup (tracker_sparql_builder_get_result (ctxt->builder));
+			tracker_processing_task_set_sparql (task, ctxt->builder);
 		}
 
-		tracker_processing_task_set_sparql (task, full_sparql);
 		/* If push_ready_task() returns FALSE, it means the actual db update was delayed,
 		 * and in this case we need to setup queue handlers again */
 		if (!tracker_processing_pool_push_ready_task (fs->private->processing_pool,
-                                                              task,
-                                                              TRUE, /* buffer! */
-                                                              processing_pool_task_finished_cb,
-                                                              fs)) {
+		                                              task,
+		                                              TRUE, /* buffer! */
+		                                              processing_pool_task_finished_cb,
+		                                              fs)) {
 			item_queue_handlers_set_up (fs);
 		}
-		g_free (full_sparql);
 	}
 
 	g_free (uri);
@@ -1658,12 +1657,12 @@ item_add_or_update (TrackerMinerFS *fs,
 	 * the file metadata and such) */
 	task = tracker_processing_task_new (file);
 	tracker_processing_task_set_context (task,
-                                             update_processing_task_context_new (TRACKER_MINER (fs),
-                                                                                 urn,
-                                                                                 parent_urn,
-                                                                                 cancellable,
-                                                                                 sparql),
-                                             (GFreeFunc) update_processing_task_context_free);
+	                                     update_processing_task_context_new (TRACKER_MINER (fs),
+		   urn,
+		   parent_urn,
+		   cancellable,
+		   sparql),
+	                                     (GFreeFunc) update_processing_task_context_free);
 	tracker_processing_pool_push_wait_task (priv->processing_pool, task);
 
 	if (do_process_file (fs, task)) {
@@ -1730,19 +1729,20 @@ item_remove (TrackerMinerFS *fs,
 
 	/* Add new task to processing pool */
 	task = tracker_processing_task_new (file);
-	tracker_processing_task_set_sparql (task, sparql->str);
+	/* Note that set_sparql_string() takes ownership of the passed string */
+	tracker_processing_task_set_sparql_string (task,
+	                                           g_string_free (sparql, FALSE));
+
 	/* If push_ready_task() returns FALSE, it means the actual db update was delayed,
 	 * and in this case we need to setup queue handlers again */
 	if (!tracker_processing_pool_push_ready_task (fs->private->processing_pool,
-                                                      task,
-                                                      FALSE,
-                                                      processing_pool_task_finished_cb,
-                                                      fs)) {
+	                                              task,
+	                                              FALSE,
+	                                              processing_pool_task_finished_cb,
+	                                              fs)) {
 		item_queue_handlers_set_up (fs);
 	}
 
-
-	g_string_free (sparql, TRUE);
 	g_free (uri);
 
 	return FALSE;
@@ -2051,21 +2051,22 @@ item_move (TrackerMinerFS *fs,
 
 	/* Add new task to processing pool */
 	task = tracker_processing_task_new (file);
-	tracker_processing_task_set_sparql (task, sparql->str);
+	/* Note that set_sparql_string() takes ownership of the passed string */
+	tracker_processing_task_set_sparql_string (task,
+	                                           g_string_free (sparql, FALSE));
 	/* If push_ready_task() returns FALSE, it means the actual db update was delayed,
 	 * and in this case we need to setup queue handlers again */
 	if (!tracker_processing_pool_push_ready_task (fs->private->processing_pool,
-                                                      task,
-                                                      FALSE,
-                                                      processing_pool_task_finished_cb,
-                                                      fs)) {
+	                                              task,
+	                                              FALSE,
+	                                              processing_pool_task_finished_cb,
+	                                              fs)) {
 		item_queue_handlers_set_up (fs);
 	}
 
 	g_free (uri);
 	g_free (source_uri);
 	g_object_unref (file_info);
-	g_string_free (sparql, TRUE);
 	g_free (source_iri);
 
 	return TRUE;
@@ -2167,8 +2168,8 @@ should_wait (TrackerMinerFS *fs,
 
 	/* Is the item already being processed? */
 	if (tracker_processing_pool_find_task (fs->private->processing_pool,
-                                               file,
-                                               TRUE)) {
+	                                       file,
+	                                       TRUE)) {
 		/* Yes, a previous event on same item currently
 		 * being processed */
 		return TRUE;
@@ -2178,8 +2179,8 @@ should_wait (TrackerMinerFS *fs,
 	parent = g_file_get_parent (file);
 	if (parent) {
 		if (tracker_processing_pool_find_task (fs->private->processing_pool,
-                                                       parent,
-                                                       TRUE)) {
+		                                       parent,
+		                                       TRUE)) {
 			/* Yes, a previous event on the parent of this item
 			 * currently being processed */
 			g_object_unref (parent);
@@ -3732,8 +3733,8 @@ tracker_miner_fs_directory_remove (TrackerMinerFS *fs,
 
 	/* Cancel all pending tasks on files inside the path given by file */
 	tracker_processing_pool_foreach (fs->private->processing_pool,
-                                         processing_pool_cancel_foreach,
-                                         file);
+	                                 processing_pool_cancel_foreach,
+	                                 file);
 
 	/* Remove all monitors */
 	tracker_monitor_remove_recursively (fs->private->monitor, file);
@@ -3925,8 +3926,8 @@ tracker_miner_fs_file_notify (TrackerMinerFS *fs,
 	fs->private->total_files_notified++;
 
 	task = tracker_processing_pool_find_task (fs->private->processing_pool,
-                                                  file,
-                                                  FALSE);
+	                                          file,
+	                                          FALSE);
 
 	if (!task) {
 		gchar *uri;
@@ -4030,8 +4031,8 @@ tracker_miner_fs_get_urn (TrackerMinerFS *fs,
 
 	/* Check if found in currently processed data */
 	task = tracker_processing_pool_find_task (fs->private->processing_pool,
-                                                  file,
-                                                  FALSE);
+	                                          file,
+	                                          FALSE);
 
 	if (!task) {
 		gchar *uri;
@@ -4120,8 +4121,8 @@ tracker_miner_fs_get_parent_urn (TrackerMinerFS *fs,
 
 	/* Check if found in currently processed data */
 	task = tracker_processing_pool_find_task (fs->private->processing_pool,
-                                                  file,
-                                                  FALSE);
+	                                          file,
+	                                          FALSE);
 
 	if (!task) {
 		gchar *uri;



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]