Open EgorBo opened 1 month ago
@EgorBot -arm64 -amd --runtimes net8.0 net9.0
using System.Runtime.CompilerServices;
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Running;
using System.Collections.Generic;
using System.Collections;
public class MyClass {}
public class Bench
{
object _obj = new MyClass[2];
[Benchmark]
public int IsCollection() => _obj is ICollection<MyClass> coll ? coll.Count : 0;
[Benchmark]
public int IsCollection_expected()
{
var obj = _obj;
if (obj is MyClass[])
return Unsafe.As<object, MyClass[]>(ref obj).Length;
return 0;
}
}
Tagging subscribers to this area: @JulieLeeMSFT, @jakobbotsch See info in area-owners.md if you want to be subscribed.
LINQ has many "fast path" casts to avoid O(n) when it sees the input is a known collection. It seems like there are many opportunities around such casts for JIT. Example:
Dynamic PGO should make both benchmark to be equally fast, unfortunately, it's not the case currently: