Skip to content

Commit

Permalink
Pre-allocate some memory when reading files (#510)
Browse files Browse the repository at this point in the history
  • Loading branch information
Michael authored and xuri committed Oct 24, 2019
1 parent e7581eb commit 9fe267f
Show file tree
Hide file tree
Showing 3 changed files with 12 additions and 7 deletions.
6 changes: 6 additions & 0 deletions excelize_test.go
Expand Up @@ -1278,3 +1278,9 @@ func fillCells(f *File, sheet string, colCount, rowCount int) {
}
}
}

func BenchmarkOpenFile(b *testing.B) {
for i := 0; i < b.N; i++ {
OpenFile(filepath.Join("test", "Book1.xlsx"))
}
}
11 changes: 5 additions & 6 deletions lib.go
Expand Up @@ -22,14 +22,12 @@ import (
// ReadZipReader can be used to read an XLSX in memory without touching the
// filesystem.
func ReadZipReader(r *zip.Reader) (map[string][]byte, int, error) {
fileList := make(map[string][]byte)
fileList := make(map[string][]byte, len(r.File))
worksheets := 0
for _, v := range r.File {
fileList[v.Name] = readFile(v)
if len(v.Name) > 18 {
if v.Name[0:19] == "xl/worksheets/sheet" {
worksheets++
}
if strings.HasPrefix(v.Name, "xl/worksheets/sheet") {
worksheets++
}
}
return fileList, worksheets, nil
Expand Down Expand Up @@ -58,7 +56,8 @@ func readFile(file *zip.File) []byte {
if err != nil {
log.Fatal(err)
}
buff := bytes.NewBuffer(nil)
dat := make([]byte, 0, file.FileInfo().Size())
buff := bytes.NewBuffer(dat)
_, _ = io.Copy(buff, rc)
rc.Close()
return buff.Bytes()
Expand Down
2 changes: 1 addition & 1 deletion rows_test.go
Expand Up @@ -695,8 +695,8 @@ func TestErrSheetNotExistError(t *testing.T) {
}

func BenchmarkRows(b *testing.B) {
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
for i := 0; i < b.N; i++ {
f, _ := OpenFile(filepath.Join("test", "Book1.xlsx"))
rows, _ := f.Rows("Sheet2")
for rows.Next() {
row, _ := rows.Columns()
Expand Down

0 comments on commit 9fe267f

Please sign in to comment.