1
0

zero_allocation_pool.go 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248
  1. package indexer
  2. import (
  3. "sync"
  4. "time"
  5. )
  6. // ObjectPool provides zero-allocation object pooling for indexer components
  7. type ObjectPool struct {
  8. jobPool sync.Pool
  9. resultPool sync.Pool
  10. docPool sync.Pool
  11. bufferPool sync.Pool
  12. }
  13. // NewObjectPool creates a new object pool with pre-allocated pools
  14. func NewObjectPool() *ObjectPool {
  15. return &ObjectPool{
  16. jobPool: sync.Pool{
  17. New: func() interface{} {
  18. return &IndexJob{
  19. Documents: make([]*Document, 0, 1000), // Pre-allocate capacity
  20. Priority: 0,
  21. }
  22. },
  23. },
  24. resultPool: sync.Pool{
  25. New: func() interface{} {
  26. return &IndexResult{
  27. Processed: 0,
  28. Succeeded: 0,
  29. Failed: 0,
  30. Duration: 0,
  31. ErrorRate: 0.0,
  32. Throughput: 0.0,
  33. }
  34. },
  35. },
  36. docPool: sync.Pool{
  37. New: func() interface{} {
  38. return &Document{
  39. ID: "",
  40. Fields: nil,
  41. }
  42. },
  43. },
  44. bufferPool: sync.Pool{
  45. New: func() interface{} {
  46. // Pre-allocate 4KB buffer for common operations
  47. return make([]byte, 0, 4096)
  48. },
  49. },
  50. }
  51. }
  52. // GetIndexJob returns a pooled IndexJob, reset and ready for use
  53. func (p *ObjectPool) GetIndexJob() *IndexJob {
  54. job := p.jobPool.Get().(*IndexJob)
  55. // Reset job state
  56. job.Documents = job.Documents[:0] // Keep capacity, reset length
  57. job.Priority = 0
  58. job.Callback = nil
  59. return job
  60. }
  61. // PutIndexJob returns an IndexJob to the pool
  62. func (p *ObjectPool) PutIndexJob(job *IndexJob) {
  63. if job != nil {
  64. // Clear any references to prevent memory leaks
  65. for i := range job.Documents {
  66. if job.Documents[i] != nil {
  67. p.PutDocument(job.Documents[i])
  68. }
  69. }
  70. job.Documents = job.Documents[:0]
  71. job.Callback = nil
  72. p.jobPool.Put(job)
  73. }
  74. }
  75. // GetIndexResult returns a pooled IndexResult, reset and ready for use
  76. func (p *ObjectPool) GetIndexResult() *IndexResult {
  77. result := p.resultPool.Get().(*IndexResult)
  78. // Reset result state
  79. result.Processed = 0
  80. result.Succeeded = 0
  81. result.Failed = 0
  82. result.Duration = 0
  83. result.ErrorRate = 0.0
  84. result.Throughput = 0.0
  85. return result
  86. }
  87. // PutIndexResult returns an IndexResult to the pool
  88. func (p *ObjectPool) PutIndexResult(result *IndexResult) {
  89. if result != nil {
  90. p.resultPool.Put(result)
  91. }
  92. }
  93. // GetDocument returns a pooled Document, reset and ready for use
  94. func (p *ObjectPool) GetDocument() *Document {
  95. doc := p.docPool.Get().(*Document)
  96. // Reset document state
  97. doc.ID = ""
  98. doc.Fields = nil
  99. return doc
  100. }
  101. // PutDocument returns a Document to the pool
  102. func (p *ObjectPool) PutDocument(doc *Document) {
  103. if doc != nil {
  104. doc.ID = ""
  105. doc.Fields = nil
  106. p.docPool.Put(doc)
  107. }
  108. }
  109. // GetBuffer returns a pooled byte buffer, reset and ready for use
  110. func (p *ObjectPool) GetBuffer() []byte {
  111. buffer := p.bufferPool.Get().([]byte)
  112. return buffer[:0] // Reset length, keep capacity
  113. }
  114. // PutBuffer returns a byte buffer to the pool
  115. func (p *ObjectPool) PutBuffer(buffer []byte) {
  116. if buffer != nil && cap(buffer) > 0 {
  117. // Only pool buffers with reasonable capacity to prevent memory bloat
  118. if cap(buffer) <= 64*1024 { // Max 64KB
  119. p.bufferPool.Put(buffer[:0])
  120. }
  121. }
  122. }
  123. // ZeroAllocBatchProcessor provides zero-allocation batch processing
  124. type ZeroAllocBatchProcessor struct {
  125. pool *ObjectPool
  126. config *Config
  127. // Metrics
  128. allocationsAvoided int64
  129. poolHitRate float64
  130. poolStats sync.RWMutex
  131. }
  132. // NewZeroAllocBatchProcessor creates a new zero-allocation batch processor
  133. func NewZeroAllocBatchProcessor(config *Config) *ZeroAllocBatchProcessor {
  134. return &ZeroAllocBatchProcessor{
  135. pool: NewObjectPool(),
  136. config: config,
  137. }
  138. }
  139. // CreateJobBatch creates a batch of jobs using object pooling
  140. func (z *ZeroAllocBatchProcessor) CreateJobBatch(documents []*Document, batchSize int) []*IndexJob {
  141. jobCount := (len(documents) + batchSize - 1) / batchSize
  142. jobs := make([]*IndexJob, 0, jobCount)
  143. for i := 0; i < len(documents); i += batchSize {
  144. end := i + batchSize
  145. if end > len(documents) {
  146. end = len(documents)
  147. }
  148. // Use pooled job
  149. job := z.pool.GetIndexJob()
  150. // Add documents to job (reusing pre-allocated slice capacity)
  151. for j := i; j < end; j++ {
  152. job.Documents = append(job.Documents, documents[j])
  153. }
  154. job.Priority = 1
  155. jobs = append(jobs, job)
  156. }
  157. return jobs
  158. }
  159. // ProcessJobResults processes job results with zero allocation
  160. func (z *ZeroAllocBatchProcessor) ProcessJobResults(results []*IndexResult) *IndexResult {
  161. // Use pooled result for aggregation
  162. aggregatedResult := z.pool.GetIndexResult()
  163. totalProcessed := 0
  164. totalSucceeded := 0
  165. totalFailed := 0
  166. var totalDuration time.Duration
  167. for _, result := range results {
  168. totalProcessed += result.Processed
  169. totalSucceeded += result.Succeeded
  170. totalFailed += result.Failed
  171. totalDuration += result.Duration
  172. // Return individual result to pool
  173. z.pool.PutIndexResult(result)
  174. }
  175. // Set aggregated values
  176. aggregatedResult.Processed = totalProcessed
  177. aggregatedResult.Succeeded = totalSucceeded
  178. aggregatedResult.Failed = totalFailed
  179. aggregatedResult.Duration = totalDuration
  180. if totalProcessed > 0 {
  181. aggregatedResult.ErrorRate = float64(totalFailed) / float64(totalProcessed)
  182. aggregatedResult.Throughput = float64(totalSucceeded) / totalDuration.Seconds()
  183. }
  184. return aggregatedResult
  185. }
  186. // ReleaseBatch releases a batch of jobs back to the pool
  187. func (z *ZeroAllocBatchProcessor) ReleaseBatch(jobs []*IndexJob) {
  188. for _, job := range jobs {
  189. z.pool.PutIndexJob(job)
  190. }
  191. }
  192. // GetPoolStats returns current pool utilization statistics
  193. func (z *ZeroAllocBatchProcessor) GetPoolStats() PoolStats {
  194. z.poolStats.RLock()
  195. defer z.poolStats.RUnlock()
  196. return PoolStats{
  197. AllocationsAvoided: z.allocationsAvoided,
  198. PoolHitRate: z.poolHitRate,
  199. ActiveObjects: z.getActiveObjectCount(),
  200. }
  201. }
  202. func (z *ZeroAllocBatchProcessor) getActiveObjectCount() int {
  203. // This is an approximation - actual implementation would need more sophisticated tracking
  204. return 0
  205. }
  206. // PoolStats represents object pool statistics
  207. type PoolStats struct {
  208. AllocationsAvoided int64 `json:"allocations_avoided"`
  209. PoolHitRate float64 `json:"pool_hit_rate"`
  210. ActiveObjects int `json:"active_objects"`
  211. }
  212. // GetPool returns the underlying object pool for direct access if needed
  213. func (z *ZeroAllocBatchProcessor) GetPool() *ObjectPool {
  214. return z.pool
  215. }