[#227] netmap: Pre-calculate resulting slice capacity in flattenNodes

```
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 <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2022-06-07 06:40:59 +03:00 committed by LeL
parent a8fd2ef954
commit 9c47fca7c2

View file

@ -21,7 +21,14 @@ func NewNetmap(nodes Nodes) (*Netmap, error) {
} }
func flattenNodes(ns []Nodes) Nodes { 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 { for i := range ns {
result = append(result, ns[i]...) result = append(result, ns[i]...)
} }