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

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

Open
bhargavgaglani07 opened this issue Aug 5, 2024 · 2 comments
Assignees

Comments

@bhargavgaglani07
Copy link

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) 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)


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
@ChristianSfr
Copy link

The regression doesn't seem specific to the type of generated document.

I have the same observation when generating a spreadsheet of 3600 lines of 14 columns.
Same Net8 code, 45s with 3.1.0 instead of 13s with 3.0.2.

@twsouthwick
Copy link
Member

I took a look with a profiler, and it appears that a large amount of the time is spent with JIT here, probably due to the generic builder pattern I put in 2.11. The perf testing I had done had always warmed things up and removed the JIT stage, but in a single one off, it's quite a large impact. I'm going to explore removing it.... it causes other issues such as #753

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

No branches or pull requests

3 participants