Skip to content

Commit 5c92781

Browse files
authored
Improve map performance (IronLanguages#1990)
1 parent 1575ad0 commit 5c92781

1 file changed

Lines changed: 13 additions & 2 deletions

File tree

  • src/core/IronPython/Runtime

src/core/IronPython/Runtime/Map.cs

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@ namespace IronPython.Runtime {
2323
public class Map : IEnumerator {
2424
private readonly CodeContext _context;
2525
private readonly object? _func;
26+
private readonly IEnumerator? _enumerator;
2627
private readonly IEnumerator[] _enumerators;
2728

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

43+
// fast path for single iterable
44+
if (_enumerators.Length == 1) {
45+
_enumerator = _enumerators[0];
46+
}
47+
4248
_context = context;
4349
_func = func;
4450
}
@@ -48,8 +54,13 @@ public Map(CodeContext context, object? func, [NotNone] params object[] iterable
4854

4955
[PythonHidden]
5056
public bool MoveNext() {
51-
if (_enumerators.Length > 0 && _enumerators.All(x => x.MoveNext())) {
52-
Current = PythonOps.CallWithContext(_context, _func, _enumerators.Select(x => x.Current).ToArray());
57+
if (_enumerator is null) {
58+
if (_enumerators.All(x => x.MoveNext())) {
59+
Current = PythonOps.CallWithContext(_context, _func, _enumerators.Select(x => x.Current).ToArray());
60+
return true;
61+
}
62+
} else if (_enumerator.MoveNext()) {
63+
Current = PythonCalls.Call(_context, _func, _enumerator.Current);
5364
return true;
5465
}
5566
Current = default;

0 commit comments

Comments
 (0)