Skip to content

Commit e11b320

Browse files
authored
Merge pull request #4993 from auduchinok/symbolCreate-symbolEnv
Don't create SymbolEnv repeatedly
2 parents 5403c52 + 1558a9f commit e11b320

File tree

4 files changed

+36
-24
lines changed

4 files changed

+36
-24
lines changed

src/fsharp/fsi/fsi.fs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1167,6 +1167,7 @@ type internal FsiDynamicCompiler
11671167
// Skip the "FSI_NNNN"
11681168
match contentFile.Declarations with
11691169
| [FSharpImplementationFileDeclaration.Entity (_eFakeModule,modDecls) ] ->
1170+
let cenv = SymbolEnv(newState.tcGlobals, newState.tcState.Ccu, Some newState.tcState.CcuSig, newState.tcImports)
11701171
for decl in modDecls do
11711172
match decl with
11721173
| FSharpImplementationFileDeclaration.MemberOrFunctionOrValue (v,_,_) ->
@@ -1177,16 +1178,16 @@ type internal FsiDynamicCompiler
11771178
| Item.Value vref ->
11781179
let optValue = newState.ilxGenerator.LookupGeneratedValue(valuePrinter.GetEvaluationContext(newState.emEnv), vref.Deref)
11791180
match optValue with
1180-
| Some (res, typ) -> Some(FsiValue(res, typ, FSharpType(tcGlobals, newState.tcState.Ccu, newState.tcState.CcuSig, newState.tcImports, vref.Type)))
1181+
| Some (res, typ) -> Some(FsiValue(res, typ, FSharpType(cenv, vref.Type)))
11811182
| None -> None
11821183
| _ -> None
11831184

1184-
let symbol = FSharpSymbol.Create(newState.tcGlobals, newState.tcState.Ccu, newState.tcState.CcuSig, newState.tcImports, v.Item)
1185+
let symbol = FSharpSymbol.Create(cenv, v.Item)
11851186
let symbolUse = FSharpSymbolUse(tcGlobals, newState.tcState.TcEnvFromImpls.DisplayEnv, symbol, ItemOccurence.Binding, v.DeclarationLocation)
11861187
fsi.TriggerEvaluation (fsiValueOpt, symbolUse, decl)
11871188
| FSharpImplementationFileDeclaration.Entity (e,_) ->
11881189
// Report a top-level module or namespace definition
1189-
let symbol = FSharpSymbol.Create(newState.tcGlobals, newState.tcState.Ccu, newState.tcState.CcuSig, newState.tcImports, e.Item)
1190+
let symbol = FSharpSymbol.Create(cenv, e.Item)
11901191
let symbolUse = FSharpSymbolUse(tcGlobals, newState.tcState.TcEnvFromImpls.DisplayEnv, symbol, ItemOccurence.Binding, e.DeclarationLocation)
11911192
fsi.TriggerEvaluation (None, symbolUse, decl)
11921193
| FSharpImplementationFileDeclaration.InitAction _ ->

src/fsharp/service/service.fs

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,7 @@ type TypeCheckInfo
183183
let amap = tcImports.GetImportMap()
184184
let infoReader = new InfoReader(g,amap)
185185
let ncenv = new NameResolver(g,amap,infoReader,NameResolution.FakeInstantiationGenerator)
186+
let cenv = SymbolEnv(g, thisCcu, Some ccuSigForFile, tcImports, amap, infoReader)
186187

187188
/// Find the most precise naming environment for the given line and column
188189
let GetBestEnvForPos cursorPos =
@@ -937,9 +938,9 @@ type TypeCheckInfo
937938
(fun () ->
938939
match GetDeclItemsForNamesAtPosition(ctok, parseResultsOpt, Some partialName.QualifyingIdents, Some partialName.PartialIdent, partialName.LastDotPos, line, lineStr, partialName.EndColumn + 1, ResolveTypeNamesToCtors, ResolveOverloads.Yes, getAllEntities, hasTextChangedSinceLastTypecheck) with
939940
| None -> FSharpDeclarationListInfo.Empty
940-
| Some (items, denv, ctx, m) ->
941+
| Some (items, denv, ctx, m) ->
941942
let items = if isInterfaceFile then items |> List.filter (fun x -> IsValidSignatureFileItem x.Item) else items
942-
let getAccessibility item = FSharpSymbol.GetAccessibility (FSharpSymbol.Create(g, thisCcu, ccuSigForFile, tcImports, item))
943+
let getAccessibility item = FSharpSymbol.GetAccessibility (FSharpSymbol.Create(cenv, item))
943944
let currentNamespaceOrModule =
944945
parseResultsOpt
945946
|> Option.bind (fun x -> x.ParseTree)
@@ -1010,15 +1011,14 @@ type TypeCheckInfo
10101011

10111012
items |> List.filter (fun (nm,items) -> not (isOpItem(nm,items)) && not(isFSharpList nm))
10121013

1013-
10141014
let items =
10151015
// Filter out duplicate names
10161016
items |> List.map (fun (_nm,itemsWithSameName) ->
10171017
match itemsWithSameName with
10181018
| [] -> failwith "Unexpected empty bag"
10191019
| items ->
10201020
items
1021-
|> List.map (fun item -> let symbol = FSharpSymbol.Create(g, thisCcu, ccuSigForFile, tcImports, item.Item)
1021+
|> List.map (fun item -> let symbol = FSharpSymbol.Create(cenv, item.Item)
10221022
FSharpSymbolUse(g, denv, symbol, ItemOccurence.Use, m)))
10231023

10241024
//end filtering
@@ -1134,7 +1134,7 @@ type TypeCheckInfo
11341134
| None | Some ([],_,_,_) -> None
11351135
| Some (items, denv, _, m) ->
11361136
let allItems = items |> List.collect (fun item -> SymbolHelpers.FlattenItems g m item.Item)
1137-
let symbols = allItems |> List.map (fun item -> FSharpSymbol.Create(g, thisCcu, ccuSigForFile, tcImports, item))
1137+
let symbols = allItems |> List.map (fun item -> FSharpSymbol.Create(cenv, item))
11381138
Some (symbols, denv, m)
11391139
)
11401140
(fun msg ->
@@ -1248,7 +1248,7 @@ type TypeCheckInfo
12481248
match GetDeclItemsForNamesAtPosition (ctok, None,Some(names), None, None, line, lineStr, colAtEndOfNames, ResolveTypeNamesToCtors, ResolveOverloads.Yes,(fun() -> []), fun _ -> false) with
12491249
| None | Some ([], _, _, _) -> None
12501250
| Some (item :: _, denv, _, m) ->
1251-
let symbol = FSharpSymbol.Create(g, thisCcu, ccuSigForFile, tcImports, item.Item)
1251+
let symbol = FSharpSymbol.Create(cenv, item.Item)
12521252
Some (symbol, denv, m)
12531253
)
12541254
(fun msg ->
@@ -1400,6 +1400,8 @@ type TypeCheckInfo
14001400
/// All open declarations in the file, including auto open modules
14011401
member __.OpenDeclarations = openDeclarations
14021402

1403+
member __.SymbolEnv = cenv
1404+
14031405
override __.ToString() = "TypeCheckInfo(" + mainInputFileName + ")"
14041406

14051407
type FSharpParsingOptions =
@@ -1887,11 +1889,12 @@ type FSharpCheckProjectResults(projectFileName:string, tcConfigOption, keepAssem
18871889
// Not, this does not have to be a SyncOp, it can be called from any thread
18881890
member __.GetAllUsesOfAllSymbols() =
18891891
let (tcGlobals, tcImports, thisCcu, ccuSig, tcSymbolUses, _topAttribs, _tcAssemblyData, _ilAssemRef, _ad, _tcAssemblyExpr, _dependencyFiles) = getDetails()
1892+
let cenv = SymbolEnv(tcGlobals, thisCcu, Some ccuSig, tcImports)
18901893

18911894
[| for r in tcSymbolUses do
18921895
for symbolUse in r.AllUsesOfSymbols do
18931896
if symbolUse.ItemOccurence <> ItemOccurence.RelatedText then
1894-
let symbol = FSharpSymbol.Create(tcGlobals, thisCcu, ccuSig, tcImports, symbolUse.Item)
1897+
let symbol = FSharpSymbol.Create(cenv, symbolUse.Item)
18951898
yield FSharpSymbolUse(tcGlobals, symbolUse.DisplayEnv, symbol, symbolUse.ItemOccurence, symbolUse.Range) |]
18961899
|> async.Return
18971900

@@ -2079,11 +2082,12 @@ type FSharpCheckFileResults(filename: string, errors: FSharpErrorInfo[], scopeOp
20792082

20802083
member info.GetAllUsesOfAllSymbolsInFile() =
20812084
threadSafeOp
2082-
(fun () -> [| |])
2083-
(fun scope ->
2084-
[| for symbolUse in scope.ScopeSymbolUses.AllUsesOfSymbols do
2085-
if symbolUse.ItemOccurence <> ItemOccurence.RelatedText then
2086-
let symbol = FSharpSymbol.Create(scope.TcGlobals, scope.ThisCcu, scope.CcuSigForFile, scope.TcImports, symbolUse.Item)
2085+
(fun () -> [| |])
2086+
(fun scope ->
2087+
let cenv = scope.SymbolEnv
2088+
[| for symbolUse in scope.ScopeSymbolUses.AllUsesOfSymbols do
2089+
if symbolUse.ItemOccurence <> ItemOccurence.RelatedText then
2090+
let symbol = FSharpSymbol.Create(cenv, symbolUse.Item)
20872091
yield FSharpSymbolUse(scope.TcGlobals, symbolUse.DisplayEnv, symbol, symbolUse.ItemOccurence, symbolUse.Range) |])
20882092
|> async.Return
20892093

@@ -2125,7 +2129,7 @@ type FSharpCheckFileResults(filename: string, errors: FSharpErrorInfo[], scopeOp
21252129
member info.OpenDeclarations =
21262130
scopeOptX
21272131
|> Option.map (fun scope ->
2128-
let cenv = SymbolEnv(scope.TcGlobals, scope.ThisCcu, Some scope.CcuSigForFile, scope.TcImports)
2132+
let cenv = scope.SymbolEnv
21292133
scope.OpenDeclarations |> Array.map (fun x -> FSharpOpenDeclaration(x.LongId, x.Range, (x.Modules |> List.map (fun x -> FSharpEntity(cenv, x))), x.AppliedScope, x.IsOwnNamespace)))
21302134
|> Option.defaultValue [| |]
21312135

src/fsharp/symbols/Symbols.fs

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,9 +49,13 @@ type FSharpAccessibility(a:Accessibility, ?isProtected) =
4949
let mangledTextOfCompPath (CompPath(scoref, path)) = getNameOfScopeRef scoref + "/" + textOfPath (List.map fst path)
5050
String.concat ";" (List.map mangledTextOfCompPath paths)
5151

52-
type SymbolEnv(g:TcGlobals, thisCcu: CcuThunk, thisCcuTyp: ModuleOrNamespaceType option, tcImports: TcImports) =
53-
let amapV = tcImports.GetImportMap()
54-
let infoReaderV = InfoReader(g, amapV)
52+
type SymbolEnv(g: TcGlobals, thisCcu: CcuThunk, thisCcuTyp: ModuleOrNamespaceType option, tcImports: TcImports, amapV: Import.ImportMap, infoReaderV: InfoReader) =
53+
54+
new(g: TcGlobals, thisCcu: CcuThunk, thisCcuTyp: ModuleOrNamespaceType option, tcImports: TcImports) =
55+
let amap = tcImports.GetImportMap()
56+
let infoReader = InfoReader(g, amap)
57+
SymbolEnv(g, thisCcu, thisCcuTyp, tcImports, amap, infoReader)
58+
5559
member __.g = g
5660
member __.amap = amapV
5761
member __.thisCcu = thisCcu
@@ -229,7 +233,7 @@ type FSharpSymbol(cenv: SymbolEnv, item: (unit -> Item), access: (FSharpSymbol -
229233
// TODO: there are several cases where we may need to report more interesting
230234
// symbol information below. By default we return a vanilla symbol.
231235
static member Create(g, thisCcu, thisCcuType, tcImports, item): FSharpSymbol =
232-
FSharpSymbol.Create (SymbolEnv(g, thisCcu, Some thisCcuType, tcImports), item)
236+
FSharpSymbol.Create(SymbolEnv(g, thisCcu, Some thisCcuType, tcImports), item)
233237

234238
static member Create(cenv, item): FSharpSymbol =
235239
let dflt() = FSharpSymbol(cenv, (fun () -> item), (fun _ _ _ -> true))

src/fsharp/symbols/Symbols.fsi

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ open System.Collections.Generic
66
open Microsoft.FSharp.Compiler
77
open Microsoft.FSharp.Compiler.AccessibilityLogic
88
open Microsoft.FSharp.Compiler.CompileOps
9+
open Microsoft.FSharp.Compiler.Import
10+
open Microsoft.FSharp.Compiler.InfoReader
911
open Microsoft.FSharp.Compiler.Range
1012
open Microsoft.FSharp.Compiler.Ast
1113
open Microsoft.FSharp.Compiler.Tast
@@ -14,8 +16,9 @@ open Microsoft.FSharp.Compiler.NameResolution
1416

1517
// Implementation details used by other code in the compiler
1618
type internal SymbolEnv =
17-
new : TcGlobals * thisCcu:CcuThunk * thisCcuTyp: ModuleOrNamespaceType option * tcImports: TcImports -> SymbolEnv
18-
member amap: Import.ImportMap
19+
new: TcGlobals * thisCcu:CcuThunk * thisCcuTyp: ModuleOrNamespaceType option * tcImports: TcImports -> SymbolEnv
20+
new: TcGlobals * thisCcu:CcuThunk * thisCcuTyp: ModuleOrNamespaceType option * tcImports: TcImports * amap: ImportMap * infoReader: InfoReader -> SymbolEnv
21+
member amap: ImportMap
1922
member g: TcGlobals
2023

2124
/// Indicates the accessibility of a symbol, as seen by the F# language
@@ -50,8 +53,8 @@ type [<Class>] public FSharpDisplayContext =
5053
/// FSharpField, FSharpGenericParameter, FSharpStaticParameter, FSharpMemberOrFunctionOrValue, FSharpParameter,
5154
/// or FSharpActivePatternCase.
5255
type [<Class>] public FSharpSymbol =
53-
/// Internal use only.
54-
static member internal Create : g:TcGlobals * thisCcu: CcuThunk * thisCcuTyp: ModuleOrNamespaceType * tcImports: TcImports * item:NameResolution.Item -> FSharpSymbol
56+
static member internal Create: g: TcGlobals * thisCcu: CcuThunk * thisCcuTyp: ModuleOrNamespaceType * tcImports: TcImports * item: NameResolution.Item -> FSharpSymbol
57+
static member internal Create: cenv: SymbolEnv * item: NameResolution.Item -> FSharpSymbol
5558

5659
/// Computes if the symbol is accessible for the given accessibility rights
5760
member IsAccessible: FSharpAccessibilityRights -> bool

0 commit comments

Comments
 (0)