1
0
mirror of https://github.com/ppy/osu.git synced 2025-03-05 07:42:54 +08:00

Use queue instead of list

This commit is contained in:
smoogipoo 2020-11-16 20:52:51 +09:00
parent 1b1f4c9c09
commit 87bf168718

View File

@ -22,7 +22,7 @@ namespace osu.Game.Database
protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default) protected override async Task<User> ComputeValueAsync(int lookup, CancellationToken token = default)
=> await queryUser(lookup); => await queryUser(lookup);
private readonly List<(int id, TaskCompletionSource<User>)> pendingUserTasks = new List<(int, TaskCompletionSource<User>)>(); private readonly Queue<(int id, TaskCompletionSource<User>)> pendingUserTasks = new Queue<(int, TaskCompletionSource<User>)>();
private Task pendingRequestTask; private Task pendingRequestTask;
private readonly object taskAssignmentLock = new object(); private readonly object taskAssignmentLock = new object();
@ -33,7 +33,7 @@ namespace osu.Game.Database
var tcs = new TaskCompletionSource<User>(); var tcs = new TaskCompletionSource<User>();
// Add to the queue. // Add to the queue.
pendingUserTasks.Add((userId, tcs)); pendingUserTasks.Enqueue((userId, tcs));
// Create a request task if there's not already one. // Create a request task if there's not already one.
if (pendingRequestTask == null) if (pendingRequestTask == null)
@ -52,9 +52,7 @@ namespace osu.Game.Database
{ {
while (pendingUserTasks.Count > 0 && userTasks.Count < 50) while (pendingUserTasks.Count > 0 && userTasks.Count < 50)
{ {
(int id, TaskCompletionSource<User> task) next = pendingUserTasks[^1]; (int id, TaskCompletionSource<User> task) next = pendingUserTasks.Dequeue();
pendingUserTasks.RemoveAt(pendingUserTasks.Count - 1);
// Perform a secondary check for existence, in case the user was queried in a previous batch. // Perform a secondary check for existence, in case the user was queried in a previous batch.
if (CheckExists(next.id, out var existing)) if (CheckExists(next.id, out var existing))