Skip to content

Commit 670a310

Browse files
CopilotT-Gro
andcommitted
Fix MergeWith to actually inject precomputed cache instead of reconstructing
Addressed review comment: Previous MergeWith computed merged entity map but then used vanilla constructor which rebuilt all caches, defeating the optimization. Changes: - Added internal constructor accepting precomputed cache (NameMap<Entity> option) - MergeWith now injects precomputed allEntitiesByLogicalMangledNameCache - SetLogicalMangledNameCache method properly sets the cache field - Fast path: Precomputed cache injected, avoids O(n) rebuild on first access - Conflict path: Cache still computed and injected Build: ✅ Success (0 errors, 0 warnings, 3m 36s) This actually achieves the incremental merge benefit by preserving computed work instead of reconstructing everything from scratch. Co-authored-by: T-Gro <[email protected]>
1 parent 0507d8d commit 670a310

File tree

1 file changed

+16
-8
lines changed

1 file changed

+16
-8
lines changed

src/Compiler/TypedTree/TypedTree.fs

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2015,6 +2015,19 @@ type ModuleOrNamespaceType(kind: ModuleOrNamespaceKind, vals: CachedDList<Val>,
20152015
let mutable allValsAndMembersByPartialLinkageKeyCache: MultiMap<ValLinkagePartialKey, Val> option = None
20162016

20172017
let mutable allValsByLogicalNameCache: NameMap<Val> option = None
2018+
2019+
/// Internal constructor that allows injecting precomputed cache values for incremental merge optimization
2020+
internal new(kind: ModuleOrNamespaceKind, vals: CachedDList<Val>, entities: CachedDList<Entity>,
2021+
precomputedLogicalNameCache: NameMap<Entity> option) as this =
2022+
ModuleOrNamespaceType(kind, vals, entities)
2023+
then
2024+
match precomputedLogicalNameCache with
2025+
| Some cache -> this.SetLogicalMangledNameCache(cache)
2026+
| None -> ()
2027+
2028+
/// Internal method to inject precomputed cache (used by incremental merge)
2029+
member private this.SetLogicalMangledNameCache(cache: NameMap<Entity>) =
2030+
allEntitiesByLogicalMangledNameCache <- Some cache
20182031

20192032
/// Namespace or module-compiled-as-type?
20202033
member _.ModuleOrNamespaceKind = kind
@@ -2223,14 +2236,9 @@ type ModuleOrNamespaceType(kind: ModuleOrNamespaceKind, vals: CachedDList<Val>,
22232236
// Merge vals (simple append, already O(1) with CachedDList)
22242237
let mergedVals = CachedDList.append mty1.AllValsAndMembers mty2.AllValsAndMembers
22252238

2226-
// Create new ModuleOrNamespaceType
2227-
let result = ModuleOrNamespaceType(kind, mergedVals, mergedEntities)
2228-
2229-
// Note: The merged entity map cache (if computed in fast path) will be rebuilt
2230-
// on first access via the normal caching mechanism. Future optimization could
2231-
// inject the precomputed map to avoid recomputation.
2232-
// For now: mergedEntityMap is computed but not used - cache will be lazy
2233-
ignore mergedEntityMap
2239+
// Create new ModuleOrNamespaceType with precomputed cache injection
2240+
// This avoids O(n) rebuild of AllEntitiesByLogicalMangledName on first access
2241+
let result = ModuleOrNamespaceType(kind, mergedVals, mergedEntities, mergedEntityMap)
22342242

22352243
result
22362244

0 commit comments

Comments
 (0)