-
Notifications
You must be signed in to change notification settings - Fork 795
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 (nearly) all ref cells in the compiler with mutable values #8063
Conversation
Is there any performance benefit to this? Or is it just a stylistic change? |
Mostly stylistic, and most of the uses seem to predate |
Taking the opportunity to also make a question: is there any remaining case (or cases) where usage of a ref cell is recommended over a mutable let? |
@heronbpv There are, but I'd say the uses are limited. The syntax just looks kind of wonky and the dereference operator For bogstandard mutation that's local to a function or method, typically done for perf reasons, mutable values are preferred. Same goes for I'd also say that using a
In those cases, you'll need to declared something as a Additionally, tupled values for DUs do not support type DU =
| A of int * mutable int // Not supported, needs to be 'int ref'
| B of int * string Additionally, there is a pattern in this codebase where a value is locked via fsharp/src/fsharp/FSharp.Core/seq.fs Lines 1015 to 1021 in e69e4f1
Finally, there are some cases where a mutable value will actually be converted into a ref cell, such as enclosing the value in a lambda function that is also the return value: let f =
let mutable x = 0
fun _ -> x <- x + 1
let g =
let x = ref 0
fun _ -> x := !x + 1
f()
f()
g()
g() Both function values are equivalent. This is getting into pretty niche territory though. I'd say that these cases tend to be pretty niche in F# programming, since mutating values is usually done local to a function or method and done for performance reasons. In those cases, |
Thanks for the answer, @cartermp ! |
@cartermp , this is good work. I will mark it approved, and leave it to you to deal with:
I think just reverting the change should be good enough. Kevin |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Subject to you dealing with:
let mutable table = mtyp.ActivePatternElemRefLookupTable
cacheOptRef &table (fun () ->
@KevinRansom Thanks, yeah it's been kinda fun digging through what are surely some of the older parts of the codebase. The |
...arp.ProjectSystem.PropertyPages/Resources/xlf/Microsoft.VisualStudio.Editors.Designer.es.xlf
Outdated
Show resolved
Hide resolved
I will incorporate parts of #8062 into this |
# This is the 1st commit message: ref -> mutable in more places in the compiler # The commit message dotnet#2 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191229.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19629.1 # The commit message dotnet#3 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191230.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19630.1 # The commit message dotnet#4 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191231.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19631.1 # The commit message dotnet#5 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20200101.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.20051.1 # The commit message dotnet#6 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191216.5 (dotnet#8079) # # - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19616.5 # The commit message dotnet#7 will be skipped: # dispose fsi at the end of a scripting session (dotnet#8084) # # The commit message dotnet#8 will be skipped: # Added static link tests and extended CompilerAssert (dotnet#8101) # # * Changed CompilerAssert to static class. Added Compile/Execute methods that take a Compilation description. Added static link tests # # * Hiding compilation description internals # # * Added another test to check for sanity # # * Making a few optional parameters # # * Hiding internals of CompilationReference # The commit message dotnet#9 will be skipped: # Parameterize product version (dotnet#8031) # # * Parameterize Product details # # * fcs # # * Repack pkgdef
Great work |
Just need to figure out why some parts of absil are so touchy :) |
…otnet#8063) * # This is a combination of 9 commits. # This is the 1st commit message: ref -> mutable in more places in the compiler # The commit message #2 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191229.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19629.1 # The commit message #3 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191230.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19630.1 # The commit message #4 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191231.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19631.1 # The commit message #5 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20200101.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.20051.1 # The commit message #6 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191216.5 (dotnet#8079) # # - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19616.5 # The commit message #7 will be skipped: # dispose fsi at the end of a scripting session (dotnet#8084) # # The commit message #8 will be skipped: # Added static link tests and extended CompilerAssert (dotnet#8101) # # * Changed CompilerAssert to static class. Added Compile/Execute methods that take a Compilation description. Added static link tests # # * Hiding compilation description internals # # * Added another test to check for sanity # # * Making a few optional parameters # # * Hiding internals of CompilationReference # The commit message #9 will be skipped: # Parameterize product version (dotnet#8031) # # * Parameterize Product details # # * fcs # # * Repack pkgdef * no ilread
…otnet#8063) * # This is a combination of 9 commits. # This is the 1st commit message: ref -> mutable in more places in the compiler # The commit message #2 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191229.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19629.1 # The commit message #3 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191230.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19630.1 # The commit message #4 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191231.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19631.1 # The commit message #5 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20200101.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.20051.1 # The commit message #6 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191216.5 (dotnet#8079) # # - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19616.5 # The commit message #7 will be skipped: # dispose fsi at the end of a scripting session (dotnet#8084) # # The commit message #8 will be skipped: # Added static link tests and extended CompilerAssert (dotnet#8101) # # * Changed CompilerAssert to static class. Added Compile/Execute methods that take a Compilation description. Added static link tests # # * Hiding compilation description internals # # * Added another test to check for sanity # # * Making a few optional parameters # # * Hiding internals of CompilationReference # The commit message #9 will be skipped: # Parameterize product version (dotnet#8031) # # * Parameterize Product details # # * fcs # # * Repack pkgdef * no ilread
…otnet#8063) * # This is a combination of 9 commits. # This is the 1st commit message: ref -> mutable in more places in the compiler # The commit message #2 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191229.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19629.1 # The commit message #3 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191230.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19630.1 # The commit message #4 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191231.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.19631.1 # The commit message #5 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20200101.1 # # - Microsoft.DotNet.Arcade.Sdk - 5.0.0-beta.20051.1 # The commit message #6 will be skipped: # Update dependencies from https://github.com/dotnet/arcade build 20191216.5 (dotnet#8079) # # - Microsoft.DotNet.Arcade.Sdk - 1.0.0-beta.19616.5 # The commit message #7 will be skipped: # dispose fsi at the end of a scripting session (dotnet#8084) # # The commit message #8 will be skipped: # Added static link tests and extended CompilerAssert (dotnet#8101) # # * Changed CompilerAssert to static class. Added Compile/Execute methods that take a Compilation description. Added static link tests # # * Hiding compilation description internals # # * Added another test to check for sanity # # * Making a few optional parameters # # * Hiding internals of CompilationReference # The commit message #9 will be skipped: # Parameterize product version (dotnet#8031) # # * Parameterize Product details # # * fcs # # * Repack pkgdef * no ilread
The large majority of ref cells in the compiler codebase are trivially replaceable with mutable values. A few are not, and so those are mostly left untouched. I think this helps with codebase readability.
Excludes test code