Sorting search results with TreeSet instead of Collection.sort
Benchmarking shows lower sorting latencies with with TreeSet, specially with larger lists: Average Latencies(ms): list size in [0, 115] list size in [40, 115] Collection.sort 39.22 77.57 TreeSet 30.23 52.06 Test: RunSettingsRoboTests Bug: 38197948 Bug: 37312700 Change-Id: I1dd7c77900e8e98e26a8417f2b77582532e1a5f3
This commit is contained in:
@@ -45,6 +45,7 @@ import java.util.HashSet;
|
|||||||
import java.util.List;
|
import java.util.List;
|
||||||
import java.util.Map;
|
import java.util.Map;
|
||||||
import java.util.Set;
|
import java.util.Set;
|
||||||
|
import java.util.TreeSet;
|
||||||
|
|
||||||
public class SearchResultsAdapter extends RecyclerView.Adapter<SearchViewHolder>
|
public class SearchResultsAdapter extends RecyclerView.Adapter<SearchViewHolder>
|
||||||
implements SearchResultsRankerCallback {
|
implements SearchResultsRankerCallback {
|
||||||
@@ -312,22 +313,23 @@ public class SearchResultsAdapter extends RecyclerView.Adapter<SearchViewHolder>
|
|||||||
int appSize = installedAppResults.size();
|
int appSize = installedAppResults.size();
|
||||||
|
|
||||||
final List<SearchResult> asyncRankingResults = new ArrayList<>(dbSize + appSize);
|
final List<SearchResult> asyncRankingResults = new ArrayList<>(dbSize + appSize);
|
||||||
List<SearchResult> databaseResultsSortedByScores = new ArrayList<>(databaseResults);
|
TreeSet<SearchResult> dbResultsSortedByScores = new TreeSet<>(
|
||||||
Collections.sort(databaseResultsSortedByScores, new Comparator<SearchResult>() {
|
new Comparator<SearchResult>() {
|
||||||
@Override
|
@Override
|
||||||
public int compare(SearchResult o1, SearchResult o2) {
|
public int compare(SearchResult o1, SearchResult o2) {
|
||||||
float score1 = getRankingScoreByStableId(o1.stableId);
|
float score1 = getRankingScoreByStableId(o1.stableId);
|
||||||
float score2 = getRankingScoreByStableId(o2.stableId);
|
float score2 = getRankingScoreByStableId(o2.stableId);
|
||||||
if (score1 > score2) {
|
if (score1 > score2) {
|
||||||
return -1;
|
return -1;
|
||||||
} else if (score1 == score2) {
|
} else if (score1 == score2) {
|
||||||
return 0;
|
return 0;
|
||||||
} else {
|
} else {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
asyncRankingResults.addAll(databaseResultsSortedByScores);
|
dbResultsSortedByScores.addAll(databaseResults);
|
||||||
|
asyncRankingResults.addAll(dbResultsSortedByScores);
|
||||||
// App results are not ranked by async ranking and appended at the end of the list.
|
// App results are not ranked by async ranking and appended at the end of the list.
|
||||||
asyncRankingResults.addAll(installedAppResults);
|
asyncRankingResults.addAll(installedAppResults);
|
||||||
return asyncRankingResults;
|
return asyncRankingResults;
|
||||||
|
Reference in New Issue
Block a user