From 0fa6df94a23ae28e8551ab31c0aa393c2c415662 Mon Sep 17 00:00:00 2001 From: Aliaksandr Valialkin Date: Thu, 26 Sep 2019 14:15:52 +0300 Subject: [PATCH] lib/storage: optimize TSID comparison --- lib/storage/raw_row.go | 40 ++++++++++++++-------------------------- lib/storage/tsid.go | 36 +++++++++--------------------------- 2 files changed, 23 insertions(+), 53 deletions(-) diff --git a/lib/storage/raw_row.go b/lib/storage/raw_row.go index c738e29f4..b1d978d33 100644 --- a/lib/storage/raw_row.go +++ b/lib/storage/raw_row.go @@ -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 diff --git a/lib/storage/tsid.go b/lib/storage/tsid.go index 210787a3b..7e3904e9a 100644 --- a/lib/storage/tsid.go +++ b/lib/storage/tsid.go @@ -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 }