qax-os / excelize

Go language library for reading and writing Microsoft Excel™ (XLAM / XLSM / XLSX / XLTM / XLTX) spreadsheets
https://xuri.me/excelize
BSD 3-Clause "New" or "Revised" License
17.62k stars 1.68k forks source link

876 #1935

Open zhayt opened 2 days ago

zhayt commented 2 days ago

PR Details

Optimizing memory usage by pre-allocation.

Description

  1. ColumnNumberToName Function:

    • The ColumnNumberToName function previously concatenated rows using the + operator, leading to significant memory allocation when called frequently.
    • The function now uses a pre-allocated slice to build the Excel column name. During construction, the converted number to letter is added to this slice. At the end, the slice of runes is converted to a string and returned.
  2. Sheet Pre-allocation:

    • Added an option to create a Sheet with pre-allocated rows.
    • The xlsxWorksheet structure now contains a field with a slice type. When calling the SetCell... function to add a row to the sheet, it in turn calls the prepareSheetXML function. This function adds an xlsxRow structure to the slice.
    • Previously, multiple calls to prepareSheetXML caused the slice to grow, allocating a lot of memory and performing unnecessary copying. The new approach pre-allocates the maximum number of rows, reducing memory allocations and copying overhead.

Related Issue

Motivation and Context

How Has This Been Tested

Types of changes

Checklist


Feel free to modify any part of this to better fit your needs!