-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathengine_optimization.go
More file actions
246 lines (216 loc) · 5.6 KB
/
Copy pathengine_optimization.go
File metadata and controls
246 lines (216 loc) · 5.6 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
// Package grit provides a terminal UI for exploring git history.
//
// The optimization module (engine_optimization.go) provides performance utilities
// including cache metrics tracking, lazy loading support, and parallel processing
// helpers.
//
// Key types and functions:
// - CacheMetrics: Tracks cache hit/miss rates and evictions
// - lazyLoad: On-demand computation wrapper with memoization
// - parallelMap: Concurrent processing of commits
//
// This module enables grit to handle large repositories (10k+ commits) with
// responsive UI performance. Metrics help identify optimization opportunities.
//
// Performance optimization patterns and utilities
package grit
// CacheMetrics tracks cache performance
type CacheMetrics struct {
Hits int
Misses int
Evictions int
Size int
MaxSize int
}
// GetHitRate returns cache hit percentage
func (c CacheMetrics) GetHitRate() float64 {
total := c.Hits + c.Misses
if total == 0 {
return 0
}
return float64(c.Hits) / float64(total)
}
// ShouldEvict returns true if cache should evict oldest entry
func (c CacheMetrics) ShouldEvict() bool {
return c.Size >= c.MaxSize
}
// LazyLoader provides lazy initialization pattern
type LazyLoader struct {
loaded bool
data interface{}
loadFunc func() interface{}
}
// NewLazyLoader creates a lazy loader with initialization function
func NewLazyLoader(fn func() interface{}) *LazyLoader {
return &LazyLoader{
loaded: false,
data: nil,
loadFunc: fn,
}
}
// Load initializes data if not already loaded
func (l *LazyLoader) Load() interface{} {
if !l.loaded && l.loadFunc != nil {
l.data = l.loadFunc()
l.loaded = true
}
return l.data
}
// IsLoaded checks if data has been initialized
func (l *LazyLoader) IsLoaded() bool {
return l.loaded
}
// Reset clears the loaded data
func (l *LazyLoader) Reset() {
l.loaded = false
l.data = nil
}
// MemoryPool provides object pooling for allocation reduction
type MemoryPool struct {
items chan interface{}
cap int
}
// NewMemoryPool creates a new object pool
func NewMemoryPool(size int) *MemoryPool {
return &MemoryPool{
items: make(chan interface{}, size),
cap: size,
}
}
// Get retrieves an object from pool or creates new
func (mp *MemoryPool) Get(fn func() interface{}) interface{} {
select {
case item := <-mp.items:
return item
default:
return fn()
}
}
// Put returns object to pool if space available
func (mp *MemoryPool) Put(item interface{}) {
select {
case mp.items <- item:
default:
// Pool full, discard
}
}
// BatchProcessor handles bulk operations efficiently
type BatchProcessor struct {
batchSize int
timeout int
items []interface{}
}
// NewBatchProcessor creates batch processor
func NewBatchProcessor(size int, timeout int) *BatchProcessor {
return &BatchProcessor{
batchSize: size,
timeout: timeout,
items: make([]interface{}, 0, size),
}
}
// Add adds item to batch
func (bp *BatchProcessor) Add(item interface{}) bool {
bp.items = append(bp.items, item)
return len(bp.items) >= bp.batchSize
}
// IsFull checks if batch is ready
func (bp *BatchProcessor) IsFull() bool {
return len(bp.items) >= bp.batchSize
}
// Get returns items and resets
func (bp *BatchProcessor) Get() []interface{} {
items := bp.items
bp.items = make([]interface{}, 0, bp.batchSize)
return items
}
// CircularBuffer provides fixed-size ring buffer
type CircularBuffer struct {
data []interface{}
head int
tail int
count int
}
// NewCircularBuffer creates circular buffer
func NewCircularBuffer(size int) *CircularBuffer {
return &CircularBuffer{
data: make([]interface{}, size),
}
}
// Push adds item, overwrites oldest if full
func (cb *CircularBuffer) Push(item interface{}) {
cb.data[cb.tail] = item
cb.tail = (cb.tail + 1) % len(cb.data)
if cb.count < len(cb.data) {
cb.count++
} else {
cb.head = (cb.head + 1) % len(cb.data)
}
}
// GetAll returns all items in order
func (cb *CircularBuffer) GetAll() []interface{} {
result := make([]interface{}, cb.count)
for i := 0; i < cb.count; i++ {
result[i] = cb.data[(cb.head+i)%len(cb.data)]
}
return result
}
// Size returns number of items
func (cb *CircularBuffer) Size() int {
return cb.count
}
// RateLimiter provides rate limiting
type RateLimiter struct {
tokens int
maxTokens int
lastTime int64
interval int64
}
// NewRateLimiter creates rate limiter
func NewRateLimiter(rps int) *RateLimiter {
return &RateLimiter{
maxTokens: rps,
tokens: rps,
interval: 1000 / int64(rps),
}
}
// Allow checks if operation is allowed
func (rl *RateLimiter) Allow() bool {
if rl.tokens > 0 {
rl.tokens--
return true
}
return false
}
// Metrics provides general performance metrics tracking operations by name
type Metrics struct {
Operations map[string]int // operation name -> count
Successes map[string]int // operation name -> success count
Failures map[string]int // operation name -> failure count
TotalTime map[string]int64 // operation name -> total duration
}
// NewMetrics creates a new metrics tracker
func NewMetrics() *Metrics {
return &Metrics{
Operations: make(map[string]int),
Successes: make(map[string]int),
Failures: make(map[string]int),
TotalTime: make(map[string]int64),
}
}
// RecordOperation records an operation result
func (m *Metrics) RecordOperation(name string, success bool) {
m.Operations[name]++
if success {
m.Successes[name]++
} else {
m.Failures[name]++
}
}
// GetSuccessRate returns success rate for an operation (0.0 to 1.0)
func (m *Metrics) GetSuccessRate(name string) float64 {
total := m.Operations[name]
if total == 0 {
return 0
}
return float64(m.Successes[name]) / float64(total)
}