Skip to content

Commit

Permalink
[Common] Avoid GC in DeviceCollection
Browse files Browse the repository at this point in the history
`DeviceCollection.GetEnumerator()` now returns a struct IEnumerable<T>
directly to avoid boxing.

Additionally, we can now use `DeviceCollection[int]` as a shortcut to
`FromIndex(int)`.
  • Loading branch information
thefiddler committed Sep 20, 2014
1 parent 2b3a4c5 commit 747d86b
Showing 1 changed file with 12 additions and 1 deletion.
13 changes: 12 additions & 1 deletion Source/OpenTK/Platform/DeviceCollection.cs
Expand Up @@ -46,7 +46,7 @@ class DeviceCollection<T> : IEnumerable<T>

#region IEnumerable<T> Members

public IEnumerator<T> GetEnumerator()
IEnumerator<T> IEnumerable<T>.GetEnumerator()
{
return Devices.GetEnumerator();
}
Expand All @@ -64,6 +64,17 @@ System.Collections.IEnumerator System.Collections.IEnumerable.GetEnumerator()

#region Public Members

// This avoids boxing when using foreach loops
public List<T>.Enumerator GetEnumerator()
{
return Devices.GetEnumerator();
}

public T this[int index]
{
get { return FromIndex(index); }
}

/// \internal
/// <summary>
/// Adds or replaces a device based on its hardware id.
Expand Down

0 comments on commit 747d86b

Please sign in to comment.