summaryrefslogtreecommitdiff
path: root/sheet.go
diff options
context:
space:
mode:
authorTed <37789839+Theodoree@users.noreply.github.com>2020-11-03 17:48:37 +0800
committerGitHub <noreply@github.com>2020-11-03 17:48:37 +0800
commitfcca8a38389c7a7f99639dc142b9b10c827ac7ce (patch)
treeaa0e2f264153a793f1157c23959a9c140fa1bf67 /sheet.go
parent9d470bb38f992d9f0da2168b7a576f9e212b7a88 (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.go12
1 files changed, 10 insertions, 2 deletions
diff --git a/sheet.go b/sheet.go
index 8da2e89..caf87d9 100644
--- a/sheet.go
+++ b/sheet.go
@@ -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()
}
}
}