-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathprofiling_analysis.py
More file actions
292 lines (257 loc) · 11.5 KB
/
profiling_analysis.py
File metadata and controls
292 lines (257 loc) · 11.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
#!/usr/bin/env python3
"""
End-to-End Profiling Analysis for SochDB Insert Performance
This tool identifies specific bottlenecks in the insertion path by:
1. Comparing Rust-native vs Python FFI performance
2. Analyzing config differences
3. Measuring specific operation costs
4. Finding the 7x performance gap with ChromaDB
"""
import time
import numpy as np
import os
import sys
import subprocess
import json
def run_rust_benchmark():
"""Run Rust benchmark to get baseline performance"""
print("🦀 Running Rust Native Benchmark...")
try:
result = subprocess.run(
["cargo", "run", "-p", "benchmarks", "--release", "--bin", "insert-profile"],
cwd="/Users/sushanth/sochdb",
capture_output=True,
text=True,
timeout=30
)
if result.returncode == 0:
# Parse output for throughput numbers
lines = result.stdout.split('\n')
rust_performance = {}
for line in lines:
if "vectors in" in line and "vec/sec" in line:
# Example: "Batch insert: 5000 vectors in 939.923834ms (5320 vec/sec)"
if "128D" in lines[lines.index(line) - 2]:
if "1000 vectors" in line:
throughput = int(line.split("(")[1].split(" vec/sec")[0])
rust_performance["128D_1K"] = throughput
elif "5000 vectors" in line:
throughput = int(line.split("(")[1].split(" vec/sec")[0])
rust_performance["128D_5K"] = throughput
elif "768D" in lines[lines.index(line) - 2]:
if "1000 vectors" in line:
throughput = int(line.split("(")[1].split(" vec/sec")[0])
rust_performance["768D_1K"] = throughput
elif "5000 vectors" in line:
throughput = int(line.split("(")[1].split(" vec/sec")[0])
rust_performance["768D_5K"] = throughput
return rust_performance
else:
print(f"❌ Rust benchmark failed: {result.stderr}")
return None
except Exception as e:
print(f"❌ Error running Rust benchmark: {e}")
return None
def analyze_config_differences():
"""Analyze configuration differences that might explain performance gaps"""
print("🔧 Analyzing Configuration Differences...")
# Expected configs for different systems
configs = {
"ChromaDB_Default": {
"max_connections": 16,
"max_connections_layer0": 32,
"ef_construction": 64, # Lower for faster inserts
"ef_search": 50,
"quantization": "None", # No quantization overhead
"batch_processing": "Optimized_C++",
"concurrency": "High"
},
"SochDB_Current": {
"max_connections": 16,
"max_connections_layer0": 32,
"ef_construction": 100, # Higher = slower inserts
"ef_search": 50,
"quantization": "F32_with_normalization", # Overhead
"batch_processing": "Rust_with_safety_checks",
"concurrency": "RwLock_per_layer"
},
"SochDB_Optimized": {
"max_connections": 16,
"max_connections_layer0": 32,
"ef_construction": 48, # Reduced for speed
"ef_search": 50,
"quantization": "Optional",
"batch_processing": "Lock_free_batches",
"concurrency": "Atomic_operations"
}
}
print("Configuration Analysis:")
for name, config in configs.items():
print(f"\n{name}:")
for key, value in config.items():
print(f" {key}: {value}")
print("\n🔍 Key Differences:")
print("1. ef_construction: ChromaDB ~64 vs SochDB 100 (56% higher)")
print("2. Quantization overhead: SochDB has normalization costs")
print("3. Safety checks: Rust bounds checking vs C++ unchecked")
print("4. Lock granularity: Per-layer locks vs bulk operations")
return configs
def estimate_operation_costs():
"""Estimate the cost of various operations in the insertion path"""
print("\n⚡ Estimating Operation Costs...")
# Based on the code analysis, here are the major operations per insert:
operations = {
"vector_quantization": "~5-10μs (SIMD normalization + storage)",
"random_level_assignment": "~0.1μs (simple RNG)",
"entry_point_lookup": "~0.1μs (atomic read)",
"search_layer_concurrent": "~50-200μs (depending on graph size, ef_construction)",
"select_neighbors_heuristic": "~20-50μs (distance calculations + sorting)",
"add_connection_safe": "~10-30μs × M connections (lock contention + retry loops)",
"reverse_edge_updates": "~10-30μs × M connections (more lock contention)",
"connectivity_validation": "~5-10μs (graph repair checks)",
}
print("Per-Insert Operation Breakdown:")
for op, cost in operations.items():
print(f" {op}: {cost}")
# Calculate theoretical total
print(f"\n📊 Theoretical Analysis:")
print(f"Best case (small graph): ~100-200μs per insert (5,000-10,000 vec/s)")
print(f"Typical case (medium graph): ~300-500μs per insert (2,000-3,000 vec/s)")
print(f"Worst case (lock contention): ~1000μs per insert (1,000 vec/s)")
print(f"Current observed: ~540μs per insert (1,854 vec/s)")
return operations
def identify_specific_bottlenecks():
"""Identify the most likely performance bottlenecks"""
print("\n🎯 Specific Bottleneck Analysis...")
bottlenecks = [
{
"issue": "Lock Contention in add_connection_safe",
"impact": "High - O(M × retry_loops) per insert",
"evidence": "MAX_RETRIES = 10, version-based retry loops",
"fix": "Use lock-free data structures or batch edge updates",
"priority": "CRITICAL"
},
{
"issue": "High ef_construction Parameter",
"impact": "Medium - More candidates searched per layer",
"evidence": "ef_construction=100 vs ChromaDB ~64 (56% overhead)",
"fix": "Reduce ef_construction to 48-64 for batch inserts",
"priority": "HIGH"
},
{
"issue": "Excessive Vector Quantization",
"impact": "Medium - Normalization + precision conversion",
"evidence": "from_f32_normalized called on every insert",
"fix": "Batch quantization or disable for bulk inserts",
"priority": "MEDIUM"
},
{
"issue": "Sequential Wave Processing",
"impact": "High - No parallelism in connection phase",
"evidence": "wave.chunks(64) but sequential connect_node_fast",
"fix": "Parallel connection building with conflict resolution",
"priority": "HIGH"
},
{
"issue": "Distance Calculation Redundancy",
"impact": "Low-Medium - Repeated calculations in pruning",
"evidence": "calculate_distance called multiple times per edge",
"fix": "Cache distance calculations during neighbor selection",
"priority": "MEDIUM"
}
]
print("Ranked Bottlenecks (by impact × likelihood):")
for i, bottleneck in enumerate(bottlenecks, 1):
print(f"\n{i}. {bottleneck['issue']} [{bottleneck['priority']}]")
print(f" Impact: {bottleneck['impact']}")
print(f" Evidence: {bottleneck['evidence']}")
print(f" Fix: {bottleneck['fix']}")
return bottlenecks
def recommend_optimizations():
"""Recommend specific optimizations to reach ChromaDB performance"""
print("\n🚀 Optimization Recommendations...")
optimizations = [
{
"name": "Reduce ef_construction for Batch Inserts",
"change": "Use adaptive ef_construction: 48 for batches, 100 for individual",
"expected_gain": "30-40% insert speedup",
"effort": "LOW",
"implementation": "Modify adaptive_ef_construction() method"
},
{
"name": "Lock-Free Edge Updates",
"change": "Replace add_connection_safe retry loops with atomic CAS",
"expected_gain": "50-70% under contention",
"effort": "HIGH",
"implementation": "Use AtomicPtr for neighbor lists"
},
{
"name": "Parallel Wave Connection",
"change": "Connect nodes in parallel within waves, defer conflicts",
"expected_gain": "2-4x on multi-core systems",
"effort": "MEDIUM",
"implementation": "Rayon parallel iterator with conflict detection"
},
{
"name": "Batch Vector Quantization",
"change": "SIMD-parallel quantization for entire batch",
"expected_gain": "10-20% for high-dimensional vectors",
"effort": "MEDIUM",
"implementation": "Use rayon + SIMD for vector preprocessing"
},
{
"name": "Distance Cache",
"change": "Cache distance calculations during neighbor selection",
"expected_gain": "15-25% for dense graphs",
"effort": "LOW",
"implementation": "Add HashMap<(u128,u128), f32> per layer"
}
]
print("Priority Optimizations:")
for opt in optimizations:
print(f"\n• {opt['name']} [{opt['effort']} effort]")
print(f" Change: {opt['change']}")
print(f" Expected gain: {opt['expected_gain']}")
print(f" Implementation: {opt['implementation']}")
# Target calculation
print(f"\n🎯 Performance Targets:")
current_throughput = 1854
chromadb_throughput = 13570
gap_factor = chromadb_throughput / current_throughput
print(f"Current SochDB: {current_throughput:,} vec/s")
print(f"Target ChromaDB: {chromadb_throughput:,} vec/s")
print(f"Gap: {gap_factor:.1f}x")
print(f"\nOptimization pathway:")
print(f"1. ef_construction fix: {current_throughput * 1.4:.0f} vec/s (+40%)")
print(f"2. + Lock-free edges: {current_throughput * 1.4 * 1.6:.0f} vec/s (+60%)")
print(f"3. + Parallel waves: {current_throughput * 1.4 * 1.6 * 2.5:.0f} vec/s (+150%)")
print(f"4. Combined target: ~{current_throughput * 1.4 * 1.6 * 2.5:.0f} vec/s")
if current_throughput * 1.4 * 1.6 * 2.5 >= chromadb_throughput * 0.8:
print("✅ Target achievable with planned optimizations!")
else:
print("⚠️ Additional optimizations needed")
return optimizations
def main():
print("=" * 60)
print(" SochDB Insert Performance Profiling Analysis")
print("=" * 60)
# Run Rust benchmark for baseline
rust_perf = run_rust_benchmark()
if rust_perf:
print(f"\n📈 Rust Native Performance:")
for test, throughput in rust_perf.items():
print(f" {test}: {throughput:,} vec/s")
# Analyze configurations
analyze_config_differences()
# Estimate operation costs
estimate_operation_costs()
# Identify bottlenecks
bottlenecks = identify_specific_bottlenecks()
# Recommend optimizations
recommendations = recommend_optimizations()
print("\n" + "=" * 60)
print("Summary: Focus on lock contention and ef_construction tuning")
print("Next step: Implement adaptive ef_construction (quick win)")
print("=" * 60)
if __name__ == "__main__":
main()