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.API.ResultItemDropEvent += API_ResultItemDropEvent;
using (new Timeit("Preload programs"))
Timeit.StopwatchDebug("Preload programs", () =>
{
programs = ProgramCacheStorage.Instance.Programs;
}
Debug.WriteLine(string.Format("Preload {0} programs from cache", programs.Count));
using (new Timeit("Program Index"))
{
IndexPrograms();
}
});
Debug.WriteLine($"Preload {programs.Count} programs from cache");
Timeit.StopwatchDebug("Program Index", IndexPrograms);
}
void API_ResultItemDropEvent(Result result, IDataObject dropObject, DragEventArgs e)

View file

@ -1,5 +1,6 @@
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.Linq;
using System.Reflection;
@ -91,7 +92,7 @@ namespace Wox.Core.Plugin
PluginPair pair = pluginPair;
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
{
@ -99,8 +100,8 @@ namespace Wox.Core.Plugin
Proxy = HttpProxy.Instance,
API = API
});
pair.InitTime = time.Current;
}
});
pair.InitTime = milliseconds;
InternationalizationManager.Instance.UpdatePluginMetadataTranslations(pair);
});
}
@ -138,9 +139,13 @@ namespace Wox.Core.Plugin
}
return new Query
{
Terms = terms, RawQuery = rawQuery, ActionKeyword = actionKeyword, Search = search,
Terms = terms,
RawQuery = rawQuery,
ActionKeyword = actionKeyword,
Search = search,
// 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 (IsInstantQueryPlugin(plugin))
{
using (new Timeit($"Plugin {plugin.Metadata.Name} is executing instant search"))
Timeit.StopwatchDebug($"Instant Query for {plugin.Metadata.Name}", () =>
{
QueryForPlugin(plugin, query);
}
});
}
else
{
@ -174,15 +179,15 @@ namespace Wox.Core.Plugin
{
try
{
using (var time = new Timeit($"Query For {pair.Metadata.Name}"))
{
var results = pair.Plugin.Query(query) ?? new List<Result>();
results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
var seconds = time.Current;
pair.QueryCount += 1;
pair.AvgQueryTime = pair.QueryCount == 1 ? seconds : (pair.AvgQueryTime + seconds) / 2;
API.PushResults(query, pair.Metadata, results);
}
List<Result> results = new List<Result>();
var milliseconds = Timeit.Stopwatch($"Query for {pair.Metadata.Name}", () =>
{
results = pair.Plugin.Query(query) ?? results;
results.ForEach(o => { o.PluginID = pair.Metadata.ID; });
});
pair.QueryCount += 1;
pair.AvgQueryTime = pair.QueryCount == 1 ? milliseconds : (pair.AvgQueryTime + milliseconds) / 2;
API.PushResults(query, pair.Metadata, results);
}
catch (System.Exception e)
{

View file

@ -4,35 +4,41 @@ using Wox.Infrastructure.Logger;
namespace Wox.Infrastructure
{
public class Timeit : IDisposable
public static class Timeit
{
private readonly Stopwatch _stopwatch = new Stopwatch();
private readonly string _name;
public Timeit(string name)
/// <summary>
/// This stopwatch will appear only in Debug mode
/// </summary>
public static void StopwatchDebug(string name, Action action)
{
_name = name;
_stopwatch.Start();
#if DEBUG
Stopwatch(name, action);
#else
action();
#endif
}
public long Current
[Conditional("DEBUG")]
private static void WriteTimeInfo(string name, long milliseconds)
{
get
{
_stopwatch.Stop();
long seconds = _stopwatch.ElapsedMilliseconds;
_stopwatch.Start();
return seconds;
}
}
public void Dispose()
{
_stopwatch.Stop();
string info = _name + " : " + _stopwatch.ElapsedMilliseconds + "ms";
string info = $"{name} : {milliseconds}ms";
Debug.WriteLine(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)
{
using (new Timeit("Startup Time"))
Timeit.StopwatchDebug("Startup Time", () =>
{
base.OnStartup(e);
DispatcherUnhandledException += ErrorReporting.DispatcherUnhandledException;
@ -39,7 +39,7 @@ namespace Wox
Window = new MainWindow();
PluginManager.Init(Window);
CommandArgsFactory.Execute(e.Args.ToList());
}
});
}

View file

@ -48,7 +48,7 @@ namespace Wox.ImageLoader
new Int32Rect(0, 0, icon.Width, icon.Height), BitmapSizeOptions.FromEmptyOptions());
}
}
catch{}
catch { }
return null;
}
@ -57,7 +57,7 @@ namespace Wox.ImageLoader
{
//ImageCacheStroage.Instance.TopUsedImages can be changed during foreach, so we need to make a copy
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)
{
@ -75,20 +75,22 @@ namespace Wox.ImageLoader
}
}
}
}
});
}
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)
{
ImageCacheStroage.Instance.Add(path);
}
ImageSource img = null;
if (imageCache.ContainsKey(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

View file

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