Skip to content

Commit d4145e4

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

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

osu.Game/Rulesets/Difficulty/DifficultyCalculator.cs

Lines changed: 15 additions & 6 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>
@@ -143,9 +150,10 @@ public IEnumerable<TimedDifficultyAttributes> CalculateTimedLazy([NotNull] IEnum
143150
var skills = CreateSkills(Beatmap, playableMods, clockRate);
144151
var progressiveBeatmap = new ProgressiveCalculationBeatmap(Beatmap);
145152
var difficultyObjects = getDifficultyHitObjects().ToArray();
146-
return enumerateTimed();
147153

148-
IEnumerable<TimedDifficultyAttributes> enumerateTimed()
154+
return enumerate();
155+
156+
IEnumerable<TimedDifficultyAttributes> enumerate()
149157
{
150158
int currentIndex = 0;
151159

@@ -157,6 +165,7 @@ IEnumerable<TimedDifficultyAttributes> enumerateTimed()
157165
{
158166
foreach (var skill in skills)
159167
{
168+
cancellationToken.ThrowIfCancellationRequested();
160169
skill.Process(difficultyObjects[currentIndex]);
161170
}
162171

0 commit comments

Comments
 (0)