Browse Source

MemPostings: allocate ListPostings once in PFLM (#15427)

Instead of allocating ListPostings pointers one by one, allocate a slice
and take pointers from that. It's faster, and also generates less
garbage (NewListPostings is one of the top offenders in number of
allocations).

Signed-off-by: Oleg Zaytsev <mail@olegzaytsev.com>
pull/14487/merge
Oleg Zaytsev 1 day ago committed by GitHub
parent
commit
cc390aab64
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
  1. 6
      tsdb/index/postings.go

6
tsdb/index/postings.go

@ -430,15 +430,17 @@ func (p *MemPostings) PostingsForLabelMatching(ctx context.Context, name string,
// Now `vals` only contains the values that matched, get their postings. // Now `vals` only contains the values that matched, get their postings.
its := make([]Postings, 0, len(vals)) its := make([]Postings, 0, len(vals))
lps := make([]ListPostings, len(vals))
p.mtx.RLock() p.mtx.RLock()
e := p.m[name] e := p.m[name]
for _, v := range vals { for i, v := range vals {
if refs, ok := e[v]; ok { if refs, ok := e[v]; ok {
// Some of the values may have been garbage-collected in the meantime this is fine, we'll just skip them. // Some of the values may have been garbage-collected in the meantime this is fine, we'll just skip them.
// If we didn't let the mutex go, we'd have these postings here, but they would be pointing nowhere // If we didn't let the mutex go, we'd have these postings here, but they would be pointing nowhere
// because there would be a `MemPostings.Delete()` call waiting for the lock to delete these labels, // because there would be a `MemPostings.Delete()` call waiting for the lock to delete these labels,
// because the series were deleted already. // because the series were deleted already.
its = append(its, NewListPostings(refs)) lps[i] = ListPostings{list: refs}
its = append(its, &lps[i])
} }
} }
// Let the mutex go before merging. // Let the mutex go before merging.

Loading…
Cancel
Save