Skip to content

Commit

Permalink
fix: copy to static array instead of append, for less ram usage
Browse files Browse the repository at this point in the history
Signed-off-by: the-other-mariana <[email protected]>
  • Loading branch information
the-other-mariana committed Nov 14, 2022
1 parent 0b7d1cc commit d782119
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 8 deletions.
Binary file removed src/cylf
Binary file not shown.
14 changes: 6 additions & 8 deletions src/cylf.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,9 +80,9 @@ func merge(name, folder string){
fmt.Printf("[SUCCESS] Files found: %v\n", len(pieces))
numberOfPieces := uint64(len(pieces))

// variable to store the accumulated bytes of every piece processed
// variable to store the accumulated amount of bytes of every piece processed
var accSize int64 = 0
//var curr []byte
fileNames := make([]string, numberOfPieces, numberOfPieces)
for i := uint64(0); i < numberOfPieces; i++ {
currFile := baseFilename + "_" + strconv.FormatUint(i, 10) + ".cylf"
file, err := os.Open(currFile)
Expand All @@ -93,17 +93,15 @@ func merge(name, folder string){
os.Exit(1)
}
defer file.Close()

fileNames[i] = currFile
}
bArray := make([]byte, accSize, accSize)
var t int64 = 0
for i := uint64(0); i < numberOfPieces; i++ {
currFile := baseFilename + "_" + strconv.FormatUint(i, 10) + ".cylf"
file, err := os.Open(currFile)
file, err := os.Open(fileNames[i])
fileInfo, _ := file.Stat()
var fileSize int64 = fileInfo.Size()
//b := make([]byte, fileSize)
// send byte array of current file to b variable
// send byte array of current file to corresponding space in array
file.Read(bArray[t:t+fileSize])
if err != nil {
fmt.Printf("[ERROR] %v\n", err)
Expand All @@ -112,7 +110,7 @@ func merge(name, folder string){
defer file.Close()
t += fileSize
}
// store the accumulated bytes into a file
// store the filled array of bytes into a file
err := ioutil.WriteFile(mergedFile, bArray, 0644)
if err != nil {
fmt.Printf("[ERROR] %v\n", err)
Expand Down

0 comments on commit d782119

Please sign in to comment.