Skip to content

Commit 2adfa41

Browse files
committed
review feedback
Signed-off-by: Huamin Chen <[email protected]>
1 parent 394f1ca commit 2adfa41

File tree

3 files changed

+28
-13
lines changed

3 files changed

+28
-13
lines changed

src/semantic-router/pkg/cache/comprehensive_benchmark_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,8 +33,8 @@ func (c ContentLength) String() string {
3333
// GenerateQuery generates a query with maximum semantic diversity using hash-based randomization
3434
func generateQuery(length ContentLength, index int) string {
3535
// Hash the index to get pseudo-random values (deterministic but well-distributed)
36-
hash := uint64(index) // #nosec G115 -- index is always positive and bounded
37-
hash = hash*2654435761 + 1013904223 // Knuth's multiplicative hash
36+
hash := uint64(index) // #nosec G115 -- index is always positive and bounded
37+
hash = hash * 2654435761 // Knuth's multiplicative hash
3838

3939
// Expanded templates for maximum diversity
4040
templates := []string{

src/semantic-router/pkg/cache/hybrid_cache.go

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,14 @@ import (
1515
"github.com/vllm-project/semantic-router/src/semantic-router/pkg/observability"
1616
)
1717

18+
const (
19+
// Buffer pool limits to prevent memory bloat
20+
maxVisitedMapSize = 1000 // Maximum size for visited map before discarding buffer
21+
maxCandidatesCapacity = 200 // Maximum capacity for candidates heap before discarding buffer
22+
maxResultsCapacity = 200 // Maximum capacity for results heap before discarding buffer
23+
maxHNSWLayers = 16 // Maximum number of layers in HNSW index
24+
)
25+
1826
// searchBuffers holds reusable buffers for HNSW search to reduce GC pressure
1927
type searchBuffers struct {
2028
visited map[int]bool
@@ -48,7 +56,7 @@ func getSearchBuffers() *searchBuffers {
4856
// putSearchBuffers returns buffers to pool
4957
func putSearchBuffers(buf *searchBuffers) {
5058
// Don't return to pool if buffers grew too large (avoid memory bloat)
51-
if len(buf.visited) > 1000 || cap(buf.candidates.data) > 200 || cap(buf.results.data) > 200 {
59+
if len(buf.visited) > maxVisitedMapSize || cap(buf.candidates.data) > maxCandidatesCapacity || cap(buf.results.data) > maxResultsCapacity {
5260
return
5361
}
5462
searchBufferPool.Put(buf)
@@ -782,7 +790,7 @@ func (h *HybridCache) selectLevelHybrid() int {
782790
// Use exponential decay to select level
783791
// Most nodes at layer 0, fewer at higher layers
784792
level := 0
785-
for level < 16 { // Max 16 layers
793+
for level < maxHNSWLayers {
786794
if randFloat() > h.hnswIndex.ml {
787795
break
788796
}

src/semantic-router/pkg/cache/hybrid_vs_milvus_benchmark_test.go

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -92,14 +92,15 @@ func (dcc *DatabaseCallCounter) Reset() {
9292

9393
// getMilvusConfigPath returns the path to milvus.yaml config file
9494
func getMilvusConfigPath() string {
95-
// Try absolute path first (for direct test execution)
96-
configPath := "/home/ubuntu/rootfs/back/semantic-router.bak/config/cache/milvus.yaml"
97-
if _, err := os.Stat(configPath); err == nil {
98-
return configPath
95+
// Check for environment variable first
96+
if envPath := os.Getenv("MILVUS_CONFIG_PATH"); envPath != "" {
97+
if _, err := os.Stat(envPath); err == nil {
98+
return envPath
99+
}
99100
}
100101

101102
// Try relative from project root (when run via make)
102-
configPath = "config/cache/milvus.yaml"
103+
configPath := "config/cache/milvus.yaml"
103104
if _, err := os.Stat(configPath); err == nil {
104105
return configPath
105106
}
@@ -126,10 +127,16 @@ func BenchmarkHybridVsMilvus(b *testing.B) {
126127
}
127128

128129
// CSV output file - save to project benchmark_results directory
129-
// Determine project root by walking up from test directory
130-
projectRoot := "/home/ubuntu/rootfs/back/semantic-router.bak"
131-
if envRoot := os.Getenv("PROJECT_ROOT"); envRoot != "" {
132-
projectRoot = envRoot
130+
// Use PROJECT_ROOT environment variable, fallback to working directory
131+
projectRoot := os.Getenv("PROJECT_ROOT")
132+
if projectRoot == "" {
133+
// If not set, use current working directory
134+
var err error
135+
projectRoot, err = os.Getwd()
136+
if err != nil {
137+
b.Logf("Warning: Could not determine working directory: %v", err)
138+
projectRoot = "."
139+
}
133140
}
134141
resultsDir := filepath.Join(projectRoot, "benchmark_results", "hybrid_vs_milvus")
135142
os.MkdirAll(resultsDir, 0755)

0 commit comments

Comments
 (0)