Skip to content

Commit

Permalink
Use TransparentCompiler cache for GetProjectOptionsFromScript (dotnet…
Browse files Browse the repository at this point in the history
  • Loading branch information
nojaf committed Mar 18, 2024
1 parent 487399f commit bd9c28f
Show file tree
Hide file tree
Showing 6 changed files with 40 additions and 13 deletions.
2 changes: 2 additions & 0 deletions src/Compiler/Facilities/AsyncMemoize.fs
Original file line number Diff line number Diff line change
Expand Up @@ -551,6 +551,8 @@ type internal AsyncMemoize<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'T

member this.OnEvent = this.Event.Add

member this.Count = cache.Count

member _.Locked = lock.Semaphore.CurrentCount < 1

member _.Running =
Expand Down
2 changes: 2 additions & 0 deletions src/Compiler/Facilities/AsyncMemoize.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ type internal AsyncMemoize<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'T

member OnEvent: ((JobEvent * (string * 'TKey * 'TVersion) -> unit) -> unit)

member Count: int

/// A drop-in replacement for AsyncMemoize that disables caching and just runs the computation every time.
type internal AsyncMemoizeDisabled<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'TVersion: equality> =

Expand Down
34 changes: 21 additions & 13 deletions src/Compiler/Service/TransparentCompiler.fs
Original file line number Diff line number Diff line change
Expand Up @@ -2135,19 +2135,27 @@ type internal TransparentCompiler
optionsStamp: int64 option,
userOpName: string
) : Async<FSharpProjectOptions * FSharpDiagnostic list> =
backgroundCompiler.GetProjectOptionsFromScript(
fileName,
sourceText,
previewEnabled,
loadedTimeStamp,
otherFlags,
useFsiAuxLib,
useSdkRefs,
sdkDirOverride,
assumeDotNetFramework,
optionsStamp,
userOpName
)
async {
let bc = this :> IBackgroundCompiler

let! snapshot, diagnostics =
bc.GetProjectSnapshotFromScript(
fileName,
SourceTextNew.ofISourceText sourceText,
previewEnabled,
loadedTimeStamp,
otherFlags,
useFsiAuxLib,
useSdkRefs,
sdkDirOverride,
assumeDotNetFramework,
optionsStamp,
userOpName
)

let projectOptions = snapshot.ToOptions()
return projectOptions, diagnostics
}

member this.GetProjectSnapshotFromScript
(
Expand Down
2 changes: 2 additions & 0 deletions src/Compiler/Utilities/LruCache.fs
Original file line number Diff line number Diff line change
Expand Up @@ -277,3 +277,5 @@ type internal LruCache<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'TVers
match w.TryGetTarget() with
| true, value -> Some(label, version, value)
| _ -> None)

member this.Count = Seq.length (this.GetValues())
3 changes: 3 additions & 0 deletions src/Compiler/Utilities/LruCache.fsi
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,9 @@ type internal LruCache<'TKey, 'TVersion, 'TValue when 'TKey: equality and 'TVers

member GetValues: unit -> (string * 'TVersion * 'TValue) seq

/// Gets the number of items in the cache
member Count: int

member Remove: key: 'TKey -> unit

member Remove: key: 'TKey * version: 'TVersion -> unit
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1010,3 +1010,13 @@ printfn "Hello from F#"
ProjectWorkflowBuilder(project, useTransparentCompiler = useTransparentCompiler) {
checkFile "As 01" expectTwoWarnings
}

[<Fact>]
let ``Transparent Compiler ScriptClosure cache is populated after GetProjectOptionsFromScript`` () =
async {
let transparentChecker = FSharpChecker.Create(useTransparentCompiler = true)
let scriptName = Path.Combine(__SOURCE_DIRECTORY__, "script.fsx")
let content = SourceTextNew.ofString ""
let! _ = transparentChecker.GetProjectOptionsFromScript(scriptName, content)
Assert.Equal(1, transparentChecker.Caches.ScriptClosure.Count)
}

0 comments on commit bd9c28f

Please sign in to comment.