Skip to content

Commit 6333f49

Browse files
CopilotT-Gro
andcommitted
Add MethodOverloadsCache language feature (preview only)
Co-authored-by: T-Gro <46543583+T-Gro@users.noreply.github.com>
1 parent c909230 commit 6333f49

File tree

5 files changed

+32
-21
lines changed

5 files changed

+32
-21
lines changed

src/Compiler/Checking/ConstraintSolver.fs

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3644,11 +3644,13 @@ and ResolveOverloading
36443644

36453645
// Try to use cached overload resolution result for repetitive patterns
36463646
// Only cache when:
3647+
// - Language feature is enabled
36473648
// - NOT doing op_Explicit/op_Implicit conversions
36483649
// - NOT doing trait constraint (SRTP) resolution (cx is None)
36493650
// - Have multiple candidates
36503651
let cacheKeyOpt =
3651-
if not isOpConversion && cx.IsNone && candidates.Length > 1 then
3652+
if g.langVersion.SupportsFeature LanguageFeature.MethodOverloadsCache &&
3653+
not isOpConversion && cx.IsNone && candidates.Length > 1 then
36523654
tryComputeOverloadCacheKey g calledMethGroup callerArgs reqdRetTyOpt
36533655
else
36543656
ValueNone

src/Compiler/Checking/OverloadResolutionCache.fs

Lines changed: 24 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -237,25 +237,29 @@ let storeCacheResult
237237
(anyHasOutArgs: bool)
238238
(calledMethOpt: CalledMeth<'T> voption)
239239
=
240-
match cacheKeyOpt with
241-
| ValueSome cacheKey ->
242-
match computeCacheResult calledMethGroup calledMethOpt with
243-
| Some res ->
244-
// Store under the "before" key
245-
cache.TryAdd(cacheKey, res) |> ignore
246-
247-
// Compute "after" key - types may have been solved during resolution
248-
// If different from "before" key, store under that too for future hits
249-
match tryComputeOverloadCacheKey g calledMethGroup callerArgs reqdRetTyOpt anyHasOutArgs with
250-
| ValueSome afterKey when afterKey <> cacheKey -> cache.TryAdd(afterKey, res) |> ignore
251-
| _ -> ()
252-
| None -> ()
253-
| ValueNone ->
254-
// Even if we couldn't compute a "before" key (unstable types),
255-
// try to compute an "after" key now that types may be solved
256-
match tryComputeOverloadCacheKey g calledMethGroup callerArgs reqdRetTyOpt anyHasOutArgs with
257-
| ValueSome afterKey ->
240+
// Only store in cache if the language feature is enabled
241+
if not (g.langVersion.SupportsFeature LanguageFeature.MethodOverloadsCache) then
242+
()
243+
else
244+
match cacheKeyOpt with
245+
| ValueSome cacheKey ->
258246
match computeCacheResult calledMethGroup calledMethOpt with
259-
| Some res -> cache.TryAdd(afterKey, res) |> ignore
247+
| Some res ->
248+
// Store under the "before" key
249+
cache.TryAdd(cacheKey, res) |> ignore
250+
251+
// Compute "after" key - types may have been solved during resolution
252+
// If different from "before" key, store under that too for future hits
253+
match tryComputeOverloadCacheKey g calledMethGroup callerArgs reqdRetTyOpt anyHasOutArgs with
254+
| ValueSome afterKey when afterKey <> cacheKey -> cache.TryAdd(afterKey, res) |> ignore
255+
| _ -> ()
260256
| None -> ()
261-
| ValueNone -> ()
257+
| ValueNone ->
258+
// Even if we couldn't compute a "before" key (unstable types),
259+
// try to compute an "after" key now that types may be solved
260+
match tryComputeOverloadCacheKey g calledMethGroup callerArgs reqdRetTyOpt anyHasOutArgs with
261+
| ValueSome afterKey ->
262+
match computeCacheResult calledMethGroup calledMethOpt with
263+
| Some res -> cache.TryAdd(afterKey, res) |> ignore
264+
| None -> ()
265+
| ValueNone -> ()

src/Compiler/FSComp.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1803,5 +1803,6 @@ featureAllowLetOrUseBangTypeAnnotationWithoutParens,"Allow let! and use! type an
18031803
3878,tcAttributeIsNotValidForUnionCaseWithFields,"This attribute is not valid for use on union cases with fields."
18041804
3879,xmlDocNotFirstOnLine,"XML documentation comments should be the first non-whitespace text on a line."
18051805
featureReturnFromFinal,"Support for ReturnFromFinal/YieldFromFinal in computation expressions to enable tailcall optimization when available on the builder."
1806+
featureMethodOverloadsCache,"Support for caching method overload resolution results for improved compilation performance."
18061807
3880,optsLangVersionOutOfSupport,"Language version '%s' is out of support. The last .NET SDK supporting it is available at https://dotnet.microsoft.com/en-us/download/dotnet/%s"
18071808
3881,optsUnrecognizedLanguageFeature,"Unrecognized language feature name: '%s'. Use a valid feature name such as 'NameOf' or 'StringInterpolation'."

src/Compiler/Facilities/LanguageFeatures.fs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,7 @@ type LanguageFeature =
104104
| ErrorOnInvalidDeclsInTypeDefinitions
105105
| AllowTypedLetUseAndBang
106106
| ReturnFromFinal
107+
| MethodOverloadsCache
107108

108109
/// LanguageVersion management
109110
type LanguageVersion(versionText, ?disabledFeaturesArray: LanguageFeature array) =
@@ -251,6 +252,7 @@ type LanguageVersion(versionText, ?disabledFeaturesArray: LanguageFeature array)
251252

252253
// F# preview (still preview in 10.0)
253254
LanguageFeature.FromEndSlicing, previewVersion // Unfinished features --- needs work
255+
LanguageFeature.MethodOverloadsCache, previewVersion // Performance optimization for overload resolution
254256
]
255257

256258
static let defaultLanguageVersion = LanguageVersion("default")
@@ -440,6 +442,7 @@ type LanguageVersion(versionText, ?disabledFeaturesArray: LanguageFeature array)
440442
| LanguageFeature.ErrorOnInvalidDeclsInTypeDefinitions -> FSComp.SR.featureErrorOnInvalidDeclsInTypeDefinitions ()
441443
| LanguageFeature.AllowTypedLetUseAndBang -> FSComp.SR.featureAllowLetOrUseBangTypeAnnotationWithoutParens ()
442444
| LanguageFeature.ReturnFromFinal -> FSComp.SR.featureReturnFromFinal ()
445+
| LanguageFeature.MethodOverloadsCache -> FSComp.SR.featureMethodOverloadsCache ()
443446

444447
/// Get a version string associated with the given feature.
445448
static member GetFeatureVersionString feature =

src/Compiler/Facilities/LanguageFeatures.fsi

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ type LanguageFeature =
9595
| ErrorOnInvalidDeclsInTypeDefinitions
9696
| AllowTypedLetUseAndBang
9797
| ReturnFromFinal
98+
| MethodOverloadsCache
9899

99100
/// LanguageVersion management
100101
type LanguageVersion =

0 commit comments

Comments
 (0)