From 9c47fca7c2ab761401a0262eb958d2cdd95b4b3f Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 7 Jun 2022 06:40:59 +0300 Subject: [PATCH] [#227] netmap: Pre-calculate resulting slice capacity in `flattenNodes` MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ``` name old time/op new time/op delta ManySelects-12 17.5µs ±27% 15.5µs ±24% -11.57% (p=0.024 n=20+20) name old alloc/op new alloc/op delta ManySelects-12 9.03kB ± 0% 8.65kB ± 0% -4.25% (p=0.000 n=20+20) name old allocs/op new allocs/op delta ManySelects-12 86.0 ± 0% 82.0 ± 0% -4.65% (p=0.000 n=20+20) ``` Signed-off-by: Leonard Lyubich --- netmap/netmap.go | 9 ++++++++- 1 file changed, 8 insertions(+), 1 deletion(-) diff --git a/netmap/netmap.go b/netmap/netmap.go index c4e2f13..54550bf 100644 --- a/netmap/netmap.go +++ b/netmap/netmap.go @@ -21,7 +21,14 @@ func NewNetmap(nodes Nodes) (*Netmap, error) { } func flattenNodes(ns []Nodes) Nodes { - result := make(Nodes, 0, len(ns)) + var sz, i int + + for i = range ns { + sz += len(ns[i]) + } + + result := make(Nodes, 0, sz) + for i := range ns { result = append(result, ns[i]...) }