Improve query cancellation and ResultCollection performance (#5370)

* Added cancellation token to downstream functions in query thread

* Remove initial lag in query

* Re add query delay

* Remove debug flag
This commit is contained in:
Divyansh Srivastava
2020-07-31 15:09:23 -07:00
committed by GitHub
parent 4da8aab44f
commit fbc625478b
3 changed files with 62 additions and 178 deletions

View File

@@ -1,138 +1,45 @@
using PowerLauncher.ViewModel; using PowerLauncher.ViewModel;
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized; using System.Collections.Specialized;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
namespace PowerLauncher.Helper namespace PowerLauncher.Helper
{ {
public class ResultCollection : ObservableCollection<ResultViewModel> public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
{ {
/// <summary> public event NotifyCollectionChangedEventHandler CollectionChanged;
/// This private variable holds the flag to
/// turn on and off the collection changed notification.
/// </summary>
private bool suspendCollectionChangeNotification;
/// <summary> private int CompareResultViewModel(ResultViewModel c1, ResultViewModel c2)
/// Initializes a new instance of the FastObservableCollection class.
/// </summary>
public ResultCollection()
: base()
{ {
this.suspendCollectionChangeNotification = false; if (c1.Result.Score > c2.Result.Score)
}
/// <summary>
/// This event is overriden CollectionChanged event of the observable collection.
/// </summary>
//public override event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Raises collection change event.
/// </summary>
public void NotifyChanges()
{
this.ResumeCollectionChangeNotification();
OnCollectionChanged(new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
/// <summary>
/// Resumes collection changed notification.
/// </summary>
public void ResumeCollectionChangeNotification()
{
this.suspendCollectionChangeNotification = false;
}
/// <summary>
/// Suspends collection changed notification.
/// </summary>
public void SuspendCollectionChangeNotification()
{
this.suspendCollectionChangeNotification = true;
}
/// <summary>
/// This method removes all items that match a predicate
/// </summary>
/// <param name="predicate">predicate</param>
public void RemovePredicate(Predicate<ResultViewModel> predicate)
{
CheckReentrancy();
this.SuspendCollectionChangeNotification();
for (int i = Count - 1; i >= 0; i--)
{ {
if (predicate(this[i])) return -1;
{
RemoveAt(i);
}
} }
} else if (c1.Result.Score == c2.Result.Score)
/// <summary>
/// Update the results collection with new results, try to keep identical results
/// </summary>
/// <param name="newItems"></param>
public void Update(List<ResultViewModel> newItems)
{
if (newItems == null)
{ {
throw new ArgumentNullException(nameof(newItems)); return 0;
}
int newCount = newItems.Count;
int oldCount = Items.Count;
int location = newCount > oldCount ? oldCount : newCount;
this.SuspendCollectionChangeNotification();
for (int i = 0; i < location; i++)
{
ResultViewModel oldResult = this[i];
ResultViewModel newResult = newItems[i];
if (!oldResult.Equals(newResult))
{ // result is not the same update it in the current index
this[i] = newResult;
}
else if (oldResult.Result.Score != newResult.Result.Score)
{
this[i].Result.Score = newResult.Result.Score;
}
}
if (newCount >= oldCount)
{
for (int i = oldCount; i < newCount; i++)
{
Add(newItems[i]);
}
} }
else else
{ {
for (int i = oldCount - 1; i >= newCount; i--) return 1;
{
RemoveAt(i);
}
} }
} }
/// <summary> /// <summary>
/// This collection changed event performs thread safe event raising. /// sort the list in descending order of score
/// </summary>
public new void Sort()
{
base.Sort(CompareResultViewModel);
}
/// <summary>
/// Notify change in the List view items
/// </summary> /// </summary>
/// <param name="e">The event argument.</param> /// <param name="e">The event argument.</param>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e) public void NotifyChanges()
{ {
// Recommended is to avoid reentry CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
// in collection changed event while collection
// is getting changed on other thread.
if(!this.suspendCollectionChangeNotification)
{
base.OnCollectionChanged(e);
}
} }
} }
} }

View File

@@ -111,7 +111,7 @@ namespace PowerLauncher.ViewModel
Task.Run(() => Task.Run(() =>
{ {
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query); PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
UpdateResultView(e.Results, pair.Metadata, e.Query); UpdateResultView(e.Results, pair.Metadata, e.Query, _updateToken);
}, _updateToken); }, _updateToken);
}; };
} }
@@ -421,11 +421,11 @@ namespace PowerLauncher.ViewModel
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() || r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() ||
StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet() StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
).ToList(); ).ToList();
History.AddResults(filtered, id); History.AddResults(filtered, id, _updateToken);
} }
else else
{ {
History.AddResults(results, id); History.AddResults(results, id, _updateToken);
} }
} }
@@ -448,25 +448,36 @@ namespace PowerLauncher.ViewModel
Task.Run(() => Task.Run(() =>
{ {
Thread.Sleep(20); Thread.Sleep(20);
RemoveOldQueryResults(query);
var plugins = PluginManager.ValidPluginsForQuery(query); var plugins = PluginManager.ValidPluginsForQuery(query);
try try
{ {
currentCancellationToken.ThrowIfCancellationRequested(); currentCancellationToken.ThrowIfCancellationRequested();
foreach(PluginPair plugin in plugins)
var resultPluginPair = new List<(List<Result>, PluginMetadata)>();
foreach (PluginPair plugin in plugins)
{ {
if (!plugin.Metadata.Disabled && !currentCancellationToken.IsCancellationRequested) if (!plugin.Metadata.Disabled)
{ {
var results = PluginManager.QueryForPlugin(plugin, query); var results = PluginManager.QueryForPlugin(plugin, query);
resultPluginPair.Add((results, plugin.Metadata));
currentCancellationToken.ThrowIfCancellationRequested(); currentCancellationToken.ThrowIfCancellationRequested();
lock (_addResultsLock)
{
UpdateResultView(results, plugin.Metadata, query);
}
} }
} }
lock (_addResultsLock)
{
RemoveOldQueryResults(query);
foreach (var p in resultPluginPair)
{
UpdateResultView(p.Item1, p.Item2, query, currentCancellationToken);
currentCancellationToken.ThrowIfCancellationRequested();
}
currentCancellationToken.ThrowIfCancellationRequested();
Results.Results.Sort();
}
currentCancellationToken.ThrowIfCancellationRequested(); currentCancellationToken.ThrowIfCancellationRequested();
Application.Current.Dispatcher.BeginInvoke(new Action(() => Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{ {
@@ -680,7 +691,7 @@ namespace PowerLauncher.ViewModel
/// <summary> /// <summary>
/// To avoid deadlock, this method should not called from main thread /// To avoid deadlock, this method should not called from main thread
/// </summary> /// </summary>
public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery) public void UpdateResultView(List<Result> list, PluginMetadata metadata, Query originQuery, CancellationToken ct)
{ {
if (list == null) if (list == null)
{ {
@@ -711,7 +722,8 @@ namespace PowerLauncher.ViewModel
if (originQuery.RawQuery == _lastQuery.RawQuery) if (originQuery.RawQuery == _lastQuery.RawQuery)
{ {
Results.AddResults(list, metadata.ID); ct.ThrowIfCancellationRequested();
Results.AddResults(list, metadata.ID, ct);
} }
} }
@@ -724,7 +736,7 @@ namespace PowerLauncher.ViewModel
Title = "hello" Title = "hello"
}; };
list.Add(r); list.Add(r);
Results.AddResults(list, "0"); Results.AddResults(list, "0", _updateToken);
Results.Clear(); Results.Clear();
MainWindowVisibility = System.Windows.Visibility.Collapsed; MainWindowVisibility = System.Windows.Visibility.Collapsed;

View File

@@ -1,7 +1,7 @@
using PowerLauncher.Helper; using PowerLauncher.Helper;
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Threading;
using System.Windows; using System.Windows;
using System.Windows.Controls; using System.Windows.Controls;
using System.Windows.Data; using System.Windows.Data;
@@ -153,12 +153,12 @@ namespace PowerLauncher.ViewModel
public void RemoveResultsExcept(PluginMetadata metadata) public void RemoveResultsExcept(PluginMetadata metadata)
{ {
Results.RemovePredicate(r => r.Result.PluginID != metadata.ID); Results.RemoveAll(r => r.Result.PluginID != metadata.ID);
} }
public void RemoveResultsFor(PluginMetadata metadata) public void RemoveResultsFor(PluginMetadata metadata)
{ {
Results.RemovePredicate(r => r.Result.PluginID == metadata.ID); Results.RemoveAll(r => r.Result.PluginID == metadata.ID);
} }
public void SelectNextTabItem() public void SelectNextTabItem()
@@ -215,57 +215,22 @@ namespace PowerLauncher.ViewModel
/// <summary> /// <summary>
/// Add new results to ResultCollection /// Add new results to ResultCollection
/// </summary> /// </summary>
public void AddResults(List<Result> newRawResults, string resultId) public void AddResults(List<Result> newRawResults, string resultId, CancellationToken ct)
{ {
var newResults = NewResults(newRawResults, resultId); if (newRawResults == null)
Results.Update(newResults); {
} throw new ArgumentNullException(nameof(newRawResults));
}
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId) List<ResultViewModel> newResults = new List<ResultViewModel>(newRawResults.Count);
{ foreach(Result r in newRawResults)
var results = Results.ToList(); {
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList(); newResults.Add(new ResultViewModel(r));
var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList(); ct.ThrowIfCancellationRequested();
}
// Find the same results in A (old results) and B (new newResults)
var sameResults = oldResults Results.RemoveAll(r => r.Result.PluginID == resultId);
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result))) Results.AddRange(newResults);
.ToList();
// remove result of relative complement of B in A
foreach (var result in oldResults.Except(sameResults))
{
results.Remove(result);
}
// update result with B's score and index position
foreach (var sameResult in sameResults)
{
int oldIndex = results.IndexOf(sameResult);
int oldScore = results[oldIndex].Result.Score;
var newResult = newResults[newResults.IndexOf(sameResult)];
int newScore = newResult.Result.Score;
if (newScore != oldScore)
{
var oldResult = results[oldIndex];
oldResult.Result.Score = newScore;
oldResult.Result.OriginQuery = newResult.Result.OriginQuery;
results.RemoveAt(oldIndex);
int newIndex = InsertIndexOf(newScore, results);
results.Insert(newIndex, oldResult);
}
}
// insert result in relative complement of A in B
foreach (var result in newResults.Except(sameResults))
{
int newIndex = InsertIndexOf(result.Result.Score, results);
results.Insert(newIndex, result);
}
return results;
} }
#endregion #endregion