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
Replace ImmutableAttribute with IImmutable interface #1305
Comments
We could cache the result in |
@ReubenBond |
I would not use interface for that.
I am however NOT a strong opposer to this idea. If you think it is still worth it, and the programming model "cost" is insignificant, I will not object. I just think this is the wrong place to spend cycles. There are other places where we can save much more. |
As every class in the hierarchy inherits the marker interface, would it be obvious for the user that all the classes in the hierarhcy should be immutable? |
I am with @ReubenBond and @gabikliot here - caching is fast and relatively easy. We do that for a lot of other type info. If we need to refactor and improve the type info caching code, that a different question to me that shouldn't have any weight on the conceptual decision. @veikkoeeva brought a subtle but important point that I think shows that the interface approach may be prone to dev mistakes due to not realizing that a particular derived type is considered immutable by the runtime via non-obvious inheritance. |
Agreed. An interface, while cheaper perf wise, is probably not the right approach. Consensus seems to be that if we want to optimize this, which is not a given, we should continue to use attribute, but cache the results so we don't have to incur the cost of the reflection hit each call. If caching is slower than reflection, we can discard change or optimize caching of type information. Any objections / alternatives we should consider? |
The immutable attribute is used to indicate that the data in the class being provided to a grain call will not be modified, so it's safe to pass it without making a copy of the data. This is a grain call optimization. There are two ways to mark data as immutable for grain calls.
Immutable attribute marker
The attribute marks the ImmutableBlarg class as immutable so when it's used in a grain call it will not be copied. This is the preferred method as it is safer, as the type itself enforces the immutability, but it requires that the type be modifiable, so it can't be used if one wants to pass a basic type like byte[] or third party types.
Generic wrapper marker
The Immutable generic wrapper indicates that the values in Blarg will not be changed, so it's safe to not make a copy of the object during the call. This is useful for objects that are not inherently immutable or those who's type information cannot be modified.
The use of immutable data is a valuable optimization, but using an attribute is an inefficient approach. The issue is that getting attributes is one of the more expensive reflection calls, and the use of this attribute means that we must incur this call on every argument on every grain call, regardless if the attribute is use or not.
I propose replacing the ImmutableAttribute with an IImmutable interface, and use an inheritance check (which is cheaper) instead of an attribute check.
Something like:
The use of an interface rather than an attribute reduces the overhead of checking for immutable data. This should result in faster grain calls.
I'd like to introduce the IImmutable interface and mark the ImmutableAttribute as deprecated for Orleans 1.2.0 and remove ImmutableAttribute entirely in 1.3.0.
Thoughts/Opinions
The text was updated successfully, but these errors were encountered: