From 704d16c6c6d630a35e2ab7d3451145f0a1134ac3 Mon Sep 17 00:00:00 2001 From: Pavel Karpy Date: Fri, 11 Jun 2021 15:41:36 +0300 Subject: [PATCH] [#307] v2/refs/test: Do not allocate memory if `!empty` Move all memory allocation and field settings in `Generate...(empty bool)` functions behind `if !empty` check. Do not create empty slices if `empty == true`. Signed-off-by: Pavel Karpy --- v2/refs/test/generate.go | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/v2/refs/test/generate.go b/v2/refs/test/generate.go index 1f90ce0..e8a5e6e 100644 --- a/v2/refs/test/generate.go +++ b/v2/refs/test/generate.go @@ -28,8 +28,10 @@ func GenerateOwnerID(empty bool) *refs.OwnerID { func GenerateAddress(empty bool) *refs.Address { m := new(refs.Address) - m.SetObjectID(GenerateObjectID(empty)) - m.SetContainerID(GenerateContainerID(empty)) + if !empty { + m.SetObjectID(GenerateObjectID(false)) + m.SetContainerID(GenerateContainerID(false)) + } return m } @@ -45,7 +47,7 @@ func GenerateObjectID(empty bool) *refs.ObjectID { } func GenerateObjectIDs(empty bool) []*refs.ObjectID { - ids := make([]*refs.ObjectID, 0) + var ids []*refs.ObjectID if !empty { ids = append(ids, @@ -67,7 +69,9 @@ func GenerateContainerID(empty bool) *refs.ContainerID { return m } -func GenerateContainerIDs(empty bool) (res []*refs.ContainerID) { +func GenerateContainerIDs(empty bool) []*refs.ContainerID { + var res []*refs.ContainerID + if !empty { res = append(res, GenerateContainerID(false), @@ -75,7 +79,7 @@ func GenerateContainerIDs(empty bool) (res []*refs.ContainerID) { ) } - return + return res } func GenerateSignature(empty bool) *refs.Signature {