Skip to content

Performance improvement and reduction in memory allocation when dealing with Formatted cells #2674

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

Open
wants to merge 4 commits into
base: develop
Choose a base branch
from

Conversation

jafin
Copy link

@jafin jafin commented May 3, 2025

This PR adds a performance benchmarking and some performance gains and reduction in memory dealing with formatted cells (and cell cloning).

Performance Benchmarking and Testing

  • Added benchmark.net test project. With benchmark to validate performance change. Is marked excluded to avoid running with standard test suite.

Code refactoring.

Focused around some hotpaths dealing with formatted cells. As we use the Sister project ClosedXML.Reports and experience performance issues there. (Note: A lot of time for me is seen in FormatCell Set OutsideBorder , maybe there are some future savings there.

The changes primarily aim to reduce memory allocation and tweak performance.
In broad strokes they are:

  • reduce use of ToArray/ToList when not required to avoid allocation and GC perf.
  • change XLRepositoryBase to take in a WeakReference<Value> and reduce calls via TryGet, GetEnumerator also now yields.
  • CollectChildrenRecursively(XLStylizedBase parent) should be faster than the HashSet, reduces recursion and uses the Stack

Other

minor style changes - feel free to ignore. (Prefer modern language features, use keyword over type name) etc.

Testing results

Before changes

Method Mean Error StdDev Gen0 Gen1 Gen2 Allocated
CellFillAndCopy 53.68 ms 1.067 ms 1.783 ms 2222.2222 888.8889 222.2222 32.74 MB

image

After changes

Method Mean Error StdDev Gen0 Gen1 Allocated
CellFillAndCopy 46.76 ms 0.931 ms 1.393 ms 1727.2727 818.1818 28.53 MB

image

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant