-
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
Remove defensive copies in System.Collections.Immutable
#101156
Conversation
Tagging subscribers to this area: @dotnet/area-system-collections |
What problems this creates here exactly? are you seeing some suboptimal codegen or correctness issues? it seems that in most uses we only access .Count on top of it and if it's inlined - there should be no difference? |
This one is not a correctness issue - it will cause the field to be copied to a local and then have the method be called on that - the change will result in better IL, but I'm not sure if the actual codegen of this particular issue will be affected though, since, at least out of the handful I looked through, they seem to just have 1x class field implementing the logic anyway (I'm not sure if the runtime can see through that or not - I assume you'd know). |
My understanding that this doesn't create any problems for e.g. small structs or when the method we call is inlined into a field load etc. I think we have quite a lot of |
Codegen seems to only be affected for methods that are not inlined: link. Some of the member accesses seem to call methods that I don't imagine would be inlined (e.g., some |
For generic ones, I would think they should only be potentially changed if it's an internal generic constraint, since otherwise you don't know if what's implementing it is actually |
Thanks, but unless you can highlight a specific problem this is already causing, I'd prefer we close this, so as to a) not churn the code unnecessarily and b) stick with things being readonly unless there's a good reason they shouldn't be. |
The field being marked
readonly
causes 26 uses of it in this file to create a defensive copy (since it's generic, it has no such things asreadonly
members, meaning every access requires a defensive copy). Removing thereadonly
should resolve these.