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

@@ -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