How to use RuntimeHelpers.IsReferenceOrContainsReferences to micro-optimize coll...
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.
How to use RuntimeHelpers.IsReferenceOrContainsReferences to micro-optimize collections
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!
Recommend
-
100
Features Easy to Use Micro's number one feature is being easy...
-
5
extra-crunch-eExtra CrunchUse Git data to optimize your developers’ annual reviews3 metrics can help you understand true performance quality
-
5
Data analytics has upended the world of business in countless ways. A growing number of businesses are discovering new applications for data analy...
-
7
The Best Micro Conversions Guide: What They Are and How to Optimize ThemConversion rate optimization is the process of optimizing your w...
-
7
Aritra Ghosh 8 minutes ago Program FDM_COLL_MAINTAIN_BR_HEAD & Table FDM_COLL_BR_HEAD 2 Views ...
-
6
Use a micro:bit as remote Bluetooth temperature sensor
-
5
Use FleetDM to optimize system monitoring with Osquery Get started with FleetDM, an open source tool to submit and schedule quer...
-
5
-
8
Here's a framework for maximizing Google Analytics 4 to improve your digital marketing strategy. Greg Jarboe on May 3...
-
3
Digital leaders use FinOps to optimize the business for AI and sustainability By Mark Chillingworth April 8, 20...
About Joyk
Aggregate valuable and interesting links.
Joyk means Joy of geeK