allocate less memory in LatestMap merging

Most maps we merge have the same keys, or at least one set of keys is
the subset of the other. Therefore, allocate a result slice capable of
holding only the max of number keys, rather than the sum.
This commit is contained in:
Matthias Radestock
2017-12-02 12:18:56 +00:00
parent acde0ea294
commit 4162b5d734
2 changed files with 15 additions and 3 deletions

View File

@@ -83,7 +83,11 @@ function generate_latest_map() {
case n.entries == nil:
return m
}
out := make([]${entry_type}, 0, len(m.entries)+len(n.entries))
l := len(m.entries)
if len(n.entries) > l {
l = len(n.entries)
}
out := make([]${entry_type}, 0, l)
i, j := 0, 0
for i < len(m.entries) {

View File

@@ -51,7 +51,11 @@ func (m StringLatestMap) Merge(n StringLatestMap) StringLatestMap {
case n.entries == nil:
return m
}
out := make([]stringLatestEntry, 0, len(m.entries)+len(n.entries))
l := len(m.entries)
if len(n.entries) > l {
l = len(n.entries)
}
out := make([]stringLatestEntry, 0, l)
i, j := 0, 0
for i < len(m.entries) {
@@ -268,7 +272,11 @@ func (m NodeControlDataLatestMap) Merge(n NodeControlDataLatestMap) NodeControlD
case n.entries == nil:
return m
}
out := make([]nodeControlDataLatestEntry, 0, len(m.entries)+len(n.entries))
l := len(m.entries)
if len(n.entries) > l {
l = len(n.entries)
}
out := make([]nodeControlDataLatestEntry, 0, l)
i, j := 0, 0
for i < len(m.entries) {