Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Free dedupe buffer's tracking map after shrink #9526

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ type DedupeBuffer struct {
// keyToPendingUpdate holds an entry for each updateWithStringKey in the
// pendingUpdates queue
keyToPendingUpdate map[string]*list.Element
peakPendingUpdatesLen int

// liveResourceKeys Contains an entry for every key that we have sent to
// the consumer and that we have not subsequently sent a deletion for.
liveResourceKeys set.Set[string]
Expand Down Expand Up @@ -176,6 +178,7 @@ func (d *DedupeBuffer) OnUpdatesKeysKnown(updates []api.Update, keys []string) {
update: u,
})
d.keyToPendingUpdate[key] = element
d.peakPendingUpdatesLen = max(len(d.keyToPendingUpdate), d.peakPendingUpdatesLen)
}
}
queueNowEmpty := d.pendingUpdates.Len() == 0
Expand Down Expand Up @@ -252,6 +255,14 @@ func (d *DedupeBuffer) pullNextBatch(buf []any, batchSize int) []any {
if u, ok := first.Value.(updateWithStringKey); ok {
key := u.key
delete(d.keyToPendingUpdate, key)
if len(d.keyToPendingUpdate) == 0 && d.peakPendingUpdatesLen > 100 {
// Map blocks never get freed when a map is scaled down.
// https://github.com/golang/go/issues/20135
// Opportunistically free the map when it's empty. This can
// free a good amount of RAM after loading a large snapshot.
d.keyToPendingUpdate = map[string]*list.Element{}
d.peakPendingUpdatesLen = 0
}
// Update liveResourceKeys now, before we drop the lock. Once we drop
// the lock we're committed to sending these keys.
if u.update.Value == nil {
Expand Down