lib/storage: use sort.Sort instead of sort.slice in getSortedMetricIDs

This commit is contained in:
Aliaksandr Valialkin 2019-09-19 20:00:33 +03:00
parent 3a697a935a
commit a84fe76677

View file

@ -1206,11 +1206,7 @@ func (is *indexSearch) getSeriesCount() (uint64, error) {
// and adds matching metrics to metricIDs.
func (is *indexSearch) updateMetricIDsByMetricNameMatch(metricIDs, srcMetricIDs map[uint64]struct{}, tfs []*tagFilter) error {
// sort srcMetricIDs in order to speed up Seek below.
sortedMetricIDs := make([]uint64, 0, len(srcMetricIDs))
for metricID := range srcMetricIDs {
sortedMetricIDs = append(sortedMetricIDs, metricID)
}
sort.Slice(sortedMetricIDs, func(i, j int) bool { return sortedMetricIDs[i] < sortedMetricIDs[j] })
sortedMetricIDs := getSortedMetricIDs(srcMetricIDs)
metricName := kbPool.Get()
defer kbPool.Put(metricName)
@ -2073,12 +2069,23 @@ func marshalCommonPrefix(dst []byte, nsPrefix byte) []byte {
}
func getSortedMetricIDs(m map[uint64]struct{}) []uint64 {
a := make([]uint64, len(m))
a := make(uint64Sorter, len(m))
i := 0
for metricID := range m {
a[i] = metricID
i++
}
sort.Slice(a, func(i, j int) bool { return a[i] < a[j] })
// Use sort.Sort instead of sort.Slice in order to reduce memory allocations
sort.Sort(a)
return a
}
type uint64Sorter []uint64
func (s uint64Sorter) Len() int { return len(s) }
func (s uint64Sorter) Less(i, j int) bool {
return s[i] < s[j]
}
func (s uint64Sorter) Swap(i, j int) {
s[i], s[j] = s[j], s[i]
}