As we all know that if we compare a value type variable with null,it will cause GC alloc cause of boxing:
{
public class TestGeneric<T>{
public static bool TestGC(T key)
{
return key == null;
}
}
TestGeneric<int>.TestGC(10);
}
However, I found that the Find(T value) method of LinkedList(System.Collections.Generic) used same code like
public LinkedListNode<T> Find(T value)
{
if (value != null)
{
//....
}
}
But the calling like
//LinkedList<int> list;
list.Find(10);
will NOT cause any GC alloc.
I am really confused about that. Any body got an idea? Please Help.Thanks a lot.
====================
Test enviroment: Unity 2018, Unity Profiler(Deep Profile Enable) .Net Version:4.0
Here is my test code.
public class TestGeneric<T> {
public static bool TestGC(T key)
{
return key == null;
}
}
public class LinkedListTest : MonoBehaviour
{
LinkedList<int> list = new LinkedList<int>();
void Start()
{
for (int i = 0; i < 10; ++i)
{
list.AddLast(i);
}
}
void Update()
{
for (int i = 0; i < 10; ++i)
{
TestGeneric<int>.TestGC(10);
}
for (int i = 0; i < 10; ++i)
{
list.Find(100);
}
}
}
The unity profiler shows
TestGC method caused 200Byte GC Alloc per frame.
list.Find method caused 0Byte GC Alloc per frame.