Skip to content

Commit 8c48c04

Browse files
committed
internal/index: call madvise every 512 KB to keep RSS limited
With this change, the pos writing phase of index merging consumes 1GB-2GB of RSS instead of up to 20 GB of RSS!
1 parent 0a94163 commit 8c48c04

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

internal/index/merge.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -238,7 +238,6 @@ func ConcatN(destdir string, srcdirs []string) error {
238238
start = time.Now()
239239
}
240240

241-
// TODO(performance): The following phase accumulates up to 20 GB (!) in memory mappings.
242241
if err := writePos(destdir, trigrams, idxDocid, idxMetaPos); err != nil {
243242
return err
244243
}
@@ -466,6 +465,8 @@ func writePos(destdir string, trigrams []Trigram, idxDocid map[Trigram][]uint32,
466465
}
467466
}
468467
}
468+
469+
idx.rd.maybeMadvise(meta.OffsetData)
469470
}
470471

471472
if err := dw.Flush(); err != nil {

internal/index/read.go

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,24 @@ func (sr *PForReader) Close() error {
140140
return nil
141141
}
142142

143+
const pageSize = 4096
144+
const pagesPerAdvise = 128 // release every 512KB
145+
146+
// maybeMadvise calls MADV_DONTNEED on processed pages to reduce RSS.
147+
func (sr *PForReader) maybeMadvise(offset int64) {
148+
if page := int(offset) / pageSize; page > 0 && page%pagesPerAdvise == 0 {
149+
sr.madviseDontNeed(page)
150+
}
151+
}
152+
153+
func (sr *PForReader) madviseDontNeed(page int) {
154+
// release pages up to the current page-aligned offset
155+
releaseUpTo := page * pageSize
156+
if releaseUpTo <= len(sr.data.Data) {
157+
unix.Madvise(sr.data.Data[:releaseUpTo], unix.MADV_DONTNEED)
158+
}
159+
}
160+
143161
func (sr *PForReader) metaEntry(trigram Trigram) (*MetaEntry, *MetaEntry, error) {
144162
num := len(sr.meta.Data) / metaEntrySize
145163
d := sr.meta.Data

0 commit comments

Comments
 (0)