diff options
author | Ted <37789839+Theodoree@users.noreply.github.com> | 2020-11-03 17:48:37 +0800 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-11-03 17:48:37 +0800 |
commit | fcca8a38389c7a7f99639dc142b9b10c827ac7ce (patch) | |
tree | aa0e2f264153a793f1157c23959a9c140fa1bf67 /sheet.go | |
parent | 9d470bb38f992d9f0da2168b7a576f9e212b7a88 (diff) |
optimize memory allocation (#722)
* optimize marshal
* optimize mem alloc
* add benchmark testing
* add NewSheetWithRowNum testing
* sync struct fields order
* add BenchmarkNewSheetWithStreamWriter
* delete NewSheetWithRowNum and benchmark test
Diffstat (limited to 'sheet.go')
-rw-r--r-- | sheet.go | 12 |
1 files changed, 10 insertions, 2 deletions
@@ -120,18 +120,26 @@ func (f *File) workBookWriter() { // workSheetWriter provides a function to save xl/worksheets/sheet%d.xml after // serialize structure. func (f *File) workSheetWriter() { + + // optimize memory alloc + var arr []byte + buffer := bytes.NewBuffer(arr) + encoder := xml.NewEncoder(buffer) + for p, sheet := range f.Sheet { if sheet != nil { for k, v := range sheet.SheetData.Row { f.Sheet[p].SheetData.Row[k].C = trimCell(v.C) } - output, _ := xml.Marshal(sheet) - f.saveFileList(p, replaceRelationshipsBytes(f.replaceNameSpaceBytes(p, output))) + // reusing buffer + encoder.Encode(sheet) + f.saveFileList(p, replaceRelationshipsBytes(f.replaceNameSpaceBytes(p, buffer.Bytes()))) ok := f.checked[p] if ok { delete(f.Sheet, p) f.checked[p] = false } + buffer.Reset() } } } |