5

How to use RuntimeHelpers.IsReferenceOrContainsReferences to micro-optimize coll...

 1 year ago
source link: https://www.meziantou.net/how-to-use-runtimehelpers-isreferenceorcontainsreferences-to-micro-optimize-coll.htm
Go to the source link to view the article. You can view the picture content, updated content and better typesetting reading experience. If the link is broken, please click the button below to view the snapshot at that time.
neoserver,ios ssh client

How to use RuntimeHelpers.IsReferenceOrContainsReferences to micro-optimize collections

 06/19/2023
 
  • Gérald Barré

RuntimeHelpers.IsReferenceOrContainsReferences<T>() is a method that allows to known if T is a reference type or contains a reference type. One use case for this method is to micro-optimize collections when removing items. Indeed, if T is a reference type, you need to remove the reference to the item to allow the GC to collect it. If T is a value type, you don't need to do that so you can avoid the memory access. This is a very small optimization, but it can be useful in some cases. Note that the JIT will replace the statement with a constant and remove branches that are always false.

Here's an example of a custom list that uses this method:

class CustomList<T>
{
    private readonly T[] _items;
    private int _count;

    public CustomList(int capacity)
        => _items = new T[capacity];

    public void Push(T item)
    {
        if (_count == _items.Length)
            throw new InvalidOperationException("The list is full.");

        _items[_count] = item;
        _count++;
    }

    public T Pop()
    {
        if (_count == 0)
            throw new InvalidOperationException("The list is empty.");

        var value = _items[_count - 1];

        // If T is a reference type or contains a reference type, you need to remove
        // the reference to the item to allow the GC to collect it.
        // For instance, CustomList<string> will need to do that, but CustomList<int> won't.
        // Note: The JIT will remove the if statement if the condition is false, so there is no performance impact.
        if (RuntimeHelpers.IsReferenceOrContainsReferences<T>())
        {
            _items[count - 1] = default;
        }

        _count--;
        return value;
    }
}

Do you have a question or a suggestion about this post? Contact me!

Follow me:

Recommend

About Joyk


Aggregate valuable and interesting links.
Joyk means Joy of geeK