1
0
mirror of https://github.com/ppy/osu.git synced 2025-02-13 14:13:18 +08:00

Avoid allocating list storage in Pattern until first usage

Patterns can often be constructed only to never be used.
This commit is contained in:
Dean Herbert 2021-09-20 16:02:02 +09:00
parent d96d1b3e47
commit 98f1c1cc29

View File

@ -2,6 +2,7 @@
// 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.Collections.Generic;
using System.Linq;
using osu.Game.Rulesets.Mania.Objects; using osu.Game.Rulesets.Mania.Objects;
namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns
@ -11,26 +12,25 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns
/// </summary> /// </summary>
internal class Pattern internal class Pattern
{ {
private readonly List<ManiaHitObject> hitObjects = new List<ManiaHitObject>(); private List<ManiaHitObject> hitObjects;
private HashSet<int> containedColumns;
private readonly HashSet<int> containedColumns = new HashSet<int>();
/// <summary> /// <summary>
/// All the hit objects contained in this pattern. /// All the hit objects contained in this pattern.
/// </summary> /// </summary>
public IEnumerable<ManiaHitObject> HitObjects => hitObjects; public IEnumerable<ManiaHitObject> HitObjects => hitObjects ?? Enumerable.Empty<ManiaHitObject>();
/// <summary> /// <summary>
/// Check whether a column of this patterns contains a hit object. /// Check whether a column of this patterns contains a hit object.
/// </summary> /// </summary>
/// <param name="column">The column index.</param> /// <param name="column">The column index.</param>
/// <returns>Whether the column with index <paramref name="column"/> contains a hit object.</returns> /// <returns>Whether the column with index <paramref name="column"/> contains a hit object.</returns>
public bool ColumnHasObject(int column) => containedColumns.Contains(column); public bool ColumnHasObject(int column) => containedColumns?.Contains(column) ?? false;
/// <summary> /// <summary>
/// Amount of columns taken up by hit objects in this pattern. /// Amount of columns taken up by hit objects in this pattern.
/// </summary> /// </summary>
public int ColumnWithObjects => containedColumns.Count; public int ColumnWithObjects => containedColumns?.Count ?? 0;
/// <summary> /// <summary>
/// Adds a hit object to this pattern. /// Adds a hit object to this pattern.
@ -38,6 +38,8 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns
/// <param name="hitObject">The hit object to add.</param> /// <param name="hitObject">The hit object to add.</param>
public void Add(ManiaHitObject hitObject) public void Add(ManiaHitObject hitObject)
{ {
prepareStorage();
hitObjects.Add(hitObject); hitObjects.Add(hitObject);
containedColumns.Add(hitObject.Column); containedColumns.Add(hitObject.Column);
} }
@ -48,10 +50,15 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns
/// <param name="other">The other pattern.</param> /// <param name="other">The other pattern.</param>
public void Add(Pattern other) public void Add(Pattern other)
{ {
hitObjects.AddRange(other.HitObjects); prepareStorage();
foreach (var h in other.hitObjects) if (other.hitObjects != null)
containedColumns.Add(h.Column); {
hitObjects.AddRange(other.hitObjects);
foreach (var h in other.hitObjects)
containedColumns.Add(h.Column);
}
} }
/// <summary> /// <summary>
@ -59,8 +66,14 @@ namespace osu.Game.Rulesets.Mania.Beatmaps.Patterns
/// </summary> /// </summary>
public void Clear() public void Clear()
{ {
hitObjects.Clear(); hitObjects?.Clear();
containedColumns.Clear(); containedColumns?.Clear();
}
private void prepareStorage()
{
hitObjects ??= new List<ManiaHitObject>();
containedColumns ??= new HashSet<int>();
} }
} }
} }