Skip to content

Commit

Permalink
This optimizes internal functions signature and mutex declarations
Browse files Browse the repository at this point in the history
  • Loading branch information
xuri committed Apr 23, 2023
1 parent 787453c commit 93c72b4
Show file tree
Hide file tree
Showing 20 changed files with 168 additions and 168 deletions.
4 changes: 2 additions & 2 deletions adjust.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,8 +60,8 @@ func (f *File) adjustHelper(sheet string, dir adjustDirection, num, offset int)
if err = f.adjustCalcChain(dir, num, offset, sheetID); err != nil {
return err
}
checkSheet(ws)
_ = checkRow(ws)
ws.checkSheet()
_ = ws.checkRow()

if ws.MergeCells != nil && len(ws.MergeCells.Cells) == 0 {
ws.MergeCells = nil
Expand Down
10 changes: 5 additions & 5 deletions calc.go
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ var (

// calcContext defines the formula execution context.
type calcContext struct {
sync.Mutex
mu sync.Mutex
entry string
maxCalcIterations uint
iterations map[string]uint
Expand Down Expand Up @@ -1553,19 +1553,19 @@ func (f *File) cellResolver(ctx *calcContext, sheet, cell string) (formulaArg, e
)
ref := fmt.Sprintf("%s!%s", sheet, cell)
if formula, _ := f.GetCellFormula(sheet, cell); len(formula) != 0 {
ctx.Lock()
ctx.mu.Lock()
if ctx.entry != ref {
if ctx.iterations[ref] <= f.options.MaxCalcIterations {
ctx.iterations[ref]++
ctx.Unlock()
ctx.mu.Unlock()
arg, _ = f.calcCellValue(ctx, sheet, cell)
ctx.iterationsCache[ref] = arg
return arg, nil
}
ctx.Unlock()
ctx.mu.Unlock()
return ctx.iterationsCache[ref], nil
}
ctx.Unlock()
ctx.mu.Unlock()
}
if value, err = f.GetCellValue(sheet, cell, Options{RawCellValue: true}); err != nil {
return arg, err
Expand Down
4 changes: 2 additions & 2 deletions calcchain.go
Original file line number Diff line number Diff line change
Expand Up @@ -58,8 +58,8 @@ func (f *File) deleteCalcChain(index int, cell string) error {
if err != nil {
return err
}
content.Lock()
defer content.Unlock()
content.mu.Lock()
defer content.mu.Unlock()
for k, v := range content.Overrides {
if v.PartName == "/xl/calcChain.xml" {
content.Overrides = append(content.Overrides[:k], content.Overrides[k+1:]...)
Expand Down
92 changes: 46 additions & 46 deletions cell.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,13 +236,13 @@ func (f *File) setCellTimeFunc(sheet, cell string, value time.Time) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.Unlock()
ws.mu.Lock()
c.S = ws.prepareCellStyle(col, row, c.S)
ws.mu.Unlock()
var date1904, isNum bool
wb, err := f.workbookReader()
if err != nil {
Expand Down Expand Up @@ -292,13 +292,13 @@ func (f *File) SetCellInt(sheet, cell string, value int) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
defer ws.Unlock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.mu.Lock()
defer ws.mu.Unlock()
c.S = ws.prepareCellStyle(col, row, c.S)
c.T, c.V = setCellInt(value)
c.IS = nil
return f.removeFormula(c, ws, sheet)
Expand All @@ -318,13 +318,13 @@ func (f *File) SetCellBool(sheet, cell string, value bool) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
defer ws.Unlock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.mu.Lock()
defer ws.mu.Unlock()
c.S = ws.prepareCellStyle(col, row, c.S)
c.T, c.V = setCellBool(value)
c.IS = nil
return f.removeFormula(c, ws, sheet)
Expand Down Expand Up @@ -355,13 +355,13 @@ func (f *File) SetCellFloat(sheet, cell string, value float64, precision, bitSiz
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
defer ws.Unlock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.mu.Lock()
defer ws.mu.Unlock()
c.S = ws.prepareCellStyle(col, row, c.S)
c.T, c.V = setCellFloat(value, precision, bitSize)
c.IS = nil
return f.removeFormula(c, ws, sheet)
Expand All @@ -381,13 +381,13 @@ func (f *File) SetCellStr(sheet, cell, value string) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
defer ws.Unlock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.mu.Lock()
defer ws.mu.Unlock()
c.S = ws.prepareCellStyle(col, row, c.S)
if c.T, c.V, err = f.setCellString(value); err != nil {
return err
}
Expand All @@ -413,8 +413,8 @@ func (f *File) setCellString(value string) (t, v string, err error) {
// sharedStringsLoader load shared string table from system temporary file to
// memory, and reset shared string table for reader.
func (f *File) sharedStringsLoader() (err error) {
f.Lock()
defer f.Unlock()
f.mu.Lock()
defer f.mu.Unlock()
if path, ok := f.tempFiles.Load(defaultXMLPathSharedStrings); ok {
f.Pkg.Store(defaultXMLPathSharedStrings, f.readBytes(defaultXMLPathSharedStrings))
f.tempFiles.Delete(defaultXMLPathSharedStrings)
Expand Down Expand Up @@ -443,8 +443,8 @@ func (f *File) setSharedString(val string) (int, error) {
if err != nil {
return 0, err
}
f.Lock()
defer f.Unlock()
f.mu.Lock()
defer f.mu.Unlock()
if i, ok := f.sharedStringsMap[val]; ok {
return i, nil
}
Expand Down Expand Up @@ -558,8 +558,8 @@ func (c *xlsxC) getCellDate(f *File, raw bool) (string, error) {
// intended to be used with for range on rows an argument with the spreadsheet
// opened file.
func (c *xlsxC) getValueFrom(f *File, d *xlsxSST, raw bool) (string, error) {
f.Lock()
defer f.Unlock()
f.mu.Lock()
defer f.mu.Unlock()
switch c.T {
case "b":
return c.getCellBool(f, raw)
Expand Down Expand Up @@ -600,13 +600,13 @@ func (f *File) SetCellDefault(sheet, cell, value string) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
ws.Lock()
defer ws.Unlock()
c.S = f.prepareCellStyle(ws, col, row, c.S)
ws.mu.Lock()
defer ws.mu.Unlock()
c.S = ws.prepareCellStyle(col, row, c.S)
c.setCellDefault(value)
return f.removeFormula(c, ws, sheet)
}
Expand Down Expand Up @@ -718,7 +718,7 @@ func (f *File) SetCellFormula(sheet, cell, formula string, opts ...FormulaOpts)
if err != nil {
return err
}
c, _, _, err := f.prepareCell(ws, cell)
c, _, _, err := ws.prepareCell(cell)
if err != nil {
return err
}
Expand Down Expand Up @@ -763,7 +763,7 @@ func (ws *xlsxWorksheet) setSharedFormula(ref string) error {
cnt := ws.countSharedFormula()
for c := coordinates[0]; c <= coordinates[2]; c++ {
for r := coordinates[1]; r <= coordinates[3]; r++ {
prepareSheetXML(ws, c, r)
ws.prepareSheetXML(c, r)
cell := &ws.SheetData.Row[r-1].C[c-1]
if cell.F == nil {
cell.F = &xlsxF{}
Expand Down Expand Up @@ -867,7 +867,7 @@ func (f *File) SetCellHyperLink(sheet, cell, link, linkType string, opts ...Hype
if err != nil {
return err
}
if cell, err = f.mergeCellsParser(ws, cell); err != nil {
if cell, err = ws.mergeCellsParser(cell); err != nil {
return err
}

Expand Down Expand Up @@ -944,7 +944,7 @@ func (f *File) GetCellRichText(sheet, cell string) (runs []RichTextRun, err erro
if err != nil {
return
}
c, _, _, err := f.prepareCell(ws, cell)
c, _, _, err := ws.prepareCell(cell)
if err != nil {
return
}
Expand Down Expand Up @@ -1171,14 +1171,14 @@ func (f *File) SetCellRichText(sheet, cell string, runs []RichTextRun) error {
if err != nil {
return err
}
c, col, row, err := f.prepareCell(ws, cell)
c, col, row, err := ws.prepareCell(cell)
if err != nil {
return err
}
if err := f.sharedStringsLoader(); err != nil {
return err
}
c.S = f.prepareCellStyle(ws, col, row, c.S)
c.S = ws.prepareCellStyle(col, row, c.S)
si := xlsxSI{}
sst, err := f.sharedStringsReader()
if err != nil {
Expand Down Expand Up @@ -1252,9 +1252,9 @@ func (f *File) setSheetCells(sheet, cell string, slice interface{}, dir adjustDi
}

// getCellInfo does common preparation for all set cell value functions.
func (f *File) prepareCell(ws *xlsxWorksheet, cell string) (*xlsxC, int, int, error) {
func (ws *xlsxWorksheet) prepareCell(cell string) (*xlsxC, int, int, error) {
var err error
cell, err = f.mergeCellsParser(ws, cell)
cell, err = ws.mergeCellsParser(cell)
if err != nil {
return nil, 0, 0, err
}
Expand All @@ -1263,9 +1263,9 @@ func (f *File) prepareCell(ws *xlsxWorksheet, cell string) (*xlsxC, int, int, er
return nil, 0, 0, err
}

prepareSheetXML(ws, col, row)
ws.Lock()
defer ws.Unlock()
ws.prepareSheetXML(col, row)
ws.mu.Lock()
defer ws.mu.Unlock()
return &ws.SheetData.Row[row-1].C[col-1], col, row, err
}

Expand All @@ -1277,7 +1277,7 @@ func (f *File) getCellStringFunc(sheet, cell string, fn func(x *xlsxWorksheet, c
if err != nil {
return "", err
}
cell, err = f.mergeCellsParser(ws, cell)
cell, err = ws.mergeCellsParser(cell)
if err != nil {
return "", err
}
Expand All @@ -1286,8 +1286,8 @@ func (f *File) getCellStringFunc(sheet, cell string, fn func(x *xlsxWorksheet, c
return "", err
}

ws.Lock()
defer ws.Unlock()
ws.mu.Lock()
defer ws.mu.Unlock()

lastRowNum := 0
if l := len(ws.SheetData.Row); l > 0 {
Expand Down Expand Up @@ -1366,7 +1366,7 @@ func (f *File) formattedValue(c *xlsxC, raw bool, cellType CellType) (string, er

// prepareCellStyle provides a function to prepare style index of cell in
// worksheet by given column index and style index.
func (f *File) prepareCellStyle(ws *xlsxWorksheet, col, row, style int) int {
func (ws *xlsxWorksheet) prepareCellStyle(col, row, style int) int {
if style != 0 {
return style
}
Expand All @@ -1387,7 +1387,7 @@ func (f *File) prepareCellStyle(ws *xlsxWorksheet, col, row, style int) int {

// mergeCellsParser provides a function to check merged cells in worksheet by
// given cell reference.
func (f *File) mergeCellsParser(ws *xlsxWorksheet, cell string) (string, error) {
func (ws *xlsxWorksheet) mergeCellsParser(cell string) (string, error) {
cell = strings.ToUpper(cell)
col, row, err := CellNameToCoordinates(cell)
if err != nil {
Expand Down
Loading

0 comments on commit 93c72b4

Please sign in to comment.