Replace Dispose with Lambda

1. Faster
2. Fix #361
This commit is contained in:
bao-qian
2015-11-04 21:35:04 +00:00
parent 57a06aa122
commit df0f310ddd
6 changed files with 67 additions and 57 deletions

View File

@@ -70,15 +70,12 @@ namespace Wox.Plugin.Program
{ {
this.context = context; this.context = context;
this.context.API.ResultItemDropEvent += API_ResultItemDropEvent; this.context.API.ResultItemDropEvent += API_ResultItemDropEvent;
using (new Timeit("Preload programs")) Timeit.StopwatchDebug("Preload programs", () =>
{ {
programs = ProgramCacheStorage.Instance.Programs; programs = ProgramCacheStorage.Instance.Programs;
} });
Debug.WriteLine(string.Format("Preload {0} programs from cache", programs.Count)); Debug.WriteLine($"Preload {programs.Count} programs from cache");
using (new Timeit("Program Index")) Timeit.StopwatchDebug("Program Index", IndexPrograms);
{
IndexPrograms();
}
} }
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e) void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)

View File

@@ -1,5 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Diagnostics;
using System.IO; using System.IO;
using System.Linq; using System.Linq;
using System.Reflection; using System.Reflection;
@@ -91,7 +92,7 @@ namespace Wox.Core.Plugin
PluginPair pair = pluginPair; PluginPair pair = pluginPair;
ThreadPool.QueueUserWorkItem(o => ThreadPool.QueueUserWorkItem(o =>
{ {
using (var time = new Timeit($"Plugin init: {pair.Metadata.Name}")) var milliseconds = Timeit.Stopwatch($"Plugin init: {pair.Metadata.Name}", () =>
{ {
pair.Plugin.Init(new PluginInitContext pair.Plugin.Init(new PluginInitContext
{ {
@@ -99,8 +100,8 @@ namespace Wox.Core.Plugin
Proxy = HttpProxy.Instance, Proxy = HttpProxy.Instance,
API = API API = API
}); });
pair.InitTime = time.Current; });
} pair.InitTime = milliseconds;
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair); InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
}); });
} }
@@ -138,9 +139,13 @@ namespace Wox.Core.Plugin
} }
return new Query return new Query
{ {
Terms = terms, RawQuery = rawQuery, ActionKeyword = actionKeyword, Search = search, Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
// Obsolete value initialisation // Obsolete value initialisation
ActionName = actionKeyword, ActionParameters = actionParameters.ToList() ActionName = actionKeyword,
ActionParameters = actionParameters.ToList()
}; };
} }
@@ -155,10 +160,10 @@ namespace Wox.Core.Plugin
if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue; if (customizedPluginConfig != null && customizedPluginConfig.Disabled) continue;
if (IsInstantQueryPlugin(plugin)) if (IsInstantQueryPlugin(plugin))
{ {
using (new Timeit($"Plugin {plugin.Metadata.Name} is executing instant search")) Timeit.StopwatchDebug($"Instant Query for {plugin.Metadata.Name}", () =>
{ {
QueryForPlugin(plugin, query); QueryForPlugin(plugin, query);
} });
} }
else else
{ {
@@ -174,15 +179,15 @@ namespace Wox.Core.Plugin
{ {
try try
{ {
using (var time = new Timeit($"Query For {pair.Metadata.Name}")) List<Result> results = new List<Result>();
{ var milliseconds = Timeit.Stopwatch($"Query for {pair.Metadata.Name}", () =>
var results = pair.Plugin.Query(query) ?? new List<Result>(); {
results.ForEach(o => { o.PluginID = pair.Metadata.ID; }); results = pair.Plugin.Query(query) ?? results;
var seconds = time.Current; results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
pair.QueryCount += 1; });
pair.AvgQueryTime = pair.QueryCount == 1 ? seconds : (pair.AvgQueryTime + seconds) / 2; pair.QueryCount += 1;
API.PushResults(query, pair.Metadata, results); pair.AvgQueryTime = pair.QueryCount == 1 ? milliseconds : (pair.AvgQueryTime + milliseconds) / 2;
} API.PushResults(query, pair.Metadata, results);
} }
catch (System.Exception e) catch (System.Exception e)
{ {

View File

@@ -4,35 +4,41 @@ using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure namespace Wox.Infrastructure
{ {
public class Timeit : IDisposable public static class Timeit
{ {
private readonly Stopwatch _stopwatch = new Stopwatch(); /// <summary>
private readonly string _name; /// This stopwatch will appear only in Debug mode
/// </summary>
public Timeit(string name) public static void StopwatchDebug(string name, Action action)
{ {
_name = name; #if DEBUG
_stopwatch.Start(); Stopwatch(name, action);
#else
action();
#endif
} }
public long Current [Conditional("DEBUG")]
private static void WriteTimeInfo(string name, long milliseconds)
{ {
get string info = $"{name} : {milliseconds}ms";
{
_stopwatch.Stop();
long seconds = _stopwatch.ElapsedMilliseconds;
_stopwatch.Start();
return seconds;
}
}
public void Dispose()
{
_stopwatch.Stop();
string info = _name + " : " + _stopwatch.ElapsedMilliseconds + "ms";
Debug.WriteLine(info); Debug.WriteLine(info);
Log.Info(info); Log.Info(info);
} }
/// <summary>
/// This stopwatch will also appear only in Debug mode
/// </summary>
public static long Stopwatch(string name, Action action)
{
var stopWatch = new Stopwatch();
stopWatch.Start();
action();
stopWatch.Stop();
var milliseconds = stopWatch.ElapsedMilliseconds;
WriteTimeInfo(name, milliseconds);
return milliseconds;
}
} }
} }

View File

@@ -29,7 +29,7 @@ namespace Wox
protected override void OnStartup(StartupEventArgs e) protected override void OnStartup(StartupEventArgs e)
{ {
using (new Timeit("Startup Time")) Timeit.StopwatchDebug("Startup Time", () =>
{ {
base.OnStartup(e); base.OnStartup(e);
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException; DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
@@ -39,7 +39,7 @@ namespace Wox
Window = new MainWindow(); Window = new MainWindow();
PluginManager.Init(Window); PluginManager.Init(Window);
CommandArgsFactory.Execute(e.Args.ToList()); CommandArgsFactory.Execute(e.Args.ToList());
} });
} }

View File

@@ -48,7 +48,7 @@ namespace Wox.ImageLoader
new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions()); new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
} }
} }
catch{} catch { }
return null; return null;
} }
@@ -57,7 +57,7 @@ namespace Wox.ImageLoader
{ {
//ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy //ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy
var imageList = new Dictionary<string, int>(ImageCacheStroage.Instance.TopUsedImages); var imageList = new Dictionary<string, int>(ImageCacheStroage.Instance.TopUsedImages);
using (new Timeit(string.Format("Preload {0} images", imageList.Count))) Timeit.StopwatchDebug($"Preload {imageList.Count} images", () =>
{ {
foreach (var image in imageList) foreach (var image in imageList)
{ {
@@ -75,20 +75,22 @@ namespace Wox.ImageLoader
} }
} }
} }
} });
} }
public static ImageSource Load(string path, bool addToCache = true) public static ImageSource Load(string path, bool addToCache = true)
{ {
using (new Timeit($"Loading image path: {path}")) if (string.IsNullOrEmpty(path)) return null;
ImageSource img = null;
Timeit.StopwatchDebug($"Loading image path: {path}", () =>
{ {
if (string.IsNullOrEmpty(path)) return null;
if (addToCache) if (addToCache)
{ {
ImageCacheStroage.Instance.Add(path); ImageCacheStroage.Instance.Add(path);
} }
ImageSource img = null;
if (imageCache.ContainsKey(path)) if (imageCache.ContainsKey(path))
{ {
img = imageCache[path]; img = imageCache[path];
@@ -119,8 +121,8 @@ namespace Wox.ImageLoader
} }
} }
} }
return img; });
} return img;
} }
// http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx // http://blogs.msdn.com/b/oldnewthing/archive/2011/01/27/10120844.aspx

View File

@@ -329,10 +329,10 @@ namespace Wox
private void OnThemeTabSelected() private void OnThemeTabSelected()
{ {
using (new Timeit("theme load")) Timeit.StopwatchDebug("theme load", () =>
{ {
var s = Fonts.SystemFontFamilies; var s = Fonts.SystemFontFamilies;
} });
if (themeTabLoaded) return; if (themeTabLoaded) return;