[gedit-code-assistance] Move various tasks to background threads



commit b42014447ed5361cae943bc73428251e4a5cf385
Author: Jesse van den Kieboom <jesse vandenkieboom epfl ch>
Date:   Tue Nov 15 18:51:53 2011 +0100

    Move various tasks to background threads
    
    This makes the plugin overall much more responsive

 src/gcp-c-backend.vala              |    2 +-
 src/gcp-c-document.vala             |   73 +++++++++++++++++++++-------------
 src/gcp-c-semantic-value.vala       |    2 +-
 src/gcp-c-translation-unit.vala     |   63 ++++++++++++++++++-----------
 src/gcp-diagnostic-support.vala     |   24 +++++++----
 src/gcp-document.vala               |   12 +++---
 src/gcp-semantic-value-support.vala |    7 +--
 src/gcp-view.vala                   |   60 ++++++++++++++++++++---------
 8 files changed, 152 insertions(+), 91 deletions(-)
---
diff --git a/src/gcp-c-backend.vala b/src/gcp-c-backend.vala
index 98d7b6a..2785940 100644
--- a/src/gcp-c-backend.vala
+++ b/src/gcp-c-backend.vala
@@ -219,7 +219,7 @@ class Backend : Gcp.Backend
 			Source.remove(d_changedId);
 		}
 
-		d_changedId = Timeout.add(1000, () => {
+		d_changedId = Timeout.add(500, () => {
 			d_changedId = 0;
 
 			reparse();
diff --git a/src/gcp-c-document.vala b/src/gcp-c-document.vala
index ef8358c..95b8fed 100644
--- a/src/gcp-c-document.vala
+++ b/src/gcp-c-document.vala
@@ -65,7 +65,6 @@ class Document : Gcp.Document,
 	private TranslationUnit d_tu;
 	private SymbolBrowser d_symbols;
 	private SourceIndex<Diagnostic> d_diagnostics;
-	private HashMap<CursorWrapper, SemanticValue> d_semanticsMap;
 	private SourceIndex<SemanticValue> d_semantics;
 
 	public Document(Gedit.Document document)
@@ -88,14 +87,20 @@ class Document : Gcp.Document,
 		}
 	}
 
-	public SourceIndex<Diagnostic> diagnostics
+	public void with_diagnostics(DiagnosticSupport.WithDiagnosticsCallback callback)
 	{
-		get { return d_diagnostics; }
+		lock(d_diagnostics)
+		{
+			callback(d_diagnostics);
+		}
 	}
 
-	public SourceIndex<SemanticValue> semantics
+	public void with_semantics(SemanticValueSupport.WithSemanticValueCallback callback)
 	{
-		get { return d_semantics; }
+		lock(d_semantics)
+		{
+			callback(d_semantics);
+		}
 	}
 
 	public TranslationUnit translation_unit
@@ -116,7 +121,7 @@ class Document : Gcp.Document,
 
 	private void update_diagnostics(CX.TranslationUnit tu)
 	{
-		d_diagnostics.clear();
+		SourceIndex<Diagnostic> ndiag = new SourceIndex<Diagnostic>();
 
 		Log.debug("New diagnostics: %d", tu.num_diagnostics);
 
@@ -145,9 +150,9 @@ class Document : Gcp.Document,
 				SourceRange range = Translator.source_range(d.get_range(j));
 
 				if (range.start.file != null &&
-				    range.end.file != null &&
-				    range.start.file.equal(location) &&
-				    range.end.file.equal(location))
+					range.end.file != null &&
+					range.start.file.equal(location) &&
+					range.end.file.equal(location))
 				{
 					clip_location(range.start);
 					clip_location(range.end);
@@ -166,9 +171,9 @@ class Document : Gcp.Document,
 				SourceRange r = Translator.source_range(range);
 
 				if (r.start.file != null &&
-				    r.end.file != null &&
-				    r.start.file.equal(location) &&
-				    r.end.file.equal(location))
+					r.end.file != null &&
+					r.start.file.equal(location) &&
+					r.end.file.equal(location))
 				{
 					clip_location(r.start);
 					clip_location(r.end);
@@ -177,37 +182,41 @@ class Document : Gcp.Document,
 				}
 			}
 
-			d_diagnostics.add(new Diagnostic(severity,
-			                                 loc,
-			                                 ranges.to_array(),
-			                                 fixits,
-			                                 d.spelling.str()));
+			ndiag.add(new Diagnostic(severity,loc,
+			                         ranges.to_array(),
+			                         fixits,
+			                         d.spelling.str()));
 		}
 
-		diagnostics_updated();
+		lock(d_diagnostics)
+		{
+			d_diagnostics = ndiag;
+		}
 	}
 
 	private void update_semantics(CX.TranslationUnit tu)
 	{
-		d_semanticsMap = new HashMap<CursorWrapper, SemanticValue>(CursorWrapper.hash,
-		                                                           (EqualFunc)CursorWrapper.equal);
+		SourceIndex<SemanticValue> sems = new SourceIndex<SemanticValue>();
+
+		HashMap<CursorWrapper, SemanticValue> semmap;
 
-		d_semantics.clear();
+		semmap = new HashMap<CursorWrapper, SemanticValue>(CursorWrapper.hash,
+		                                                  (EqualFunc)CursorWrapper.equal);
 
 		SemanticValue.translate(tu.cursor, location, (cursor, val) => {
-			d_semantics.add(val);
-			d_semanticsMap[new CursorWrapper(cursor)] = val;
+			sems.add(val);
+			semmap[new CursorWrapper(cursor)] = val;
 
 			if (Translator.is_reference(cursor))
 			{
 				CursorWrapper wrapper = new CursorWrapper(cursor.referenced());
 
-				if (!d_semanticsMap.has_key(wrapper))
+				if (!semmap.has_key(wrapper))
 				{
-					d_semanticsMap[wrapper] = new SemanticValue(cursor.referenced());
+					semmap[wrapper] = new SemanticValue(cursor.referenced());
 				}
 
-				SemanticValue rr = d_semanticsMap[wrapper];
+				SemanticValue rr = semmap[wrapper];
 
 				for (int i = 0; i < rr.num_references; ++i)
 				{
@@ -222,15 +231,23 @@ class Document : Gcp.Document,
 			}
 		});
 
-		semantic_values_updated();
+		lock(d_semantics)
+		{
+			d_semantics = sems;
+		}
 	}
 
 	private void on_tu_update()
 	{
 		/* Refill the symbol browser */
-		d_tu.with_translation_unit((tu) => {
+		d_tu.with_translation_unit.begin((tu) => {
 			update_diagnostics(tu);
 			update_semantics(tu);
+		}, (obj, res) => {
+			d_tu.with_translation_unit.end(res);
+
+			diagnostics_updated();
+			semantic_values_updated();
 		});
 	}
 }
diff --git a/src/gcp-c-semantic-value.vala b/src/gcp-c-semantic-value.vala
index ae34ea9..29c3387 100644
--- a/src/gcp-c-semantic-value.vala
+++ b/src/gcp-c-semantic-value.vala
@@ -9,7 +9,7 @@ class SemanticValue : Gcp.SemanticValue
 
 	private class Translator
 	{
-		private CursorMappedFunc d_mapped;
+		private unowned CursorMappedFunc d_mapped;
 		private SemanticValue? d_parent;
 		private SemanticValue? d_current;
 		private File? d_source;
diff --git a/src/gcp-c-translation-unit.vala b/src/gcp-c-translation-unit.vala
index ff4636c..6b8d89d 100644
--- a/src/gcp-c-translation-unit.vala
+++ b/src/gcp-c-translation-unit.vala
@@ -96,44 +96,59 @@ class TranslationUnit
 		}
 	}
 
-	public void with_translation_unit(WithTranslationUnitCallback callback)
+	public async void with_translation_unit(WithTranslationUnitCallback callback)
 	{
-		if (tainted)
-		{
-			MainContext ctx = MainContext.get_thread_default();
-			bool exitit = false;
+		SourceFunc cb = with_translation_unit.callback;
 
-			while (!exitit)
+		ThreadFunc<void *> run = () => {
+			if (tainted)
 			{
-				ctx.iteration(true);
+				MainContext ctx = MainContext.get_thread_default();
+				bool exitit = false;
 
-				d_lock.lock();
+				while (!exitit)
+				{
+					ctx.iteration(true);
 
-				exitit = !d_tainted;
+					d_lock.lock();
 
-				if (exitit)
-				{
-					if (d_tu != null)
+					exitit = !d_tainted;
+
+					if (exitit)
 					{
-						callback(d_tu);
+						if (d_tu != null)
+						{
+							callback(d_tu);
+						}
 					}
+
+					d_lock.unlock();
+				}
+			}
+			else
+			{
+				d_lock.lock();
+
+				if (d_tu != null)
+				{
+					callback(d_tu);
 				}
 
 				d_lock.unlock();
+
 			}
 
-			return;
+			Idle.add((owned)cb);
+			return null;
+		};
+
+		try
+		{
+			Thread.create<void *>(run, false);
+			yield;
 		}
-		else
+		catch
 		{
-			d_lock.lock();
-
-			if (d_tu != null)
-			{
-				callback(d_tu);
-			}
-
-			d_lock.unlock();
 		}
 	}
 
@@ -187,7 +202,7 @@ class TranslationUnit
 
 			d_tainted = false;
 
-			stdout.printf("Took %f seconds to parse...\n", elapsed);
+			Log.debug("Took %f seconds to parse...", elapsed);
 
 			d_lock.unlock();
 
diff --git a/src/gcp-diagnostic-support.vala b/src/gcp-diagnostic-support.vala
index 100dd35..328b8f6 100644
--- a/src/gcp-diagnostic-support.vala
+++ b/src/gcp-diagnostic-support.vala
@@ -28,16 +28,20 @@ interface DiagnosticSupport : Document
 
 	public signal void diagnostics_updated();
 
-	public abstract SourceIndex<Diagnostic> diagnostics { get; }
+	public delegate void WithDiagnosticsCallback(SourceIndex<Diagnostic> diagnostics);
+
+	public abstract void with_diagnostics(WithDiagnosticsCallback callback);
 
 	public Diagnostic[] find_at(SourceLocation location)
 	{
 		ArrayList<Diagnostic> ret = new ArrayList<Diagnostic>();
 
-		foreach (Diagnostic d in diagnostics.find_at(location))
-		{
-			ret.add(d);
-		}
+		with_diagnostics((diagnostics) => {
+			foreach (Diagnostic d in diagnostics.find_at(location))
+			{
+				ret.add(d);
+			}
+		});
 
 		ret.sort_with_data<Diagnostic>((CompareDataFunc)sort_on_severity);
 
@@ -59,10 +63,12 @@ interface DiagnosticSupport : Document
 	{
 		ArrayList<Diagnostic> ret = new ArrayList<Diagnostic>();
 
-		foreach (Diagnostic d in diagnostics.find_at_line(line))
-		{
-			ret.add(d);
-		}
+		with_diagnostics((diagnostics) => {
+			foreach (Diagnostic d in diagnostics.find_at_line(line))
+			{
+				ret.add(d);
+			}
+		});
 
 		ret.sort_with_data<Diagnostic>((CompareDataFunc)sort_on_severity);
 
diff --git a/src/gcp-document.vala b/src/gcp-document.vala
index 50c100f..209111c 100644
--- a/src/gcp-document.vala
+++ b/src/gcp-document.vala
@@ -197,8 +197,6 @@ class Document : GLib.Object
 		TextIter end;
 
 		DiagnosticSupport sup = this as DiagnosticSupport;
-		TextTag? tag = sup.tags[diagnostic.severity];
-		string? category = mark_category_for_severity(diagnostic.severity);
 
 		for (uint i = 0; i < diagnostic.ranges.length; ++i)
 		{
@@ -250,10 +248,12 @@ class Document : GLib.Object
 
 		remove_marks();
 
-		foreach (Diagnostic diag in diagnostic.diagnostics)
-		{
-			mark_diagnostic(diag);
-		}
+		diagnostic.with_diagnostics((diagnostics) => {
+			foreach (Diagnostic diag in diagnostics)
+			{
+				mark_diagnostic(diag);
+			}
+		});
 	}
 
 	private void set_location(File? location)
diff --git a/src/gcp-semantic-value-support.vala b/src/gcp-semantic-value-support.vala
index 1a6c1ed..f715354 100644
--- a/src/gcp-semantic-value-support.vala
+++ b/src/gcp-semantic-value-support.vala
@@ -3,10 +3,9 @@ namespace Gcp
 
 interface SemanticValueSupport : Gcp.Document
 {
-	public abstract SourceIndex<SemanticValue> semantics
-	{
-		get;
-	}
+	public delegate void WithSemanticValueCallback(SourceIndex<SemanticValue> diagnostics);
+
+	public abstract void with_semantics(WithSemanticValueCallback callback);
 
 	public signal void semantic_values_updated();
 }
diff --git a/src/gcp-view.vala b/src/gcp-view.vala
index 87dd7ab..b595998 100644
--- a/src/gcp-view.vala
+++ b/src/gcp-view.vala
@@ -497,11 +497,25 @@ class View : Object
 
 	private void on_semantics_updated(SemanticValueSupport semantics)
 	{
+		Timer timer = new Timer();
+		double elapsed = 0;
+
+		timer.start();
+
 		update_references();
+
+		elapsed = timer.elapsed();
+
+		Log.debug("Semantics update in: %f seconds...", elapsed);
 	}
 
 	private void on_diagnostic_updated(DiagnosticSupport diagnostics)
 	{
+		Timer timer = new Timer();
+		double elapsed = 0;
+
+		timer.start();
+
 		d_scrollbarMarker.clear();
 
 		DiagnosticColors colors;
@@ -524,34 +538,39 @@ class View : Object
 
 		uint mid = d_scrollbarMarker.new_merge_id();
 
-		foreach (Diagnostic d in diagnostics.diagnostics)
-		{
-			Gdk.RGBA color = colors[d.severity];
-			Gdk.RGBA mix = mixed[d.severity];
-
-			foreach (SourceRange range in d.ranges)
+		diagnostics.with_diagnostics((diagnostics) => {
+			foreach (Diagnostic d in diagnostics)
 			{
-				d_scrollbarMarker.add_with_id(mid, range, color);
+				Gdk.RGBA color = colors[d.severity];
+				Gdk.RGBA mix = mixed[d.severity];
 
-				if (range.start.line == range.end.line &&
-				    range.start.column == range.end.column)
+				foreach (SourceRange range in d.ranges)
 				{
-					if (diagnostic_is_at_end(range.start))
+					d_scrollbarMarker.add_with_id(mid, range, color);
+
+					if (range.start.line == range.end.line &&
+						range.start.column == range.end.column)
 					{
-						add_diagnostic_at_end(range.start, mix);
+						if (diagnostic_is_at_end(range.start))
+						{
+							add_diagnostic_at_end(range.start, mix);
+						}
 					}
 				}
-			}
 
-			d_scrollbarMarker.add_with_id(mid, d.location.range, color);
+				d_scrollbarMarker.add_with_id(mid, d.location.range, color);
 
-			if (diagnostic_is_at_end(d.location))
-			{
-				add_diagnostic_at_end(d.location, mix);
+				if (diagnostic_is_at_end(d.location))
+				{
+					add_diagnostic_at_end(d.location, mix);
+				}
 			}
-		}
+		});
 
 		update_diagnostic_message();
+		elapsed = timer.elapsed();
+
+		Log.debug("Diagnostics update in: %f seconds...", elapsed);
 	}
 
 	private void on_notify_buffer()
@@ -807,8 +826,13 @@ class View : Object
 		d_buffer.get_iter_at_mark(out iter, d_buffer.get_insert());
 
 		SourceLocation loc = new SourceLocation.iter(iter);
+		SemanticValue? ret = null;
+
+		sem.with_semantics((semantics) => {
+			ret = semantics.find_inner_at(loc);
+		});
 
-		return sem.semantics.find_inner_at(loc);
+		return ret;
 	}
 
 	private SemanticValue[] references_at_cursor(out SemanticValue? val, out int vidx)



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