You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Performance wise this performs very poorly, due to two reasons:
Lots of temporary allocations:
1.1. StringWriter
1.2. XmlDomTextWriter (which allocates a 6000 char buffer)
1.3 Final string
String comparison instead of comparing primitive types.
Is your feature request related to a problem? Please describe.
Due to the amount of allocations, we are seeing tons of garbage collection happening when creating Excel sheets in our service.
When creating the sheets we have attach styles for the sheet, but to avoid re-adding styles, we compare them to previously added styles.
Describe the solution you'd like
Would be nice if OpenXmlElement implemented IEquatable.
However, due to potential wishes for controlling ordering behaviour I could imagine someone wanting a more complex API, but I'm okay with any of them.
Describe alternatives you've considered
Tried to implement a faster comparison based on writing to a stream and comparing the underlying bytes instead.
using (var ms0 = new MemoryStream(256))
using (var ms1 = new MemoryStream(256))
using (XmlWriter writer = XmlWriter.Create(ms0, xwSettings))
using (XmlWriter writer2 = XmlWriter.Create(ms0, xwSettings))
{
font.WriteTo(writer);
font.WriteTo(writer2);
writer.Flush();
writer2.Flush();
if (ms0.Position == ms1.Position)
{
byte[] buf1 = ms0.GetBuffer();
byte[] buf2 = ms1.GetBuffer();
buf1.AsSpan(0, (int)ms0.Position).SequenceEqual(ms1.GetBuffer().AsSpan(0, (int)ms1.Position));
}
}
I have also tried to do the above, but reusing the XmlWriter and MemoryStream, which reduces the allocations and time significantly. However StackOverflow warns of reuse.
However a slightly naive attempt to recurse the children and attributes show there is more to gain. It could be even faster if the GetAttributes didn't allocate a new list.
When comparing OpenXmlElement objects, there isn't an obvious performant way to do it.
The current top solution from StackOverflow does:
Performance wise this performs very poorly, due to two reasons:
1.1. StringWriter
1.2. XmlDomTextWriter (which allocates a 6000 char buffer)
1.3 Final string
Is your feature request related to a problem? Please describe.
Due to the amount of allocations, we are seeing tons of garbage collection happening when creating Excel sheets in our service.
When creating the sheets we have attach styles for the sheet, but to avoid re-adding styles, we compare them to previously added styles.
Describe the solution you'd like
Would be nice if OpenXmlElement implemented IEquatable.
However, due to potential wishes for controlling ordering behaviour I could imagine someone wanting a more complex API, but I'm okay with any of them.
Describe alternatives you've considered
Tried to implement a faster comparison based on writing to a stream and comparing the underlying bytes instead.
I have also tried to do the above, but reusing the XmlWriter and MemoryStream, which reduces the allocations and time significantly. However StackOverflow warns of reuse.
However a slightly naive attempt to recurse the children and attributes show there is more to gain. It could be even faster if the GetAttributes didn't allocate a new list.
Additional context
Benchmarks:
The text was updated successfully, but these errors were encountered: