From c91fd6c67b9114b2b5823b25dec55d4f22a43172 Mon Sep 17 00:00:00 2001 From: Bryan Boreham Date: Mon, 9 Jul 2018 08:07:17 +0000 Subject: [PATCH] Optimise merge of LatestMaps with same keys, different timestamps Swap the two maps so we are merging older into later. --- extras/generate_latest_map | 8 ++++++-- report/latest_map_generated.go | 16 ++++++++++++---- 2 files changed, 18 insertions(+), 6 deletions(-) diff --git a/extras/generate_latest_map b/extras/generate_latest_map index e2bc3e2ea..0f831799c 100755 --- a/extras/generate_latest_map +++ b/extras/generate_latest_map @@ -79,13 +79,17 @@ function generate_latest_map() { // Tries to return one of its inputs, if that already holds the correct result. func (m ${latest_map_type}) Merge(n ${latest_map_type}) ${latest_map_type} { switch { - case m == nil: + case len(m) == 0: return n - case n == nil: + case len(n) == 0: return m } if len(n) > len(m) { m, n = n, m //swap so m is always at least as long as n + } else if len(n) == len(m) && m[0].Timestamp.Before(n[0].Timestamp) { + // Optimise common case where we merge two nodes with the same contents + // sampled at different times. + m, n = n, m // swap equal-length arrays so first element of m is newer } i, j := 0, 0 diff --git a/report/latest_map_generated.go b/report/latest_map_generated.go index ff0e9272b..626b80988 100644 --- a/report/latest_map_generated.go +++ b/report/latest_map_generated.go @@ -47,13 +47,17 @@ func (m StringLatestMap) Size() int { // Tries to return one of its inputs, if that already holds the correct result. func (m StringLatestMap) Merge(n StringLatestMap) StringLatestMap { switch { - case m == nil: + case len(m) == 0: return n - case n == nil: + case len(n) == 0: return m } if len(n) > len(m) { m, n = n, m //swap so m is always at least as long as n + } else if len(n) == len(m) && m[0].Timestamp.Before(n[0].Timestamp) { + // Optimise common case where we merge two nodes with the same contents + // sampled at different times. + m, n = n, m // swap equal-length arrays so first element of m is newer } i, j := 0, 0 @@ -290,13 +294,17 @@ func (m NodeControlDataLatestMap) Size() int { // Tries to return one of its inputs, if that already holds the correct result. func (m NodeControlDataLatestMap) Merge(n NodeControlDataLatestMap) NodeControlDataLatestMap { switch { - case m == nil: + case len(m) == 0: return n - case n == nil: + case len(n) == 0: return m } if len(n) > len(m) { m, n = n, m //swap so m is always at least as long as n + } else if len(n) == len(m) && m[0].Timestamp.Before(n[0].Timestamp) { + // Optimise common case where we merge two nodes with the same contents + // sampled at different times. + m, n = n, m // swap equal-length arrays so first element of m is newer } i, j := 0, 0