1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-11 06:37:19 +08:00

Arrays instead of enumerables

This commit is contained in:
Dan Balasescu 2025-02-25 22:55:55 +09:00
parent 8a27b6689e
commit 3b5bf391da
No known key found for this signature in database
7 changed files with 24 additions and 28 deletions

View File

@ -4,7 +4,6 @@
#nullable disable #nullable disable
using System; using System;
using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using NUnit.Framework; using NUnit.Framework;
@ -415,19 +414,19 @@ namespace osu.Game.Tests.Visual.Ranking
RetryOverlay = InternalChildren.OfType<HotkeyRetryOverlay>().SingleOrDefault(); RetryOverlay = InternalChildren.OfType<HotkeyRetryOverlay>().SingleOrDefault();
} }
protected override Task<IEnumerable<ScoreInfo>> FetchScores() protected override Task<ScoreInfo[]> FetchScores()
{ {
var scores = new List<ScoreInfo>(); var scores = new ScoreInfo[20];
for (int i = 0; i < 20; i++) for (int i = 0; i < scores.Length; i++)
{ {
var score = TestResources.CreateTestScoreInfo(); var score = TestResources.CreateTestScoreInfo();
score.TotalScore += 10 - i; score.TotalScore += 10 - i;
score.HasOnlineReplay = true; score.HasOnlineReplay = true;
scores.Add(score); scores[i] = score;
} }
return Task.FromResult<IEnumerable<ScoreInfo>>(scores); return Task.FromResult(scores);
} }
} }
@ -443,19 +442,19 @@ namespace osu.Game.Tests.Visual.Ranking
this.fetchWaitTask = fetchWaitTask ?? Task.CompletedTask; this.fetchWaitTask = fetchWaitTask ?? Task.CompletedTask;
} }
protected override Task<IEnumerable<ScoreInfo>> FetchScores() protected override Task<ScoreInfo[]> FetchScores()
{ {
return Task.Run<IEnumerable<ScoreInfo>>(async () => return Task.Run(async () =>
{ {
await fetchWaitTask; await fetchWaitTask;
var scores = new List<ScoreInfo>(); var scores = new ScoreInfo[20];
for (int i = 0; i < 20; i++) for (int i = 0; i < scores.Length; i++)
{ {
var score = TestResources.CreateTestScoreInfo(); var score = TestResources.CreateTestScoreInfo();
score.TotalScore += 10 - i; score.TotalScore += 10 - i;
scores.Add(score); scores[i] = score;
} }
Schedule(() => FetchCompleted = true); Schedule(() => FetchCompleted = true);

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Threading.Tasks; using System.Threading.Tasks;
using osu.Game.Scoring; using osu.Game.Scoring;
using osu.Game.Screens.Play; using osu.Game.Screens.Play;
@ -22,8 +21,8 @@ namespace osu.Game.Screens.OnlinePlay.Multiplayer.Spectate
Scheduler.AddDelayed(() => StatisticsPanel.ToggleVisibility(), 1000); Scheduler.AddDelayed(() => StatisticsPanel.ToggleVisibility(), 1000);
} }
protected override Task<IEnumerable<ScoreInfo>> FetchScores() => Task.FromResult<IEnumerable<ScoreInfo>>([]); protected override Task<ScoreInfo[]> FetchScores() => Task.FromResult<ScoreInfo[]>([]);
protected override Task<IEnumerable<ScoreInfo>> FetchNextPage(int direction) => Task.FromResult<IEnumerable<ScoreInfo>>([]); protected override Task<ScoreInfo[]> FetchNextPage(int direction) => Task.FromResult<ScoreInfo[]>([]);
} }
} }

View File

@ -83,7 +83,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
protected abstract APIRequest<MultiplayerScore> CreateScoreRequest(); protected abstract APIRequest<MultiplayerScore> CreateScoreRequest();
protected override async Task<IEnumerable<ScoreInfo>> FetchScores() protected override async Task<ScoreInfo[]> FetchScores()
{ {
// This performs two requests: // This performs two requests:
// 1. A request to show the relevant score (and scores around). // 1. A request to show the relevant score (and scores around).
@ -141,7 +141,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
} }
} }
protected override async Task<IEnumerable<ScoreInfo>> FetchNextPage(int direction) protected override async Task<ScoreInfo[]> FetchNextPage(int direction)
{ {
Debug.Assert(direction == 1 || direction == -1); Debug.Assert(direction == 1 || direction == -1);
@ -165,7 +165,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
/// </summary> /// </summary>
/// <remarks>Does not queue the request.</remarks> /// <remarks>Does not queue the request.</remarks>
/// <param name="pivot">An optional score pivot to retrieve scores around. Can be null to retrieve scores from the highest score.</param> /// <param name="pivot">An optional score pivot to retrieve scores around. Can be null to retrieve scores from the highest score.</param>
private async Task<IEnumerable<ScoreInfo>> fetchScoresAround(MultiplayerScores? pivot = null) private async Task<ScoreInfo[]> fetchScoresAround(MultiplayerScores? pivot = null)
{ {
var requestTaskSource = new TaskCompletionSource<IndexedMultiplayerScores>(); var requestTaskSource = new TaskCompletionSource<IndexedMultiplayerScores>();
var indexReq = pivot != null var indexReq = pivot != null

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
@ -30,7 +29,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
protected override APIRequest<MultiplayerScore> CreateScoreRequest() => new ShowPlaylistScoreRequest(RoomId, PlaylistItem.ID, scoreId); protected override APIRequest<MultiplayerScore> CreateScoreRequest() => new ShowPlaylistScoreRequest(RoomId, PlaylistItem.ID, scoreId);
protected override void OnScoresAdded(IEnumerable<ScoreInfo> scores) protected override void OnScoresAdded(ScoreInfo[] scores)
{ {
base.OnScoresAdded(scores); base.OnScoresAdded(scores);
SelectedScore.Value ??= scores.SingleOrDefault(s => s.OnlineID == scoreId); SelectedScore.Value ??= scores.SingleOrDefault(s => s.OnlineID == scoreId);

View File

@ -1,7 +1,6 @@
// Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence. // Copyright (c) ppy Pty Ltd <contact@ppy.sh>. Licensed under the MIT Licence.
// See the LICENCE file in the repository root for full licence text. // See the LICENCE file in the repository root for full licence text.
using System.Collections.Generic;
using System.Linq; using System.Linq;
using osu.Game.Online.API; using osu.Game.Online.API;
using osu.Game.Online.Rooms; using osu.Game.Online.Rooms;
@ -24,7 +23,7 @@ namespace osu.Game.Screens.OnlinePlay.Playlists
protected override APIRequest<MultiplayerScore> CreateScoreRequest() => new ShowPlaylistUserScoreRequest(RoomId, PlaylistItem.ID, userId); protected override APIRequest<MultiplayerScore> CreateScoreRequest() => new ShowPlaylistUserScoreRequest(RoomId, PlaylistItem.ID, userId);
protected override void OnScoresAdded(IEnumerable<ScoreInfo> scores) protected override void OnScoresAdded(ScoreInfo[] scores)
{ {
base.OnScoresAdded(scores); base.OnScoresAdded(scores);

View File

@ -246,7 +246,7 @@ namespace osu.Game.Screens.Ranking
if (lastFetchCompleted) if (lastFetchCompleted)
{ {
Task<IEnumerable<ScoreInfo>> nextPageTask = Task.FromResult<IEnumerable<ScoreInfo>>([]); Task<ScoreInfo[]> nextPageTask = Task.FromResult<ScoreInfo[]>([]);
if (ScorePanelList.IsScrolledToStart) if (ScorePanelList.IsScrolledToStart)
nextPageTask = FetchNextPage(-1); nextPageTask = FetchNextPage(-1);
@ -322,13 +322,13 @@ namespace osu.Game.Screens.Ranking
/// <summary> /// <summary>
/// Performs a fetch/refresh of scores to be displayed. /// Performs a fetch/refresh of scores to be displayed.
/// </summary> /// </summary>
protected virtual Task<IEnumerable<ScoreInfo>> FetchScores() => Task.FromResult<IEnumerable<ScoreInfo>>([]); protected virtual Task<ScoreInfo[]> FetchScores() => Task.FromResult<ScoreInfo[]>([]);
/// <summary> /// <summary>
/// Performs a fetch of the next page of scores. This is invoked every frame. /// Performs a fetch of the next page of scores. This is invoked every frame.
/// </summary> /// </summary>
/// <param name="direction">The fetch direction. -1 to fetch scores greater than the current start of the list, and 1 to fetch scores lower than the current end of the list.</param> /// <param name="direction">The fetch direction. -1 to fetch scores greater than the current start of the list, and 1 to fetch scores lower than the current end of the list.</param>
protected virtual Task<IEnumerable<ScoreInfo>> FetchNextPage(int direction) => Task.FromResult<IEnumerable<ScoreInfo>>([]); protected virtual Task<ScoreInfo[]> FetchNextPage(int direction) => Task.FromResult<ScoreInfo[]>([]);
/// <summary> /// <summary>
/// Creates the <see cref="Statistics.StatisticsPanel"/> to be used to display extended information about scores. /// Creates the <see cref="Statistics.StatisticsPanel"/> to be used to display extended information about scores.
@ -340,7 +340,7 @@ namespace osu.Game.Screens.Ranking
: new StatisticsPanel(); : new StatisticsPanel();
} }
private void addScores(IEnumerable<ScoreInfo> scores) => Schedule(() => private void addScores(ScoreInfo[] scores) => Schedule(() =>
{ {
foreach (var s in scores) foreach (var s in scores)
{ {
@ -365,7 +365,7 @@ namespace osu.Game.Screens.Ranking
/// Invoked after online scores are fetched and added to the list. /// Invoked after online scores are fetched and added to the list.
/// </summary> /// </summary>
/// <param name="scores">The scores that were added.</param> /// <param name="scores">The scores that were added.</param>
protected virtual void OnScoresAdded(IEnumerable<ScoreInfo> scores) protected virtual void OnScoresAdded(ScoreInfo[] scores)
{ {
} }

View File

@ -31,7 +31,7 @@ namespace osu.Game.Screens.Ranking
{ {
} }
protected override async Task<IEnumerable<ScoreInfo>> FetchScores() protected override async Task<ScoreInfo[]> FetchScores()
{ {
Debug.Assert(Score != null); Debug.Assert(Score != null);
@ -70,7 +70,7 @@ namespace osu.Game.Screens.Ranking
} }
} }
return toDisplay; return toDisplay.ToArray();
} }
catch (OperationCanceledException) catch (OperationCanceledException)
{ {