use make slice to store objects to improve efficiency

Signed-off-by: allencloud <allen.sun@daocloud.io>
pull/6/head
allencloud 2017-05-05 23:25:56 +08:00
parent 4e74c43e9a
commit 503c19aec3
5 changed files with 8 additions and 7 deletions

View File

@ -350,7 +350,7 @@ func (ds *dockerService) ContainerStatus(containerID string) (*runtimeapi.Contai
imageID := toPullableImageID(r.Image, ir)
// Convert the mounts.
mounts := []*runtimeapi.Mount{}
mounts := make([]*runtimeapi.Mount, 0, len(r.Mounts))
for i := range r.Mounts {
m := r.Mounts[i]
readonly := !m.RW

View File

@ -40,7 +40,7 @@ func (ds *dockerService) ListImages(filter *runtimeapi.ImageFilter) ([]*runtimea
return nil, err
}
result := []*runtimeapi.Image{}
result := make([]*runtimeapi.Image, 0, len(images))
for _, i := range images {
apiImage, err := imageToRuntimeAPIImage(&i)
if err != nil {

View File

@ -187,7 +187,7 @@ func (ds *dockerService) ListLegacyPodSandbox(filter *runtimeapi.PodSandboxFilte
}
// Convert docker containers to runtime api sandboxes.
result := []*runtimeapi.PodSandbox{}
result := make([]*runtimeapi.PodSandbox, 0, len(containers))
for i := range containers {
c := containers[i]
// Skip new containers with containerTypeLabelKey label.
@ -242,7 +242,7 @@ func (ds *dockerService) ListLegacyContainers(filter *runtimeapi.ContainerFilter
}
// Convert docker to runtime api containers.
result := []*runtimeapi.Container{}
result := make([]*runtimeapi.Container, 0, len(containers))
for i := range containers {
c := containers[i]
// Skip new containers with containerTypeLabelKey label.

View File

@ -335,7 +335,7 @@ func (ds *dockerService) GetPodPortMappings(podSandboxID string) ([]*hostport.Po
}
}
portMappings := []*hostport.PortMapping{}
portMappings := make([]*hostport.PortMapping, 0, len(checkpoint.Data.PortMappings))
for _, pm := range checkpoint.Data.PortMappings {
proto := toAPIProtocol(*pm.Protocol)
portMappings = append(portMappings, &hostport.PortMapping{

View File

@ -127,7 +127,8 @@ func extractLabels(input map[string]string) (map[string]string, map[string]strin
// '<HostPath>:<ContainerPath>:ro', if the path is read only, or
// '<HostPath>:<ContainerPath>:Z', if the volume requires SELinux
// relabeling and the pod provides an SELinux label
func generateMountBindings(mounts []*runtimeapi.Mount) (result []string) {
func generateMountBindings(mounts []*runtimeapi.Mount) []string {
result := make([]string, 0, len(mounts))
for _, m := range mounts {
bind := fmt.Sprintf("%s:%s", m.HostPath, m.ContainerPath)
readOnly := m.Readonly
@ -147,7 +148,7 @@ func generateMountBindings(mounts []*runtimeapi.Mount) (result []string) {
}
result = append(result, bind)
}
return
return result
}
func makePortsAndBindings(pm []*runtimeapi.PortMapping) (map[dockernat.Port]struct{}, map[dockernat.Port][]dockernat.PortBinding) {