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

Trying something with AsyncMemoize #16540

Closed
wants to merge 22 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion docs/release-notes/.FSharp.Compiler.Service/8.0.300.md
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@

* `implicitCtorSynPats` in `SynTypeDefnSimpleRepr.General` is now `SynPat option` instead of `SynSimplePats option`. ([PR #16425](https://github.com/dotnet/fsharp/pull/16425))
* `SyntaxVisitorBase<'T>.VisitSimplePats` now takes `SynPat` instead of `SynSimplePat list`. ([PR #16425](https://github.com/dotnet/fsharp/pull/16425))
* Reduce allocations in compiler checking via `ValueOption` usage ([PR #16323](https://github.com/dotnet/fsharp/pull/16323))
* Reduce allocations in compiler checking via `ValueOption` usage ([PR #16323](https://github.com/dotnet/fsharp/pull/16323))
* Reduce indentation in `AsyncMemoize`. ([PR #16540](https://github.com/dotnet/fsharp/pull/16540))
472 changes: 206 additions & 266 deletions src/Compiler/Facilities/AsyncMemoize.fs

Large diffs are not rendered by default.

8 changes: 0 additions & 8 deletions src/Compiler/Facilities/BuildGraph.fs
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,12 @@ let wrapThreadStaticInfo computation =
async {
let diagnosticsLogger = DiagnosticsThreadStatics.DiagnosticsLogger
let phase = DiagnosticsThreadStatics.BuildPhase
let ct = Cancellable.Token

try
return! computation
finally
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
DiagnosticsThreadStatics.BuildPhase <- phase
Cancellable.Token <- ct
}

type Async<'T> with
Expand Down Expand Up @@ -127,23 +125,20 @@ type NodeCode private () =
static member RunImmediate(computation: NodeCode<'T>, ct: CancellationToken) =
let diagnosticsLogger = DiagnosticsThreadStatics.DiagnosticsLogger
let phase = DiagnosticsThreadStatics.BuildPhase
let ct2 = Cancellable.Token

try
try
let work =
async {
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
DiagnosticsThreadStatics.BuildPhase <- phase
Cancellable.Token <- ct2
return! computation |> Async.AwaitNodeCode
}

Async.StartImmediateAsTask(work, cancellationToken = ct).Result
finally
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
DiagnosticsThreadStatics.BuildPhase <- phase
Cancellable.Token <- ct2
with :? AggregateException as ex when ex.InnerExceptions.Count = 1 ->
raise (ex.InnerExceptions[0])

Expand All @@ -153,22 +148,19 @@ type NodeCode private () =
static member StartAsTask_ForTesting(computation: NodeCode<'T>, ?ct: CancellationToken) =
let diagnosticsLogger = DiagnosticsThreadStatics.DiagnosticsLogger
let phase = DiagnosticsThreadStatics.BuildPhase
let ct2 = Cancellable.Token

try
let work =
async {
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
DiagnosticsThreadStatics.BuildPhase <- phase
Cancellable.Token <- ct2
return! computation |> Async.AwaitNodeCode
}

Async.StartAsTask(work, cancellationToken = defaultArg ct CancellationToken.None)
finally
DiagnosticsThreadStatics.DiagnosticsLogger <- diagnosticsLogger
DiagnosticsThreadStatics.BuildPhase <- phase
Cancellable.Token <- ct2

static member CancellationToken = cancellationToken

Expand Down
3 changes: 0 additions & 3 deletions src/Compiler/Interactive/fsi.fs
Original file line number Diff line number Diff line change
Expand Up @@ -4089,7 +4089,6 @@ type FsiInteractionProcessor
?cancellationToken: CancellationToken
) =
let cancellationToken = defaultArg cancellationToken CancellationToken.None
use _ = Cancellable.UsingToken(cancellationToken)

if tokenizer.LexBuffer.IsPastEndOfStream then
let stepStatus =
Expand Down Expand Up @@ -4218,7 +4217,6 @@ type FsiInteractionProcessor

member _.EvalInteraction(ctok, sourceText, scriptFileName, diagnosticsLogger, ?cancellationToken) =
let cancellationToken = defaultArg cancellationToken CancellationToken.None
use _ = Cancellable.UsingToken(cancellationToken)
use _ = UseBuildPhase BuildPhase.Interactive
use _ = UseDiagnosticsLogger diagnosticsLogger
use _scope = SetCurrentUICultureForThread fsiOptions.FsiLCID
Expand Down Expand Up @@ -4895,7 +4893,6 @@ type FsiEvaluationSession
SpawnInteractiveServer(fsi, fsiOptions, fsiConsoleOutput)

use _ = UseBuildPhase BuildPhase.Interactive
use _ = Cancellable.UsingToken(CancellationToken.None)

if fsiOptions.Interact then
// page in the type check env
Expand Down
28 changes: 0 additions & 28 deletions src/Compiler/Service/BackgroundCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -574,9 +574,6 @@ type internal BackgroundCompiler
Activity.Tags.cache, cache.ToString()
|]

let! ct = Async.CancellationToken
use _ = Cancellable.UsingToken(ct)

if cache then
let hash = sourceText.GetHashCode() |> int64

Expand Down Expand Up @@ -629,9 +626,6 @@ type internal BackgroundCompiler
"BackgroundCompiler.GetBackgroundParseResultsForFileInProject"
[| Activity.Tags.fileName, fileName; Activity.Tags.userOpName, userOpName |]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, creationDiags = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand Down Expand Up @@ -783,9 +777,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! cachedResults =
node {
let! builderOpt, creationDiags = getAnyBuilder (options, userOpName)
Expand Down Expand Up @@ -846,9 +837,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, creationDiags = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand Down Expand Up @@ -897,9 +885,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, creationDiags = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand Down Expand Up @@ -969,9 +954,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, _ = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand All @@ -991,9 +973,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, creationDiags = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand Down Expand Up @@ -1134,9 +1113,6 @@ type internal BackgroundCompiler
Activity.Tags.userOpName, userOpName
|]

let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, _ = getOrCreateBuilder (options, userOpName)

match builderOpt with
Expand Down Expand Up @@ -1185,8 +1161,6 @@ type internal BackgroundCompiler
/// Parse and typecheck the whole project (the implementation, called recursively as project graph is evaluated)
member private _.ParseAndCheckProjectImpl(options, userOpName) =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! builderOpt, creationDiags = getOrCreateBuilder (options, userOpName)

Expand Down Expand Up @@ -1452,8 +1426,6 @@ type internal BackgroundCompiler
|]

async {
let! ct = Async.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! ct = Async.CancellationToken
// If there was a similar entry (as there normally will have been) then re-establish an empty builder . This
Expand Down
3 changes: 0 additions & 3 deletions src/Compiler/Service/FSharpCheckerResults.fs
Original file line number Diff line number Diff line change
Expand Up @@ -3585,9 +3585,6 @@ type FsiInteractiveChecker(legacyReferenceResolver, tcConfig: TcConfig, tcGlobal

member _.ParseAndCheckInteraction(sourceText: ISourceText, ?userOpName: string) =
cancellable {
let! ct = Cancellable.token ()
use _ = Cancellable.UsingToken(ct)

let userOpName = defaultArg userOpName "Unknown"
let fileName = Path.Combine(tcConfig.implicitIncludeDir, "stdin.fsx")
let suggestNamesForErrors = true // Will always be true, this is just for readability
Expand Down
34 changes: 0 additions & 34 deletions src/Compiler/Service/TransparentCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -1762,8 +1762,6 @@ type internal TransparentCompiler
node {
//use _ =
// Activity.start "ParseFile" [| Activity.Tags.fileName, fileName |> Path.GetFileName |]
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

// TODO: might need to deal with exceptions here:
let tcConfigB, sourceFileNames, _ = ComputeTcConfigBuilder projectSnapshot
Expand Down Expand Up @@ -1818,9 +1816,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<FSharpCheckFileAnswer> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot =
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
|> NodeCode.AwaitAsync
Expand All @@ -1842,9 +1837,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<FSharpCheckFileAnswer option> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot =
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
|> NodeCode.AwaitAsync
Expand Down Expand Up @@ -1897,9 +1889,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<seq<range>> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

ignore canInvalidateProject
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync

Expand All @@ -1914,9 +1903,6 @@ type internal TransparentCompiler

member this.GetAssemblyData(options: FSharpProjectOptions, fileName, userOpName: string) : NodeCode<ProjectAssemblyDataResult> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
return! this.GetAssemblyData(snapshot.ProjectSnapshot, fileName, userOpName)
}
Expand All @@ -1936,9 +1922,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<FSharpParseFileResults * FSharpCheckFileResults> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync

match! this.ParseAndCheckFileInProject(fileName, snapshot.ProjectSnapshot, userOpName) with
Expand All @@ -1953,9 +1936,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<FSharpParseFileResults> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
return! this.ParseFile(fileName, snapshot.ProjectSnapshot, userOpName)
}
Expand All @@ -1969,8 +1949,6 @@ type internal TransparentCompiler
) : NodeCode<(FSharpParseFileResults * FSharpCheckFileResults) option> =
node {
ignore builder
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot =
FSharpProjectSnapshot.FromOptions(options, fileName, 1, sourceText)
Expand Down Expand Up @@ -2023,9 +2001,6 @@ type internal TransparentCompiler
) : NodeCode<EditorServices.SemanticClassificationView option> =
node {
ignore userOpName
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
return! ComputeSemanticClassification(fileName, snapshot.ProjectSnapshot)
}
Expand All @@ -2048,9 +2023,6 @@ type internal TransparentCompiler
userOpName: string
) : NodeCode<FSharpParseFileResults * FSharpCheckFileAnswer> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

let! snapshot =
FSharpProjectSnapshot.FromOptions(options, fileName, fileVersion, sourceText)
|> NodeCode.AwaitAsync
Expand All @@ -2063,19 +2035,13 @@ type internal TransparentCompiler

member this.ParseAndCheckProject(options: FSharpProjectOptions, userOpName: string) : NodeCode<FSharpCheckProjectResults> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

ignore userOpName
let! snapshot = FSharpProjectSnapshot.FromOptions options |> NodeCode.AwaitAsync
return! ComputeParseAndCheckProject snapshot.ProjectSnapshot
}

member this.ParseAndCheckProject(projectSnapshot: FSharpProjectSnapshot, userOpName: string) : NodeCode<FSharpCheckProjectResults> =
node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

ignore userOpName
return! ComputeParseAndCheckProject projectSnapshot.ProjectSnapshot
}
Expand Down
6 changes: 0 additions & 6 deletions src/Compiler/Service/service.fs
Original file line number Diff line number Diff line change
Expand Up @@ -348,9 +348,6 @@ type FSharpChecker
use _ = Activity.start "FSharpChecker.Compile" [| Activity.Tags.userOpName, _userOpName |]

async {
let! ct = Async.CancellationToken
use _ = Cancellable.UsingToken(ct)

let ctok = CompilationThreadToken()
return CompileHelpers.compileFromArgs (ctok, argv, legacyReferenceResolver, None, None)
}
Expand Down Expand Up @@ -485,9 +482,6 @@ type FSharpChecker
let userOpName = defaultArg userOpName "Unknown"

node {
let! ct = NodeCode.CancellationToken
use _ = Cancellable.UsingToken(ct)

if fastCheck <> Some true || not captureIdentifiersWhenParsing then
return! backgroundCompiler.FindReferencesInFile(fileName, options, symbol, canInvalidateProject, userOpName)
else
Expand Down
Loading
Loading