-
Notifications
You must be signed in to change notification settings - Fork 4.9k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
JIT: Hint to "inline" generic type parameter #32815
Comments
Seems like the same ask as in #9682. Changing to area-meta. |
Almost the same. But with GenericSpecialization the author of the HexWriter would need to provide the full list of the buffer writers in advance. I would like to avoid that because the HexWriter could be defined in some library and the SimpleBufferWriter could be a client class. And as far as I understood the [MethodImpl(MethodImplOptions.NonSharedGeneric)] is supposed to be declared for methods only. And that works for all generic parameters at once, not to only one. |
To narrow the scope the attribute could be added to the client-side class: [GenericSpecializationOn(typeof(HexWriter<>), ...)] // Classes to be "inlined" into. With methodof(...) we would also be able to reference the methods.
public sealed class SimpleBufferWriter<T> : IBufferWriter<T>, IDisposable So it would be complementary to the GenericSpecialization from #9682 (comment). Btw, I believe that GenericSpecialization should be applied for individual generic parameters: TFeature IFeatureCollection.Get<[GenericSpecialization(...)] TFeature>() Edit: class GenericSpecializationOnAttribute : Attribute
{
public GenericSpecializationOnAttribute(params object[] typesAndIndices);
}
class ClassWithThreeParams<T0,T1,T2>{}
[GenericSpecializationOn(
typeof(ClassWithThreeParams<,,>), 2, // T2 parameter of ClassWithThreeParams<T0,T1,T2>
...
)]
public ClientClass
{} |
Both HexWriter and SimpleBufferWriter could be from external libraries and they may know nothing about each other. There has to be another option available for the client-side code: [assembly: GenericSpecialization(typeof(HexWriter<SimpleBufferWriterWrapper<byte>>))] So there will be three places where you will be able to declare the generic specification hints:
|
There will be strange corner cases if the target or source classes have subclasses or if both classes are defined in the library and already instantiated there. |
Correct me if I'm wrong but I believe this would actually first require language support for it, so instead of logging this here, we should probably first try logging this on the https://github.com/dotnet/roslyn repo, so that it starts the discussion over there, would that be ok? |
Due to lack of recent activity, this issue has been marked as a candidate for backlog cleanup. It will be closed if no further activity occurs within 14 more days. Any new comment (by anyone, not necessarily the author) will undo this process. This process is part of our issue cleanup automation. |
This issue will now be closed since it had been marked |
Scenario
Let's assume we have a simple buffer writer like this:
SimpleBufferWriter
We created some buffer writer consumer as a generic class parametrized by the buffer writer class like this:
Also there is a struct wrapping the SimpleBufferWriter:
Benchmark test code
Test results:
The wrapped version is significantly faster than the unwrapped one because the JIT compiler compiles the generic type for each struct type parameter separately so that calls the IBufferWriter implementation methods directly (not via interface) and so it also can inline them. This looks strange but we added another level of indirection to let the JIT compiler get rid of all the levels of indirection and get everything inlined 😄
Feature request
That would be very good if the "inlineable generic parameters" behavior would be (at least optionally) available for reference types as well so it wouldn't be required to create wrapper structs to achieve that.
That could be declared either by special attribute (C# language won't be extended):
or by a special syntax
The text was updated successfully, but these errors were encountered: