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 System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Collections.Specialized;
using System.Diagnostics;
using System.Windows;
using System.Windows.Threading;
namespace PowerLauncher.Helper
{
public class ResultCollection : ObservableCollection<ResultViewModel>
public class ResultCollection : List<ResultViewModel>, INotifyCollectionChanged
{
/// <summary>
/// This private variable holds the flag to
/// turn on and off the collection changed notification.
/// </summary>
private bool suspendCollectionChangeNotification;
public event NotifyCollectionChangedEventHandler CollectionChanged;
/// <summary>
/// Initializes a new instance of the FastObservableCollection class.
/// </summary>
public ResultCollection()
: base()
private int CompareResultViewModel(ResultViewModel c1, ResultViewModel c2)
{
this.suspendCollectionChangeNotification = false;
}
/// <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 (c1.Result.Score > c2.Result.Score)
{
if (predicate(this[i]))
{
RemoveAt(i);
}
return -1;
}
}
/// <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)
else if (c1.Result.Score == c2.Result.Score)
{
throw new ArgumentNullException(nameof(newItems));
}
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]);
}
return 0;
}
else
{
for (int i = oldCount - 1; i >= newCount; i--)
{
RemoveAt(i);
}
return 1;
}
}
/// <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>
/// <param name="e">The event argument.</param>
protected override void OnCollectionChanged(NotifyCollectionChangedEventArgs e)
public void NotifyChanges()
{
// Recommended is to avoid reentry
// in collection changed event while collection
// is getting changed on other thread.
if(!this.suspendCollectionChangeNotification)
{
base.OnCollectionChanged(e);
}
CollectionChanged(this, new NotifyCollectionChangedEventArgs(NotifyCollectionChangedAction.Reset));
}
}
}

View File

@@ -111,7 +111,7 @@ namespace PowerLauncher.ViewModel
Task.Run(() =>
{
PluginManager.UpdatePluginMetadata(e.Results, pair.Metadata, e.Query);
UpdateResultView(e.Results, pair.Metadata, e.Query);
UpdateResultView(e.Results, pair.Metadata, e.Query, _updateToken);
}, _updateToken);
};
}
@@ -421,11 +421,11 @@ namespace PowerLauncher.ViewModel
r => StringMatcher.FuzzySearch(query, r.Title).IsSearchPrecisionScoreMet() ||
StringMatcher.FuzzySearch(query, r.SubTitle).IsSearchPrecisionScoreMet()
).ToList();
History.AddResults(filtered, id);
History.AddResults(filtered, id, _updateToken);
}
else
{
History.AddResults(results, id);
History.AddResults(results, id, _updateToken);
}
}
@@ -448,25 +448,36 @@ namespace PowerLauncher.ViewModel
Task.Run(() =>
{
Thread.Sleep(20);
RemoveOldQueryResults(query);
var plugins = PluginManager.ValidPluginsForQuery(query);
try
{
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);
resultPluginPair.Add((results, plugin.Metadata));
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();
Application.Current.Dispatcher.BeginInvoke(new Action(() =>
{
@@ -680,7 +691,7 @@ namespace PowerLauncher.ViewModel
/// <summary>
/// To avoid deadlock, this method should not called from main thread
/// </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)
{
@@ -711,7 +722,8 @@ namespace PowerLauncher.ViewModel
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"
};
list.Add(r);
Results.AddResults(list, "0");
Results.AddResults(list, "0", _updateToken);
Results.Clear();
MainWindowVisibility = System.Windows.Visibility.Collapsed;

View File

@@ -1,7 +1,7 @@
using PowerLauncher.Helper;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Threading;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
@@ -153,12 +153,12 @@ namespace PowerLauncher.ViewModel
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)
{
Results.RemovePredicate(r => r.Result.PluginID == metadata.ID);
Results.RemoveAll(r => r.Result.PluginID == metadata.ID);
}
public void SelectNextTabItem()
@@ -215,57 +215,22 @@ namespace PowerLauncher.ViewModel
/// <summary>
/// Add new results to ResultCollection
/// </summary>
public void AddResults(List<Result> newRawResults, string resultId)
public void AddResults(List<Result> newRawResults, string resultId, CancellationToken ct)
{
var newResults = NewResults(newRawResults, resultId);
Results.Update(newResults);
}
if (newRawResults == null)
{
throw new ArgumentNullException(nameof(newRawResults));
}
private List<ResultViewModel> NewResults(List<Result> newRawResults, string resultId)
{
var results = Results.ToList();
var newResults = newRawResults.Select(r => new ResultViewModel(r)).ToList();
var oldResults = results.Where(r => r.Result.PluginID == resultId).ToList();
// Find the same results in A (old results) and B (new newResults)
var sameResults = oldResults
.Where(t1 => newResults.Any(x => x.Result.Equals(t1.Result)))
.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;
List<ResultViewModel> newResults = new List<ResultViewModel>(newRawResults.Count);
foreach(Result r in newRawResults)
{
newResults.Add(new ResultViewModel(r));
ct.ThrowIfCancellationRequested();
}
Results.RemoveAll(r => r.Result.PluginID == resultId);
Results.AddRange(newResults);
}
#endregion