sergey-tihon / Clippit

Fresh PowerTools for OpenXml
https://sergey-tihon.github.io/Clippit/
MIT License
50 stars 19 forks source link

feat: improved memory consumption for PresentationBuilder.PublishSlides #44

Closed f1nzer closed 2 years ago

f1nzer commented 2 years ago

The improvement consists of two parts: 1) Slide's title is retrieved from the generated slide rather than from the original one. There is no need to load and parse the content for the slide twice (once for the original slide and once for the new one during generation) 2) The source XDoc for generating a slide is retrieved using slide.GetXDocument(), then it saves the XDocument in the .Annotation<XDocument>(). So, there is a direct reference from the original slide to the new one. That's why the generated slides are not collected by GC (they were collected after the original PresentationDocument disposal).

You can see the difference in memory results below (execution is paused before srcDoc disposal).

Code:

[Fact]
public void MemoryTest()
{
    var sourcePath = @"TESTFILE.pptx";
    var openSettings = new OpenSettings { AutoSave = false };

    using (var srcStream = File.Open(sourcePath, FileMode.Open))
    using (var srcDoc = OpenXmlExtensions.OpenPresentation(srcStream, false, openSettings))
    {
        var index = 1;
        foreach (var slide in PresentationBuilder.PublishSlides(srcDoc, sourcePath))
        {
            index++;
        }
    }
}

Before: devenv_m3dFruyAZF

After: devenv_0nBlFZiHFo

sergey-tihon commented 2 years ago

Perfect! Thank you @f1nzer ! Released as v1.8.2