Skip to content

Commit 21cf35a

Browse files
committed
Implement lazy timed difficulty calculation
1 parent e9a4d59 commit 21cf35a

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

osu.Game/Rulesets/Difficulty/DifficultyCalculator.cs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,9 @@ public DifficultyAttributes Calculate([NotNull] IEnumerable<Mod> mods, Cancellat
9494
/// <summary>
9595
/// Calculates the difficulty of the beatmap with no mods applied and returns a set of <see cref="TimedDifficultyAttributes"/> representing the difficulty at every relevant time value in the beatmap.
9696
/// </summary>
97+
/// <remarks>
98+
/// Unless a <paramref name="cancellationToken"/> is specified, calculation times out after 10 seconds.
99+
/// </remarks>
97100
/// <param name="cancellationToken">The cancellation token.</param>
98101
/// <returns>The set of <see cref="TimedDifficultyAttributes"/>.</returns>
99102
public List<TimedDifficultyAttributes> CalculateTimed(CancellationToken cancellationToken = default)
@@ -102,12 +105,15 @@ public List<TimedDifficultyAttributes> CalculateTimed(CancellationToken cancella
102105
/// <summary>
103106
/// Calculates the difficulty of the beatmap using a specific mod combination and returns a set of <see cref="TimedDifficultyAttributes"/> representing the difficulty at every relevant time value in the beatmap.
104107
/// </summary>
108+
/// <remarks>
109+
/// Unless a <paramref name="cancellationToken"/> is specified, calculation times out after 10 seconds.
110+
/// </remarks>
105111
/// <param name="mods">The mods that should be applied to the beatmap.</param>
106112
/// <param name="cancellationToken">The cancellation token.</param>
107113
/// <returns>The set of <see cref="TimedDifficultyAttributes"/>.</returns>
108114
public List<TimedDifficultyAttributes> CalculateTimed([NotNull] IEnumerable<Mod> mods, CancellationToken cancellationToken = default)
109115
{
110-
List<TimedDifficultyAttributes> list = [];
116+
List<TimedDifficultyAttributes> attribs = [];
111117
using var timedCancellationSource = new CancellationTokenSource(TimeSpan.FromSeconds(10));
112118

113119
if (!cancellationToken.CanBeCanceled)
@@ -116,17 +122,18 @@ public List<TimedDifficultyAttributes> CalculateTimed([NotNull] IEnumerable<Mod>
116122
foreach (var timedAttr in CalculateTimedLazy(mods, cancellationToken))
117123
{
118124
cancellationToken.ThrowIfCancellationRequested();
119-
list.Add(timedAttr);
125+
attribs.Add(timedAttr);
120126
}
121127

122-
return list;
128+
return attribs;
123129
}
124130

125131
/// <summary>
126132
/// Lazily calculates the difficulty of the beatmap on-demand using a specific mod combination and yields <see cref="TimedDifficultyAttributes"/> representing the difficulty until every relevant time value in the beatmap.
127133
/// </summary>
128134
/// <remarks>
129-
/// Preprocessing is done before this method returns.
135+
/// 1. Preprocessing is done before this method returns.<br />
136+
/// 2. Cancelling the <paramref name="cancellationToken"/> will throw while enumerating.
130137
/// </remarks>
131138
/// <param name="mods">The mods that should be applied to the beatmap.</param>
132139
/// <param name="cancellationToken">The cancellation token.</param>
@@ -157,6 +164,7 @@ IEnumerable<TimedDifficultyAttributes> enumerateTimed()
157164
{
158165
foreach (var skill in skills)
159166
{
167+
cancellationToken.ThrowIfCancellationRequested();
160168
skill.Process(difficultyObjects[currentIndex]);
161169
}
162170

0 commit comments

Comments
 (0)