Greatly improve performance due to ScriptIterator changes
All checks were successful
Build / Build (push) Successful in 1m43s

This commit is contained in:
Deukhoofd 2025-07-26 14:41:18 +02:00
parent 6eba332096
commit cccffc4954

View File

@ -8,6 +8,7 @@ namespace PkmnLib.Dynamic.ScriptHandling;
public class ScriptIterator : IEnumerable<ScriptContainer>
{
private readonly IReadOnlyList<IEnumerable<ScriptContainer>> _scripts;
private int _currentIndex = 0;
/// <inheritdoc cref="ScriptIterator"/>
public ScriptIterator(IReadOnlyList<IEnumerable<ScriptContainer>> scripts)
@ -15,39 +16,41 @@ public class ScriptIterator : IEnumerable<ScriptContainer>
_scripts = scripts;
}
private IEnumerable<ScriptContainer> GetAsEnumerable()
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator()
{
foreach (var enumerable in _scripts)
while (_currentIndex < _scripts.Count)
{
if (enumerable is IScriptSet set)
var enumerable = _scripts[_currentIndex++];
switch (enumerable)
{
// We can't assume that the set remains the same size, so we need to iterate through it
// with a for loop.
for (var j = 0; j < set.Count; j++)
case IScriptSet set:
{
var script = set.At(j);
// We can ignore empty scripts.
if (script.IsEmpty)
continue;
yield return script;
}
}
else
{
foreach (var script in enumerable)
{
// We can ignore empty scripts.
if (script.IsEmpty)
continue;
yield return script;
// We can't assume that the set remains the same size, so we need to iterate through it
// with a for loop.
for (var j = 0; j < set.Count; j++)
{
var script = set.At(j);
// We can ignore empty scripts.
if (script.IsEmpty)
continue;
yield return script;
}
break;
}
case ScriptContainer { IsEmpty: true }:
continue;
case ScriptContainer container:
yield return container;
break;
default:
throw new InvalidOperationException(
$"Unexpected script type: {enumerable.GetType().Name}. Expected IScriptSet or ScriptContainer.");
}
}
_currentIndex = 0;
}
/// <inheritdoc />
public IEnumerator<ScriptContainer> GetEnumerator() => GetAsEnumerable().GetEnumerator();
/// <inheritdoc />
IEnumerator IEnumerable.GetEnumerator() => GetEnumerator();
}