Skip to content

Commit fe635db

Browse files
authored
Add GraphNode.FromResult (dotnet#14894)
* Add GraphNode.FromResult * fantomas
1 parent b87ca33 commit fe635db

5 files changed

Lines changed: 22 additions & 7 deletions

File tree

src/Compiler/Facilities/BuildGraph.fs

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,14 +228,14 @@ module GraphNode =
228228
| None -> ()
229229

230230
[<Sealed>]
231-
type GraphNode<'T>(retryCompute: bool, computation: NodeCode<'T>) =
231+
type GraphNode<'T> private (retryCompute: bool, computation: NodeCode<'T>, cachedResult: Task<'T>, cachedResultNode: NodeCode<'T>) =
232232

233233
let gate = obj ()
234234
let mutable computation = computation
235235
let mutable requestCount = 0
236236

237-
let mutable cachedResult: Task<'T> = Unchecked.defaultof<_>
238-
let mutable cachedResultNode: NodeCode<'T> = Unchecked.defaultof<_>
237+
let mutable cachedResult: Task<'T> = cachedResult
238+
let mutable cachedResultNode: NodeCode<'T> = cachedResultNode
239239

240240
let isCachedResultNodeNotNull () =
241241
not (obj.ReferenceEquals(cachedResultNode, null))
@@ -429,4 +429,9 @@ type GraphNode<'T>(retryCompute: bool, computation: NodeCode<'T>) =
429429

430430
member _.IsComputing = requestCount > 0
431431

432+
static member FromResult(result: 'T) =
433+
let nodeResult = node.Return result
434+
GraphNode(true, nodeResult, Task.FromResult(result), nodeResult)
435+
436+
new(retryCompute: bool, computation) = GraphNode(retryCompute, computation, Unchecked.defaultof<_>, Unchecked.defaultof<_>)
432437
new(computation) = GraphNode(retryCompute = true, computation = computation)

src/Compiler/Facilities/BuildGraph.fsi

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -102,6 +102,9 @@ type internal GraphNode<'T> =
102102
/// By default, 'retryCompute' is 'true'.
103103
new: computation: NodeCode<'T> -> GraphNode<'T>
104104

105+
/// Creates a GraphNode with given result already cached.
106+
static member FromResult: 'T -> GraphNode<'T>
107+
105108
/// Return NodeCode which, when executed, will get the value of the computation if already computed, or
106109
/// await an existing in-progress computation for the node if one exists, or else will synchronously
107110
/// start the computation on the current thread.

src/Compiler/Service/IncrementalBuild.fs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,7 +275,7 @@ type TcInfoNode =
275275
static member FromState(state: TcInfoState) =
276276
let tcInfo = state.TcInfo
277277
let tcInfoExtras = state.TcInfoExtras
278-
TcInfoNode(GraphNode(node.Return tcInfo), GraphNode(node.Return (tcInfo, defaultArg tcInfoExtras emptyTcInfoExtras)))
278+
TcInfoNode(GraphNode.FromResult tcInfo, GraphNode.FromResult (tcInfo, defaultArg tcInfoExtras emptyTcInfoExtras))
279279

280280
/// Bound model of an underlying syntax and typed tree.
281281
[<Sealed>]
@@ -1098,7 +1098,7 @@ module IncrementalBuilderStateHelpers =
10981098
| ValueSome(boundModel) when initialState.enablePartialTypeChecking && boundModel.BackingSignature.IsSome ->
10991099
let newBoundModel = boundModel.ClearTcInfoExtras()
11001100
{ state with
1101-
boundModels = state.boundModels.SetItem(slot, GraphNode(node.Return newBoundModel))
1101+
boundModels = state.boundModels.SetItem(slot, GraphNode.FromResult newBoundModel)
11021102
stampedFileNames = state.stampedFileNames.SetItem(slot, stamp)
11031103
}
11041104
| _ ->
@@ -1165,7 +1165,7 @@ type IncrementalBuilderState with
11651165
let referencedAssemblies = initialState.referencedAssemblies
11661166

11671167
let cache = TimeStampCache(defaultTimeStamp)
1168-
let initialBoundModel = GraphNode(node.Return initialBoundModel)
1168+
let initialBoundModel = GraphNode.FromResult initialBoundModel
11691169
let boundModels = ImmutableArrayBuilder.create fileNames.Length
11701170

11711171
for slot = 0 to fileNames.Length - 1 do

src/Compiler/Service/service.fs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -410,7 +410,7 @@ type BackgroundCompiler
410410
let createBuilderNode (options, userOpName, ct: CancellationToken) =
411411
lock gate (fun () ->
412412
if ct.IsCancellationRequested then
413-
GraphNode(node.Return(None, [||]))
413+
GraphNode.FromResult(None, [||])
414414
else
415415
let getBuilderNode = GraphNode(CreateOneIncrementalBuilder(options, userOpName))
416416
incrementalBuildersCache.Set(AnyCallerThread, options, getBuilderNode)

tests/FSharp.Compiler.UnitTests/BuildGraphTests.fs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -272,3 +272,10 @@ module BuildGraphTests =
272272
tasks
273273
|> Seq.iter (fun x ->
274274
try x.Wait(1000) |> ignore with | :? TimeoutException -> reraise() | _ -> ())
275+
276+
[<Fact>]
277+
let ``GraphNode created from an already computed result will return it in tryPeekValue`` () =
278+
let graphNode = GraphNode.FromResult 1
279+
280+
Assert.shouldBeTrue graphNode.HasValue
281+
Assert.shouldBe (ValueSome 1) (graphNode.TryPeekValue())

0 commit comments

Comments
 (0)