dotnet / dotnet/Open-XML-SDK

Performance degradation for WordprocessingDocument (v3.1.0 vs v2.20.0 vs v2.5.0)

Open
#1,770 4 comments 0 reactions 1 assignee Claimed by @twsouthwick View on GitHub
Dominant language
C#
Stars
4.6k
Forks
605
PR merge metrics
No merged PRs in 30d

Description

**Describe the bug**
Updating a word document operation has degraded significantly in the latest version. In v2.5.0, updating a word document ([SampleDocument.docx](https://github.com/user-attachments/files/16490865/SampleDocument.docx)) takes on an average ~310ms but same operation takes around ~1610ms (5x) in v2.20.0 and ~1370ms (4x) in v3.1.0.

**To Reproduce**
Use below sample code with attached document ([SampleDocument.docx](https://github.com/user-attachments/files/16490865/SampleDocument.docx))

```

string filePath = @"SampleDocument.docx";
string outPath = $@"UpdatedSampleDocument.docx";
Stopwatch stopwatch = new Stopwatch();

var bytes = File.ReadAllBytes(filePath);
MemoryStream stream = new MemoryStream();
stream.Position = 0;
stream.Write(bytes, 0, bytes.Length);

stopwatch.Start();

// Open the document
using (WordprocessingDocument wordDocument = WordprocessingDocument.Open(stream, true))
{
wordDocument.MainDocumentPart.Document.Body.PrependChild(new Paragraph(new ParagraphProperties(), new Run(new Text(DateTime.Now.ToString()))));
wordDocument.MainDocumentPart.Document.Body.PrependChild(new Paragraph(new ParagraphProperties(), new Run(new Text(DateTime.Now.ToString()))));
wordDocument.MainDocumentPart.Document.Body.PrependChild(new Paragraph(new ParagraphProperties(), new Run(new Text(DateTime.Now.ToString()))));
wordDocument.MainDocumentPart.Document.Body.PrependChild(new Paragraph(new ParagraphProperties(), new Run(new Text(DateTime.Now.ToString()))));
wordDocument.MainDocumentPart.Document.Body.PrependChild(new Paragraph(new ParagraphProperties(), new Run(new Text(DateTime.Now.ToString()))));

//// Save the document

//// for v2.20.0
wordDocument.MainDocumentPart.Document.Save();
wordDocument.Package.Flush();

//// for v3.1.0 & v2.20.0
wordDocument.Save();
}

stopwatch.Stop();

File.WriteAllBytes(outPath, stream.ToArray());
stream.Close();
stream.Dispose();
Console.WriteLine($"Time taken to save the document: {stopwatch.ElapsedMilliseconds} ms");

```

**Observed behavior**
Upgrading to the latest version has significant performance degradation for mentioned operation.

**Expected behavior**
Upgrading to the latest version should not degrade performance.

**Desktop (please complete the following information):**
- OS: windows
- Office version: 16.0.17628.20188
- .NET Target: 4.8
- DocumentFormat.OpenXml Version: 2.5.0, 2.20.0, 3.1.0

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.