Optimise merge of LatestMaps with same keys, different timestamps

Swap the two maps so we are merging older into later.
This commit is contained in:
Bryan Boreham
2018-07-09 08:07:17 +00:00
parent 43a0a7e5d3
commit c91fd6c67b
2 changed files with 18 additions and 6 deletions

View File

@@ -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

View File

@@ -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