lib/storage: optimize TSID comparison

This commit is contained in:
Aliaksandr Valialkin 2019-09-26 14:15:52 +03:00
parent c39355921e
commit 0fa6df94a2
2 changed files with 23 additions and 53 deletions

View File

@ -48,42 +48,30 @@ type rawRowsSort []rawRow
func (rrs *rawRowsSort) Len() int { return len(*rrs) }
func (rrs *rawRowsSort) Less(i, j int) bool {
x := *rrs
if i < 0 || j < 0 || i >= len(x) || j >= len(x) {
// This is no-op for compiler, so it doesn't generate panic code
// for out of range access on x[i], x[j] below
return false
}
a := &x[i]
b := &x[j]
ta := &a.TSID
tb := &b.TSID
if ta.MetricID == tb.MetricID {
// Fast path - identical TSID values.
return a.Timestamp < b.Timestamp
}
// Slow path - compare TSIDs.
// Manually inline TSID.Less here, since the compiler doesn't inline it yet :(
if ta.MetricGroupID < tb.MetricGroupID {
return true
if ta.MetricGroupID != tb.MetricGroupID {
return ta.MetricGroupID < tb.MetricGroupID
}
if ta.MetricGroupID > tb.MetricGroupID {
return false
if ta.JobID != tb.JobID {
return ta.JobID < tb.JobID
}
if ta.JobID < tb.JobID {
return true
if ta.InstanceID != tb.InstanceID {
return ta.InstanceID < tb.InstanceID
}
if ta.JobID > tb.JobID {
return false
if ta.MetricID != tb.MetricID {
return ta.MetricID < tb.MetricID
}
if ta.InstanceID < tb.InstanceID {
return true
}
if ta.InstanceID > tb.InstanceID {
return false
}
if ta.MetricID < tb.MetricID {
return true
}
if ta.MetricID > tb.MetricID {
return false
}
return false
return a.Timestamp < b.Timestamp
}
func (rrs *rawRowsSort) Swap(i, j int) {
x := *rrs

View File

@ -88,34 +88,16 @@ func (t *TSID) Unmarshal(src []byte) ([]byte, error) {
// Less return true if t < b.
func (t *TSID) Less(b *TSID) bool {
if t.MetricID == b.MetricID {
// Fast path - two TSID values are identical.
return false
// Do not compare MetricIDs here as fast path for determining identical TSIDs,
// since identical TSIDs aren't passed here in hot paths.
if t.MetricGroupID != b.MetricGroupID {
return t.MetricGroupID < b.MetricGroupID
}
if t.MetricGroupID < b.MetricGroupID {
return true
if t.JobID != b.JobID {
return t.JobID < b.JobID
}
if t.MetricGroupID > b.MetricGroupID {
return false
if t.InstanceID != b.InstanceID {
return t.InstanceID < b.InstanceID
}
if t.JobID < b.JobID {
return true
}
if t.JobID > b.JobID {
return false
}
if t.InstanceID < b.InstanceID {
return true
}
if t.InstanceID > b.InstanceID {
return false
}
if t.MetricID < b.MetricID {
return true
}
if t.MetricID > b.MetricID {
return false
}
return false
return t.MetricID < b.MetricID
}