Repository navigation
Description
The current implementation of stelem.ref does the type check using only the runtime type of the array; it does not receive any information about the array's static type which could be used to optimize the common case where the static element type of the array is the same as the runtime type. Because of this, it is currently more efficient to get a reference to an array element (using ldelema, which does use static type information) and write to that reference than to write to the array element directly, if the type of the object being assigned is a derived type of (but not the same as) the array's element type, as the following benchmark shows: (Results are from .NET 5)
public class Benchmark {
public class A { }
public class B : A { }
public class C : B { }
public class D : C { }
private A[] arr = new A[1000];
public IEnumerable<object> args => new[] { new A(), new B(), new C(), new D() };
[Benchmark(Baseline = true)]
[ArgumentsSource(nameof(args))]
public void AssignToArray(A obj) {
A[] arr = this.arr;
for (int i = 0; i < arr.Length; i++) {
arr[i] = obj;
}
}
[Benchmark]
[ArgumentsSource(nameof(args))]
public void AssignToArrayWithRef(A obj) {
A[] arr = this.arr;
for (int i = 0; i < arr.Length; i++) {
ref A elemRef = ref arr[i];
elemRef = obj;
}
}
}| Method | obj | Mean | Error | StdDev | Ratio | RatioSD | Code Size |
|---|---|---|---|---|---|---|---|
| AssignToArray | Benchmark+A | 2.574 μs | 0.0396 μs | 0.0371 μs | 1.00 | 0.00 | 52 B |
| AssignToArray | Benchmark+B | 9.948 μs | 0.0777 μs | 0.0727 μs | 1.00 | 0.00 | 52 B |
| AssignToArray | Benchmark+C | 8.383 μs | 0.0716 μs | 0.0559 μs | 1.00 | 0.00 | 52 B |
| AssignToArray | Benchmark+D | 6.104 μs | 0.1185 μs | 0.1268 μs | 1.00 | 0.00 | 52 B |
| AssignToArrayWithRef | Benchmark+A | 3.958 μs | 0.0362 μs | 0.0339 μs | 1.54 | 0.02 | 70 B |
| AssignToArrayWithRef | Benchmark+B | 3.997 μs | 0.0250 μs | 0.0222 μs | 0.40 | 0.00 | 70 B |
| AssignToArrayWithRef | Benchmark+C | 3.983 μs | 0.0340 μs | 0.0318 μs | 0.47 | 0.00 | 70 B |
| AssignToArrayWithRef | Benchmark+D | 4.008 μs | 0.0543 μs | 0.0508 μs | 0.66 | 0.01 | 70 B |
There is a special case check for object[] here which can be replaced with a check for the array static type (passed in as an additional handle argument to StelemRef).
- This would not regress the case where the array's type is
object[], aside from the cost of passing in the additional argument. This is because if the runtime type of an array isobject[], its static type must also beobject[]. - With this, it would be safe to pass
object[]as the static type for any array, which could be used in cases where the static type handle is expensive to obtain (for example, in shared generic code) or is unknown.
category:proposal
theme:basic-cq
skill-level:intermediate
cost:medium
impact:small
Metadata
Metadata
Assignees
Labels
Type
Projects
- StatusShow more project fieldsOptimizations
Activity
EgorBo commentedon Sep 1, 2021
So if I understand it correctly, the only way we can optimize
AssignToArrayhere are the following tricks:arrandobjare both known to be of the same exact class (means it's eitherAissealedor jit sees where and how both array and obj were created)arris exactly ofobject[]type - it means we can safely assign everything to itobjisnullarr[i1] = arr[i2];arr[i2] is guaranteed to return the same type so we don't need a check here.the rest of the cases we have to call the slow covariance-aware helper 🙁
However, we can optimize it in JIT like this (what you suggest if I am not mistaken):
or
PGO can help us here to get a better type info cc @AndyAyersMS
EgorBo commentedon Sep 1, 2021
Another missing opportunity in JIT:
JIT should be able to skip covariant checks here
AndyAyersMS commentedon Sep 1, 2021
We can extend
impCanSkipCovariantStoreCheckfor that second case.For the first case I wonder how the cost of the checks to avoid the store check compares to the cost of the store check.
jkotas commentedon Sep 1, 2021
You will end up calling a write barrier helper for this case in any situation, so there is always going to be call overhead.
Here is the existing code of the helper that performs the array store checks
runtime/src/coreclr/System.Private.CoreLib/src/System/Runtime/CompilerServices/CastHelpers.cs
Lines 575 to 579 in d019e70
It may be interesting to look at:
jfd16 commentedon Sep 1, 2021
The static type check would make the check for
object[]redundant, so the only additional overhead would be that of having one additional parameter inCastHelpers.StelemRef.EgorBo commentedon Sep 2, 2021
Thanks for ideas!
The idea has been floating around for a while, it's currently a bit tricky to mark custom calls as inline-candidates in jit by hands but should be possible 👍
Yes, should be pretty straightforward to fix since gtGetClassHandle returns all the needed info