From bf626ddd530b6d43125c0cccad0ba79870e1263e Mon Sep 17 00:00:00 2001 From: nikiforovall Date: Tue, 13 Aug 2024 13:03:45 +0300 Subject: [PATCH] feat: add caching [NOT WORKING] --- src/Dependify.Core/MsBuildService.cs | 72 ++++++++++++++++++++++---- src/Dependify.Core/SolutionRegistry.cs | 10 +++- 2 files changed, 70 insertions(+), 12 deletions(-) diff --git a/src/Dependify.Core/MsBuildService.cs b/src/Dependify.Core/MsBuildService.cs index 0d118a2..3e18e0c 100644 --- a/src/Dependify.Core/MsBuildService.cs +++ b/src/Dependify.Core/MsBuildService.cs @@ -23,6 +23,24 @@ public MsBuildService(ILogger logger, ILoggerFactory loggerFacto } public DependencyGraph AnalyzeReferences(SolutionReferenceNode solution, MsBuildConfig config) + { + return this.AnalyzeReferencesCore(solution, config, null); + } + + public DependencyGraph AnalyzeReferencesWithCache( + SolutionReferenceNode solution, + MsBuildConfig config, + DependencyGraph cache + ) + { + return this.AnalyzeReferencesCore(solution, config, cache); + } + + private DependencyGraph AnalyzeReferencesCore( + SolutionReferenceNode solution, + MsBuildConfig config, + DependencyGraph? cache + ) { this.logger.LogInformation("Analyzing solution {Solution}", solution.Path); this.subject.OnNext(new NodeEvent(NodeEventType.SolutionLoading, solution.Id, solution.Path)); @@ -40,10 +58,18 @@ public DependencyGraph AnalyzeReferences(SolutionReferenceNode solution, MsBuild foreach (var project in projects) { - var projectNode = new ProjectReferenceNode(project.Key); - builder.WithEdge(new Edge(builder.Root, projectNode)); + var projectNode = new ProjectReferenceNode(project.Value.ProjectFile.Path); - this.AddDependenciesToGraph(builder, project.Value, projectNode, config); + if (cache is not null && cache.Nodes.Contains(projectNode)) + { + this.CopyFromCache(builder, cache, projectNode); + } + else + { + builder.WithEdge(new Edge(builder.Root, projectNode)); + + this.AddDependenciesToGraph(builder, project.Value, projectNode, config); + } } if (config.FullScan) @@ -53,7 +79,7 @@ public DependencyGraph AnalyzeReferences(SolutionReferenceNode solution, MsBuild { nodesToScan = builder.GetNotScannedNodes().OfType().ToList(); - this.AnalyzeReferencesCore(builder, nodesToScan, config); + this.AnalyzeReferencesCore(builder, nodesToScan, config, cache); } while (nodesToScan.Count > 0); } @@ -67,7 +93,7 @@ public DependencyGraph AnalyzeReferences(ProjectReferenceNode node, MsBuildConfi { var builder = new DependencyGraph.Builder(node); - this.AnalyzeReferencesCore(builder, [node], config); + this.AnalyzeReferencesCore(builder, [node], config, null); return builder.Build(); } @@ -76,7 +102,7 @@ public DependencyGraph AnalyzeReferences(IEnumerable nodes { var builder = new DependencyGraph.Builder(new SolutionReferenceNode()); - this.AnalyzeReferencesCore(builder, nodes, config); + this.AnalyzeReferencesCore(builder, nodes, config, null); return builder.Build(); } @@ -84,7 +110,8 @@ public DependencyGraph AnalyzeReferences(IEnumerable nodes private void AnalyzeReferencesCore( DependencyGraph.Builder builder, IEnumerable nodes, - MsBuildConfig config + MsBuildConfig config, + DependencyGraph? cache ) { if (!nodes.Any()) @@ -97,9 +124,17 @@ MsBuildConfig config foreach (var path in nodes.Select(n => n.Path)) { var projectNode = new ProjectReferenceNode(path); - var project = analyzerManager.GetProject(path); - this.AddDependenciesToGraph(builder, project, projectNode, config); + if (cache is not null && cache.Nodes.Contains(projectNode)) + { + this.CopyFromCache(builder, cache, projectNode); + } + else + { + var project = analyzerManager.GetProject(path); + + this.AddDependenciesToGraph(builder, project, projectNode, config); + } } if (config.FullScan) @@ -109,11 +144,28 @@ MsBuildConfig config { nodesToScan = builder.GetNotScannedNodes().OfType().ToList(); - this.AnalyzeReferencesCore(builder, nodesToScan, config); + this.AnalyzeReferencesCore(builder, nodesToScan, config, cache); } while (nodesToScan.Count > 0); } } + private void CopyFromCache(DependencyGraph.Builder builder, DependencyGraph cache, ProjectReferenceNode projectNode) + { + var subgraph = cache.SubGraph(projectNode); + + this.logger.LogInformation("Loaded project {Project} from cache", projectNode.Path); + + foreach (var node in subgraph.Nodes) + { + builder.WithNode(node, scanned: true); + } + + foreach (var edge in subgraph.Edges) + { + builder.WithEdge(edge); + } + } + private void AddDependenciesToGraph( DependencyGraph.Builder builder, IProjectAnalyzer projectAnalyzer, diff --git a/src/Dependify.Core/SolutionRegistry.cs b/src/Dependify.Core/SolutionRegistry.cs index 09934be..b63a147 100644 --- a/src/Dependify.Core/SolutionRegistry.cs +++ b/src/Dependify.Core/SolutionRegistry.cs @@ -46,7 +46,7 @@ public void LoadRegistry() public Task LoadSolutionsAsync(MsBuildConfig msBuildConfig, CancellationToken cancellationToken = default) { - this.IsLoaded = false; + this.ResetCache(); lock (LockObject) { @@ -61,7 +61,7 @@ public Task LoadSolutionsAsync(MsBuildConfig msBuildConfig, CancellationToken ca this.Nodes.OfType().ToList(), msBuildConfig ) - : this.buildService.AnalyzeReferences(solution, msBuildConfig); + : this.buildService.AnalyzeReferencesWithCache(solution, msBuildConfig, this.GetFullGraph()); this.solutionGraphs[solution] = dependencyGraph; @@ -131,6 +131,12 @@ public DependencyGraph GetFullGraph() return builder.Build(); } + + private void ResetCache() + { + this.IsLoaded = false; + this.solutionGraphs.Clear(); + } } public record NodeUsage(