Skip to content

Commit

Permalink
Remove excessive memory usage during image processing
Browse files Browse the repository at this point in the history
  • Loading branch information
ChrisCinelli committed Nov 29, 2018
1 parent 7d77439 commit 8e4ea7c
Showing 1 changed file with 5 additions and 14 deletions.
19 changes: 5 additions & 14 deletions image/image.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@ package image
import (
"archive/tar"
"bufio"
"bytes"
"encoding/json"
"fmt"
"io"
Expand Down Expand Up @@ -126,11 +125,11 @@ func NewImageConfig(configBytes []byte) ImageConfig {
return imageConfig
}

func processLayerTar(line *jotframe.Line, layerMap map[string]*filetree.FileTree, name string, tarredBytes []byte) {
func processLayerTar(line *jotframe.Line, layerMap map[string]*filetree.FileTree, name string, reader *tar.Reader) {
tree := filetree.NewFileTree()
tree.Name = name

fileInfos := getFileList(tarredBytes)
fileInfos := getFileList(reader)

shortName := name[:15]
pb := NewProgressBar(int64(len(fileInfos)))
Expand Down Expand Up @@ -230,14 +229,8 @@ func InitializeData(imageID string) ([]*Layer, []*filetree.FileTree, float64, fi
shortName := name[:15]
io.WriteString(line, " ├─ "+shortName+" : loading...")

var tarredBytes = make([]byte, header.Size)

_, err = tarReader.Read(tarredBytes)
if err != nil && err != io.EOF {
logrus.Panic(err)
}

go processLayerTar(line, layerMap, name, tarredBytes)
layerReader := tar.NewReader(tarReader)
processLayerTar(line, layerMap, name, layerReader)
} else if strings.HasSuffix(name, ".json") {
var fileBuffer = make([]byte, header.Size)
n, err = tarReader.Read(fileBuffer)
Expand Down Expand Up @@ -378,11 +371,9 @@ func saveImage(imageID string) (string, string) {
return imageTarPath, tmpDir
}

func getFileList(tarredBytes []byte) []filetree.FileInfo {
func getFileList(tarReader *tar.Reader) []filetree.FileInfo {
var files []filetree.FileInfo

reader := bytes.NewReader(tarredBytes)
tarReader := tar.NewReader(reader)
for {
header, err := tarReader.Next()

Expand Down

0 comments on commit 8e4ea7c

Please sign in to comment.