Skip to content

Commit

Permalink
index.Delete() to free up memory
Browse files Browse the repository at this point in the history
  • Loading branch information
Thejas-bhat committed Nov 7, 2023
1 parent 7945e8e commit 1570a51
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 0 deletions.
2 changes: 2 additions & 0 deletions faiss_vector_posting.go
Original file line number Diff line number Diff line change
Expand Up @@ -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++ {
Expand Down
15 changes: 15 additions & 0 deletions section_faiss_vector_index.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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.
Expand Down

0 comments on commit 1570a51

Please sign in to comment.