Skip to content

Commit 72a8241

Browse files
committed
refactor: rename NewAnalyzer to New and update ClearCache method for improved memory management
1 parent 2994789 commit 72a8241

2 files changed

Lines changed: 23 additions & 8 deletions

File tree

v2/graph/analyzer/analyzer.go

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package analyzer
22

33
import (
4+
"runtime"
45
"sync"
56

67
"github.com/elecbug/netkit/v2/graph"
@@ -16,8 +17,8 @@ type Analyzer struct {
1617
cfg *Config // cfg holds configuration options for the analyzer, such as worker counts and normalization settings.
1718
}
1819

19-
// NewAnalyzer creates a new Analyzer instance based on the provided graph.
20-
func NewAnalyzer(g *graph.Graph, parallelCoreCount int, cfg *Config) *Analyzer {
20+
// New creates a new Analyzer instance based on the provided graph.
21+
func New(g *graph.Graph, parallelCoreCount int, cfg *Config) *Analyzer {
2122
return &Analyzer{
2223
baseGraph: g,
2324
graphHash: "",
@@ -27,6 +28,20 @@ func NewAnalyzer(g *graph.Graph, parallelCoreCount int, cfg *Config) *Analyzer {
2728
}
2829
}
2930

31+
// ClearCache clears the cached shortest paths and resets the graph hash. This should be called when the underlying graph
32+
// changes to ensure that subsequent shortest path computations are accurate and not based on stale data.
33+
func (a *Analyzer) ClearCache() string {
34+
a.mu.Lock()
35+
defer a.mu.Unlock()
36+
37+
a.allShortestPaths = make(map[graph.NodeID]map[graph.NodeID][]graph.Path)
38+
a.graphHash = ""
39+
40+
runtime.GC() // Trigger garbage collection to free memory used by the old cache
41+
42+
return a.graphHash
43+
}
44+
3045
// Graph returns the base graph associated with the analyzer.
3146
func (a *Analyzer) Graph() *graph.Graph {
3247
return a.baseGraph

v2/graph/analyzer/analyzer_test.go

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ func testComputeShortestPath(t *testing.T) {
3636
g.AddEdge("A", "C", graph.NewWeight(2))
3737
g.AddEdge("C", "D", graph.NewWeight(1))
3838

39-
a := analyzer.NewAnalyzer(g, 1, analyzer.DefaultConfig())
39+
a := analyzer.New(g, 1, analyzer.DefaultConfig())
4040

4141
paths, err := a.ShortestPaths("A", "D")
4242
if err != nil {
@@ -106,7 +106,7 @@ func TestPerformance(t *testing.T) {
106106
t.Fatalf("failed to create graph: %v", err)
107107
}
108108

109-
a := analyzer.NewAnalyzer(g, 1, analyzer.DefaultConfig())
109+
a := analyzer.New(g, 1, analyzer.DefaultConfig())
110110

111111
startTime := time.Now()
112112
paths, err := a.ShortestPaths("0", "999")
@@ -121,7 +121,7 @@ func TestPerformance(t *testing.T) {
121121
duration := time.Since(startTime)
122122
fmt.Printf(" - Time taken to compute shortest paths: %v\n", duration)
123123

124-
a = analyzer.NewAnalyzer(g, 4, analyzer.DefaultConfig())
124+
a = analyzer.New(g, 4, analyzer.DefaultConfig())
125125

126126
startTime = time.Now()
127127
pathsCompared, err := a.ShortestPaths("0", "999")
@@ -135,7 +135,7 @@ func TestPerformance(t *testing.T) {
135135
t.Errorf("expected total distance %v, got %v", paths[0].TotalDistance(), pathsCompared[0].TotalDistance())
136136
}
137137

138-
a = analyzer.NewAnalyzer(g, 16, analyzer.DefaultConfig())
138+
a = analyzer.New(g, 16, analyzer.DefaultConfig())
139139

140140
startTime = time.Now()
141141
pathsCompared, err = a.ShortestPaths("0", "999")
@@ -149,7 +149,7 @@ func TestPerformance(t *testing.T) {
149149
t.Errorf("expected total distance %v, got %v", paths[0].TotalDistance(), pathsCompared[0].TotalDistance())
150150
}
151151

152-
a = analyzer.NewAnalyzer(g, 32, analyzer.DefaultConfig())
152+
a = analyzer.New(g, 32, analyzer.DefaultConfig())
153153

154154
startTime = time.Now()
155155
pathsCompared, err = a.ShortestPaths("0", "999")
@@ -204,7 +204,7 @@ func TestAnaylzer(t *testing.T) {
204204
}
205205

206206
cfg := analyzer.DefaultConfig()
207-
a := analyzer.NewAnalyzer(g, 16, cfg)
207+
a := analyzer.New(g, 16, cfg)
208208

209209
t.Run("ShortestPaths", func(t *testing.T) {
210210
results["shortest_paths"] = make(map[string]any)

0 commit comments

Comments
 (0)