Avoid potential reallocs by pre-sizing some slices

There's a couple spots where we know exactly how large
the destination buffer should be, so pre-size these to
avoid any reallocs to a higher capacity.

Signed-off-by: Danny Canter <danny@dcantah.dev>
This commit is contained in:
Danny Canter 2024-07-19 13:05:49 -07:00
parent da3655678d
commit b41bb6df73
5 changed files with 17 additions and 17 deletions

View File

@ -268,9 +268,9 @@ func (c *Client) Containers(ctx context.Context, filters ...string) ([]Container
if err != nil {
return nil, err
}
var out []Container
for _, container := range r {
out = append(out, containerFromRecord(c, container))
out := make([]Container, len(r))
for i, container := range r {
out[i] = containerFromRecord(c, container)
}
return out, nil
}
@ -513,9 +513,9 @@ func (c *Client) Restore(ctx context.Context, id string, checkpoint Image, opts
}
defer done(ctx)
copts := []NewContainerOpts{}
for _, o := range opts {
copts = append(copts, o(ctx, id, c, checkpoint, index))
copts := make([]NewContainerOpts, len(opts))
for i, o := range opts {
copts[i] = o(ctx, id, c, checkpoint, index)
}
ctr, err := c.NewContainer(ctx, id, copts...)

View File

@ -119,9 +119,9 @@ When '--all-platforms' is given all images in a manifest list must be available.
exportOpts = append(exportOpts, tarchive.WithSkipNonDistributableBlobs)
}
storeOpts := []image.StoreOpt{}
for _, img := range images {
storeOpts = append(storeOpts, image.WithExtraReference(img))
storeOpts := make([]image.StoreOpt, len(images))
for i, img := range images {
storeOpts[i] = image.WithExtraReference(img)
}
return client.Transfer(ctx,

View File

@ -256,8 +256,8 @@ func onUntarBlob(ctx context.Context, r io.Reader, store content.Ingester, size
func resolveLayers(ctx context.Context, store content.Store, layerFiles []string, blobs map[string]ocispec.Descriptor, compress bool) ([]ocispec.Descriptor, error) {
layers := make([]ocispec.Descriptor, len(layerFiles))
filters := make([]string, len(layerFiles))
descs := map[digest.Digest]*ocispec.Descriptor{}
filters := []string{}
for i, f := range layerFiles {
desc, ok := blobs[f]
if !ok {
@ -265,7 +265,7 @@ func resolveLayers(ctx context.Context, store content.Store, layerFiles []string
}
layers[i] = desc
descs[desc.Digest] = &layers[i]
filters = append(filters, fmt.Sprintf("labels.\"%s\"==%s", labels.LabelUncompressed, desc.Digest.String()))
filters[i] = fmt.Sprintf("labels.\"%s\"==%s", labels.LabelUncompressed, desc.Digest.String())
}
err := store.Walk(ctx, func(info content.Info) error {

View File

@ -64,9 +64,9 @@ func commonContainerToNRI(ctr Container) *nri.Container {
}
func containersToNRI(ctrList []Container) []*nri.Container {
ctrs := []*nri.Container{}
for _, ctr := range ctrList {
ctrs = append(ctrs, containerToNRI(ctr))
ctrs := make([]*nri.Container, len(ctrList))
for i, ctr := range ctrList {
ctrs[i] = containerToNRI(ctr)
}
return ctrs
}

View File

@ -1098,9 +1098,9 @@ func getSupplementalGroupsFromPath(root string, filter func(user.Group) bool) ([
// if there are no additional groups; just return an empty set
return []uint32{}, nil
}
addlGids := []uint32{}
for _, grp := range groups {
addlGids = append(addlGids, uint32(grp.Gid))
addlGids := make([]uint32, len(groups))
for i, grp := range groups {
addlGids[i] = uint32(grp.Gid)
}
return addlGids, nil
}