dotnet / Open-XML-SDK

Open XML SDK by Microsoft

Home Page:https://www.nuget.org/packages/DocumentFormat.OpenXml/

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

[Performance] Reduce ElementFeatureCollection calls

Tornhoof opened this issue · comments

Is your feature request related to a problem? Please describe.
This is only a performance report of something I noticed during profiling.

Describe the solution you'd like
A recent profiling session of a simple XML file with ~100_000 lines (by ~10 columns) showed that

public static TFeature GetRequired<TFeature>(this IFeatureCollection features)
takes ~25% of the time of the actual WriteTo calls.

OptimizeFeatureExtensions OptimizeFeatureExtensions2

I expect that the time is simply that much as it is called for each cell multiple times, each individual call is likely very short, but it simply adds up.

Describe alternatives you've considered
n/a

Additional context
n/a

Thanks for the report. Can you provide a repro we can add to our benchmarks?

I'll see what I can do, I have yet to reproduce IT standalone.

I managed to repro it and it's a bug in the code I was profiling.
For each cell, it tried to get the current Column via:

   uint index = (uint) columnIndex + 1;
   column = columns.ChildElements.OfType<Column>().SingleOrDefault(a => a.Min! == index);

and apparently a new instance of OpenXmlElementList is returned each time and something in there triggered all these calls to GetRequired.

The obvious solution was to move the column logic out of the inner loop.

I apologize for the noise.