Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 13 additions & 2 deletions src/core/IronPython/Runtime/Map.cs
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ namespace IronPython.Runtime {
public class Map : IEnumerator {
private readonly CodeContext _context;
private readonly object? _func;
private readonly IEnumerator? _enumerator;
private readonly IEnumerator[] _enumerators;

public Map(CodeContext context, object? func, [NotNone] params object[] iterables) {
Expand All @@ -39,6 +40,11 @@ public Map(CodeContext context, object? func, [NotNone] params object[] iterable
_enumerators[i] = enumerator;
}

// fast path for single iterable
if (_enumerators.Length == 1) {
_enumerator = _enumerators[0];
}

_context = context;
_func = func;
}
Expand All @@ -48,8 +54,13 @@ public Map(CodeContext context, object? func, [NotNone] params object[] iterable

[PythonHidden]
public bool MoveNext() {
if (_enumerators.Length > 0 && _enumerators.All(x => x.MoveNext())) {
Current = PythonOps.CallWithContext(_context, _func, _enumerators.Select(x => x.Current).ToArray());
if (_enumerator is null) {
if (_enumerators.All(x => x.MoveNext())) {
Current = PythonOps.CallWithContext(_context, _func, _enumerators.Select(x => x.Current).ToArray());
return true;
}
} else if (_enumerator.MoveNext()) {
Current = PythonCalls.Call(_context, _func, _enumerator.Current);
return true;
}
Current = default;
Expand Down
Loading