Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Reduce memory consumption of Worksheet::rangeToArray() when many empty rows are read #3834

Merged
merged 2 commits into from
Dec 21, 2023

Conversation

sj-i
Copy link
Contributor

@sj-i sj-i commented Dec 19, 2023

This is:

  • a bugfix
  • a new feature
  • refactoring
  • additional unit tests
  • performance improvement

Checklist:

  • Changes are covered by unit tests
    • Changes are covered by existing unit tests
    • New unit tests have been added
  • Code style is respected
  • Commit message explains why the change is made (see https://github.com/erlang/otp/wiki/Writing-good-commit-messages)
  • CHANGELOG.md contains a short summary of the change and a link to the pull request if applicable
  • Documentation is updated as necessary

Why this change is needed?

There may be cases where some data is located "far away" in a sheet, like AMJ1048576, and the rest of the rows are mostly empty. This change dramatically reduces the memory usage in such cases. For example, currently using Worksheet::toArray() with example.xlsx in #3814 eats up over 20GB of memory in PHP 8.2. After this change, the memory consumption becomes less than 32MB.

So, this fixes #3814

@oleibman
Copy link
Collaborator

@sj-i This is very impressive analysis and an ingenious solution. I need to study it a little more to understand it better, but my intention is to merge it within the next day or two.

@oleibman oleibman added this pull request to the merge queue Dec 21, 2023
Merged via the queue into PHPOffice:master with commit 9f87d7c Dec 21, 2023
14 checks passed
@oleibman
Copy link
Collaborator

Thank you for your contribution, and for the lesson about CoW.

oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Dec 21, 2023
There are severe inefficiencies in `toArray` (or, to be more precise, `rangeToArray`, which toArray calls). For small ranges, the inefficiencies hardly matter, and, for large ranges, the code was likely to exhaust memory before the execution speed mattered. But, with PR PHPOffice#3834 now merged into the codebase, the memory problem is much lessened, and there is now merit in looking into performance.

The code currently accesses each cell in the range. This PR changes it to access only cells which have been initialized - fewer cells processed, and many fewer function calls for those that are. For ranges which are densely filled, there will be little difference. However, for ranges which are lightly filled (a very common occurrence in issues which are raised for this project), the effect will be staggering. The sample file which accompanied 3834 saw an enormous reduction in memory consumption from 20GB to 32MB when toArray was used on it. The same file still took well over an hour to process even after the merge; after this PR, it is processed in approximately a second.

While testing this change, it became apparent that Collection/Cells does not handle column XFD (highest allowed) correctly. That is corrected and new tests added.
@oleibman oleibman mentioned this pull request Dec 21, 2023
12 tasks
oleibman added a commit to oleibman/PhpSpreadsheet that referenced this pull request Feb 17, 2024
Fix PHPOffice#3904. PR PHPOffice#3839 provided a huge performance boost for sparsely populated spreadsheets. Unfortunately, it degraded performance for more densely populated spreadsheets when writing Csvs. The reason is that Csv Writer calls toArray for each row, meaning that a lot of the intermediate data used to speed things up needs to be recalculated for every row. It would be better off calling toArray just once for the entire spreadsheet; however this gives back some of the memory improvements of PR PHPOffice#3834. However, the memory effects can be substantially alleviated by supplying a Generator function to do the work. This PR does that; the result is that Csv Writer is now quite a bit faster, and with only a small memory uptick, vs. its performance in PhpSpreadsheet 1.29.
@oleibman oleibman mentioned this pull request Feb 17, 2024
11 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

Allowed memory size of X bytes exhausted in PhpSpreadsheet\Worksheheet\Worksheet.php on line 3108
2 participants