From 1570a5127d35b2ab5b79e68dc939ea3d9f9a0d44 Mon Sep 17 00:00:00 2001 From: Thejas-bhat Date: Tue, 7 Nov 2023 09:47:02 +0530 Subject: [PATCH] index.Delete() to free up memory --- faiss_vector_posting.go | 2 ++ section_faiss_vector_index.go | 15 +++++++++++++++ 2 files changed, 17 insertions(+) diff --git a/faiss_vector_posting.go b/faiss_vector_posting.go index 017c3101..59e193b6 100644 --- a/faiss_vector_posting.go +++ b/faiss_vector_posting.go @@ -334,6 +334,8 @@ func (sb *SegmentBase) SimilarVectors(field string, qVector []float32, k int64, return nil, err } + vecIndex.Delete() + // for every similar vector returned by the Search() API, add the corresponding // docID and the score to the newly created vecPostingsList for i := 0; i < len(ids); i++ { diff --git a/section_faiss_vector_index.go b/section_faiss_vector_index.go index 198a02c3..b22010a0 100644 --- a/section_faiss_vector_index.go +++ b/section_faiss_vector_index.go @@ -302,6 +302,8 @@ func (v *vectorIndexOpaque) mergeAndWriteVectorIndexes(fieldID int, sbs []*Segme return err } + index.Delete() + fieldStart, err := v.flushVectorSection(vecToDocID, mergedIndexBytes, w) if err != nil { return err @@ -328,6 +330,8 @@ func (v *vectorIndexOpaque) mergeAndWriteVectorIndexes(fieldID int, sbs []*Segme return err } + freeReconstructedIndexes(vecIndexes) + fieldStart, err := v.flushVectorSection(vecToDocID, mergedIndexBytes, w) if err != nil { return err @@ -336,6 +340,13 @@ func (v *vectorIndexOpaque) mergeAndWriteVectorIndexes(fieldID int, sbs []*Segme return nil } +// todo: can be parallelized. +func freeReconstructedIndexes(vecIndexes []*faiss.IndexImpl) { + for _, index := range vecIndexes { + index.Delete() + } +} + // todo: is it possible to merge this resuable stuff with the interim's tmp0? func (v *vectorIndexOpaque) grabBuf(size int) []byte { buf := v.tmp0 @@ -385,6 +396,10 @@ func (vo *vectorIndexOpaque) writeVectorIndexes(w *CountHashWriter) (offset uint return 0, err } + // safe to delete the index now since its been serialized and not + // referenced anymore + index.Delete() + fieldStart := w.Count() // writing out two offset values to indicate that the current field's // vector section doesn't have valid doc value content within it.