Merge pull request #45421 from allencloud/change-to-use-make-slice-to-store-objects

Automatic merge from submit-queue

use make slice to store objects to improve efficiency

Signed-off-by: allencloud <allen.sun@daocloud.io>



**What this PR does / why we need it**:

we we know the slice length in advance, I think we had better use make to create the specified length of slice. This will improve some kind of performance. Since if we create a slice with []type{}, we did not know how much space runtime should reserve, since slice implementation should be continuous in memory. While when we make a slice with specified length, runtime would reserve a continuous memory space which will not result in slice movement in case of current space is not enough.

**Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes #
NONE

**Special notes for your reviewer**:
NONE

**Release note**:

```release-note
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-05-30 06:27:18 -07:00 committed by GitHub
commit 20ec8912d0
5 changed files with 8 additions and 7 deletions

View File

@ -342,7 +342,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

@ -43,7 +43,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

@ -332,7 +332,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

@ -129,7 +129,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
@ -149,7 +150,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) {