Skip to content
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

Added inref immutability assumption removal #7407

Merged
merged 22 commits into from
Aug 20, 2019

Conversation

TIHan
Copy link
Contributor

@TIHan TIHan commented Aug 15, 2019

This is to resolve: #7406

Gets rid of the immutability assumption for .NET structs when it's an inref.

Added awareness of IsReadOnlyAttribute on method calls from structs, only aware of IL method calls, not F# method calls. This is not a feature, but merely an optimization and will help mitigate extra warnings as a result of the immutability assumption change.

  • Find out if we can optimize the attribute lookup? Simple query, will only check on structs and when we must take an address.
  • Tests

@cartermp cartermp requested a review from dsyme August 16, 2019 20:00
@TIHan
Copy link
Contributor Author

TIHan commented Aug 17, 2019

Almost done. Just need a few additional checks for extensions.

@TIHan
Copy link
Contributor Author

TIHan commented Aug 18, 2019

If tests pass, then this is finished.

@dsyme
Copy link
Contributor

dsyme commented Aug 19, 2019

Hi @TIHan could you review these before proceeding with this?

I believe it's the same root issue, where .NET structs are assumed to be immutable. And you're fixing this for the inref case, which seems reasonable as it is essentially new, and also fixing the other issues around ReadOnly attributes etc.

The code and tests look good, I really just want to double check that this is not impacting the performance of existing code that doesn't use inref, and that we're thinking carefully about the impact of performance of code that does use inref on .NET struct types

@TIHan
Copy link
Contributor Author

TIHan commented Aug 19, 2019

Yea, I can do another review pass that them. I actually looked at both of those on Friday and it did help me. It might be the same root issue.

The core of both the non-inref and inref rules are here (as of current changes):

let isRecdOrStructTyconRefReadOnlyAux g m isInref (tcref: TyconRef) =
    if isInref && tcref.IsILStructOrEnumTycon then
        isTyconRefReadOnly g m tcref
    else
        isTyconRefReadOnly g m tcref || isTyconRefAssumedReadOnly g tcref

I agree that we really need to be careful on existing code that does not use inref. Just in case, I'll add tests that will execute both the non-inref and inref versions to show expected behavior; probably in fsfromfsviacs.

Concerning performance, existing inref code will see an impact if they are using .NET structs that are not marked as read-only, or if the method/property they are calling is not marked read-only. NETCore 2.1+ marked a lot of types as read-only, such as DateTime, Int, etc. In NETCore 3.0, methods are starting to be marked as read-only, such as the System.Numerics Vector types' methods (thanks @tannergooding). With the C# 8 compiler, it will start automatically marking properties as read-only if it only has an automatic getter, no setter. It will even mark them in lower C# lang versions as it is really considered a performance optimization. Based on this, I'm not worried about the performance implications of inref moving forward.

Another thing that we should be doing, is disallowing [<IsReadOnly>] attribute to be put on methods. I'm not going to make that part of this PR though

@TIHan
Copy link
Contributor Author

TIHan commented Aug 20, 2019

Ok, I added tests. I was still able to reproduce @saul 's issue, but it didn't happen on the inref version. I'll look into it since I'm already doing this work.

@TIHan
Copy link
Contributor Author

TIHan commented Aug 20, 2019

Ok @dsyme , I believe these changes do not affect non-inref behavior. I added a test to verify the current behavior that matches @saul 's behavior.

Whenever we resolve #3923 then the test will fail.

@TIHan
Copy link
Contributor Author

TIHan commented Aug 20, 2019

Thank you all for reviewing. Glad to see this get in.

@TIHan TIHan merged commit 8dd81c6 into dotnet:release/dev16.3 Aug 20, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants