Enumerables Without the Garbage: Part 7
Today we’ll wrap up the iterator series by finishing up porting C++’s <algorithm> header. We end up with a library of functions for common LINQ-style algorithms but without any of the garbage creation that slows our games down. Read on for the source and examples!
Here are the functions from the “min/max” and “other” sections that finish up the <algorithm> header:
MinElement: find the smallest element
MaxElement: find the largest element
MinMaxElement: find the smallest and largest elements
LexicographicalCompare: lexicographic order comparison
NextPermutation: get the next permutation of a range
PrevPermutation: get the previous permutation of a range
Building again on the test app from the previous articles in this series, I’ve added tests for each of the above functions to show you how to use them and that they work. You can also consult the thorough C++ documentation since these are direct ports from there. The test app also has a
GcTest function to show that no garbage is created by any of these functions. You can check it out in the TestScript.cs file in the GitHub project. You can also check out the output in the TestScript_output.txt file.
And here’s a screenshot of the
GcTest function in Unity’s profiler in “deep” mode to prove that none of this creates any garbage whatsoever:
Check out the GitHub project for the
Iterator.cs file that implements all of this. If you’ve got any questions about the iterator library or any suggestions on how to make it better, let me know in the comments!
Update: check out part eight