Skip to content

Commit 792d815

Browse files
committed
Removed one enumerator allocation in AddRows<T>
1 parent f22e456 commit 792d815

1 file changed

Lines changed: 9 additions & 9 deletions

File tree

CsvExport/CsvExport.cs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -131,18 +131,18 @@ public void AddRow()
131131
/// </summary>
132132
public void AddRows<T>(IEnumerable<T> list)
133133
{
134-
if (list.Any())
134+
using var e = list.GetEnumerator();
135+
if (!e.MoveNext()) return; //empty - skip reflection cache warm-up
136+
137+
var values = ReflectionCache<T>.Properties;
138+
do
135139
{
136-
var values = ReflectionCache<T>.Properties;
137-
foreach (T obj in list)
140+
AddRow();
141+
foreach (var value in values)
138142
{
139-
AddRow();
140-
foreach (var value in values)
141-
{
142-
this[value.Name] = value.GetValue(obj, null);
143-
}
143+
this[value.Name] = value.GetValue(e.Current, null);
144144
}
145-
}
145+
} while (e.MoveNext());
146146
}
147147

148148
/// <summary>

0 commit comments

Comments
 (0)