mirror of
https://github.com/containers/podman.git
synced 2026-03-13 08:01:19 +08:00
Preallocate a slice
When we already know the resulting slice size but still need/want to use append, it makes sense to preallocate the slice by using make with the capacity argument. This commit is part of series fixing issues reported by prealloc linter from golangci-lint v2.8.0. Signed-off-by: Kir Kolyshkin <kolyshkin@gmail.com>
This commit is contained in:
@@ -145,7 +145,7 @@ func jsonOut(responses []entities.ListContainer) error {
|
||||
entities.ListContainer
|
||||
Created int64
|
||||
}
|
||||
r := make([]jsonFormat, 0)
|
||||
r := make([]jsonFormat, 0, len(responses))
|
||||
for _, con := range responses {
|
||||
con.CreatedAt = units.HumanDuration(time.Since(con.Created)) + " ago"
|
||||
con.Status = psReporter{con}.Status()
|
||||
|
||||
@@ -103,8 +103,9 @@ func AutocompleteMachine(cmd *cobra.Command, args []string, toComplete string) (
|
||||
}
|
||||
|
||||
func autocompleteMachineProvider(_ *cobra.Command, _ []string, _ string) ([]string, cobra.ShellCompDirective) {
|
||||
suggestions := make([]string, 0)
|
||||
for _, p := range provider2.GetAll() {
|
||||
providers := provider2.GetAll()
|
||||
suggestions := make([]string, 0, len(providers))
|
||||
for _, p := range providers {
|
||||
suggestions = append(suggestions, p.VMType().String())
|
||||
}
|
||||
return suggestions, cobra.ShellCompDirectiveNoFileComp
|
||||
|
||||
@@ -135,9 +135,8 @@ func loadUnitDropins(unit *parser.UnitFile, sourcePaths []string) error {
|
||||
prevError = err
|
||||
}
|
||||
|
||||
dropinDirs := []string{}
|
||||
unitDropinPaths := unit.GetUnitDropinPaths()
|
||||
|
||||
dropinDirs := make([]string, 0, len(unitDropinPaths))
|
||||
for _, dropinPath := range unitDropinPaths {
|
||||
for _, sourcePath := range sourcePaths {
|
||||
dropinDirs = append(dropinDirs, path.Join(sourcePath, dropinPath))
|
||||
|
||||
@@ -464,7 +464,7 @@ func (c *Container) StaticDir() string {
|
||||
// The name of each is guaranteed to point to a valid libpod Volume present in
|
||||
// the state.
|
||||
func (c *Container) NamedVolumes() []*ContainerNamedVolume {
|
||||
volumes := []*ContainerNamedVolume{}
|
||||
volumes := make([]*ContainerNamedVolume, 0, len(c.config.NamedVolumes))
|
||||
for _, vol := range c.config.NamedVolumes {
|
||||
newVol := new(ContainerNamedVolume)
|
||||
newVol.Name = vol.Name
|
||||
|
||||
@@ -441,7 +441,7 @@ type YAMLContainer struct {
|
||||
|
||||
// ConvertV1PodToYAMLPod takes k8s API core Pod and returns a pointer to YAMLPod
|
||||
func ConvertV1PodToYAMLPod(pod *v1.Pod) *YAMLPod {
|
||||
cs := []*YAMLContainer{}
|
||||
cs := make([]*YAMLContainer, 0, len(pod.Spec.Containers))
|
||||
for _, cc := range pod.Spec.Containers {
|
||||
var res *v1.ResourceRequirements
|
||||
if len(cc.Resources.Limits) > 0 || len(cc.Resources.Requests) > 0 {
|
||||
|
||||
@@ -263,7 +263,7 @@ func GetLimits(resource *spec.LinuxResources) (runcconfig.Resources, error) {
|
||||
final.Devices = devs
|
||||
|
||||
// HugepageLimits
|
||||
pageLimits := []*runcconfig.HugepageLimit{}
|
||||
pageLimits := make([]*runcconfig.HugepageLimit, 0, len(resource.HugepageLimits))
|
||||
for _, entry := range resource.HugepageLimits {
|
||||
pageLimits = append(pageLimits, &runcconfig.HugepageLimit{
|
||||
Pagesize: entry.Pagesize,
|
||||
|
||||
@@ -163,7 +163,7 @@ func createContainerWaitFn(ctx context.Context, containerName string, interval t
|
||||
var containerEngine entities.ContainerEngine = &abi.ContainerEngine{Libpod: runtime}
|
||||
|
||||
return func(conditions ...define.ContainerStatus) (int32, error) {
|
||||
var rawConditions []string
|
||||
rawConditions := make([]string, 0, len(conditions))
|
||||
for _, con := range conditions {
|
||||
rawConditions = append(rawConditions, con.String())
|
||||
}
|
||||
|
||||
@@ -37,7 +37,7 @@ func GenerateContainerFilterFuncs(filter string, filterValues []string, r *libpo
|
||||
case "name":
|
||||
// we only have to match one name
|
||||
return func(c *libpod.Container) bool {
|
||||
var filters []string
|
||||
filters := make([]string, 0, len(filterValues))
|
||||
for _, f := range filterValues {
|
||||
filters = append(filters, strings.ReplaceAll(f, "/", ""))
|
||||
}
|
||||
|
||||
@@ -210,7 +210,7 @@ func (ic *ContainerEngine) VolumeMounted(_ context.Context, nameOrID string) (*e
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) VolumeMount(_ context.Context, nameOrIDs []string) ([]*entities.VolumeMountReport, error) {
|
||||
reports := []*entities.VolumeMountReport{}
|
||||
reports := make([]*entities.VolumeMountReport, 0, len(nameOrIDs))
|
||||
for _, name := range nameOrIDs {
|
||||
report := entities.VolumeMountReport{Id: name}
|
||||
vol, err := ic.Libpod.LookupVolume(name)
|
||||
@@ -226,7 +226,7 @@ func (ic *ContainerEngine) VolumeMount(_ context.Context, nameOrIDs []string) ([
|
||||
}
|
||||
|
||||
func (ic *ContainerEngine) VolumeUnmount(_ context.Context, nameOrIDs []string) ([]*entities.VolumeUnmountReport, error) {
|
||||
reports := []*entities.VolumeUnmountReport{}
|
||||
reports := make([]*entities.VolumeUnmountReport, 0, len(nameOrIDs))
|
||||
for _, name := range nameOrIDs {
|
||||
report := entities.VolumeUnmountReport{Id: name}
|
||||
vol, err := ic.Libpod.LookupVolume(name)
|
||||
|
||||
@@ -598,7 +598,7 @@ func (ic *ContainerEngine) ContainerAttach(ctx context.Context, nameOrID string,
|
||||
}
|
||||
|
||||
func makeExecConfig(options entities.ExecOptions) *handlers.ExecCreateConfig {
|
||||
env := []string{}
|
||||
env := make([]string, 0, len(options.Envs))
|
||||
for k, v := range options.Envs {
|
||||
env = append(env, fmt.Sprintf("%s=%s", k, v))
|
||||
}
|
||||
|
||||
@@ -78,7 +78,7 @@ func getPolicyShowOutput(policyContentStruct policyContent, systemRegistriesDirP
|
||||
|
||||
// descriptionsOfPolicyRequirements turns reqs into user-readable policy entries, with Transport/Name/Reponame coming from template, potentially looking up scope (which may be "") in registryConfigs.
|
||||
func descriptionsOfPolicyRequirements(reqs []repoContent, template Policy, registryConfigs *registryConfiguration, scope string, idReader gpgIDReader) []*Policy {
|
||||
res := []*Policy{}
|
||||
res := make([]*Policy, 0, len(reqs))
|
||||
|
||||
var lookasidePath string
|
||||
registryNamespace := registriesDConfigurationForScope(registryConfigs, scope)
|
||||
|
||||
@@ -612,13 +612,13 @@ func getAvailableIDRanges(fullRanges, usedRanges [][2]int) (availableRanges [][2
|
||||
// multirange of unassigned subordinated ids.
|
||||
func getAvailableIDRangesFromMappings(idmap []idtools.IDMap, parentMapping []ruser.IDMap) (availableRanges [][2]int) {
|
||||
// Get all subordinated ids from parentMapping:
|
||||
fullRanges := [][2]int{} // {Multirange: [start, end), [start, end), ...}
|
||||
fullRanges := make([][2]int, 0, len(parentMapping)) // {Multirange: [start, end), [start, end), ...}
|
||||
for _, mapPiece := range parentMapping {
|
||||
fullRanges = append(fullRanges, [2]int{int(mapPiece.ID), int(mapPiece.ID + mapPiece.Count)})
|
||||
}
|
||||
|
||||
// Get the ids already mapped:
|
||||
usedRanges := [][2]int{}
|
||||
usedRanges := make([][2]int, 0, len(idmap))
|
||||
for _, mapPiece := range idmap {
|
||||
usedRanges = append(usedRanges, [2]int{mapPiece.HostID, mapPiece.HostID + mapPiece.Size})
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user