From df0f310ddda3d177c93148ba06db4dda1921b84f Mon Sep 17 00:00:00 2001 From: bao-qian Date: Wed, 4 Nov 2015 21:35:04 +0000 Subject: [PATCH] Replace Dispose with Lambda 1. Faster 2. Fix #361 --- Plugins/Wox.Plugin.Program/Programs.cs | 11 +++--- Wox.Core/Plugin/PluginManager.cs | 37 ++++++++++--------- Wox.Infrastructure/Timeit.cs | 50 ++++++++++++++------------ Wox/App.xaml.cs | 4 +-- Wox/ImageLoader/ImageLoader.cs | 18 +++++----- Wox/SettingWindow.xaml.cs | 4 +-- 6 files changed, 67 insertions(+), 57 deletions(-) diff --git a/Plugins/Wox.Plugin.Program/Programs.cs b/Plugins/Wox.Plugin.Program/Programs.cs index 8e2349e435..45b31e0a13 100644 --- a/Plugins/Wox.Plugin.Program/Programs.cs +++ b/Plugins/Wox.Plugin.Program/Programs.cs @@ -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) diff --git a/Wox.Core/Plugin/PluginManager.cs b/Wox.Core/Plugin/PluginManager.cs index 56b8693c67..5413d24657 100644 --- a/Wox.Core/Plugin/PluginManager.cs +++ b/Wox.Core/Plugin/PluginManager.cs @@ -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(); - 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 results = new List(); + 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) { diff --git a/Wox.Infrastructure/Timeit.cs b/Wox.Infrastructure/Timeit.cs index 1e804fc2bf..425821ecc8 100644 --- a/Wox.Infrastructure/Timeit.cs +++ b/Wox.Infrastructure/Timeit.cs @@ -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) + /// + /// This stopwatch will appear only in Debug mode + /// + 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); } + + /// + /// This stopwatch will also appear only in Debug mode + /// + 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; + } + } } diff --git a/Wox/App.xaml.cs b/Wox/App.xaml.cs index 3e5f2b3f18..ae75a53101 100644 --- a/Wox/App.xaml.cs +++ b/Wox/App.xaml.cs @@ -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()); - } + }); } diff --git a/Wox/ImageLoader/ImageLoader.cs b/Wox/ImageLoader/ImageLoader.cs index 18a5a91d09..daf83cabe1 100644 --- a/Wox/ImageLoader/ImageLoader.cs +++ b/Wox/ImageLoader/ImageLoader.cs @@ -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(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 diff --git a/Wox/SettingWindow.xaml.cs b/Wox/SettingWindow.xaml.cs index e78ce1b40a..60b3d10a4c 100644 --- a/Wox/SettingWindow.xaml.cs +++ b/Wox/SettingWindow.xaml.cs @@ -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;