Free Support Forum - aspose.com

Improve API performance for parsing and traversing through SVGDocument

Hi, While converting a PDF document to SVG and traversing through that document I notice that we are consuming a lot of memory for operations like Saving svgdocument to png stream, getting total length of svg path element, computing x,y coordinates of svgpath etc. Resharper’s profiler is showing me memory allocations at around 6 GBs for these operations. Please find code snippets for these operations below.

Converting SVGDocument to PNG stream
image.png (218.2 KB)

and the code that i wrote is
foreach ( var filePath in filePaths )
{
using (var document = new Document(filePath))
{
foreach ( var page in document.Pages )
{
// Extract each page as a separate pdf document for conversion
using (var pageDocument = new Document())
{
pageDocument.Pages.Add(page);
pageDocument.Flatten();

                        // Convert page into svg document for manipulation
                        using ( var ms = new MemoryStream() )
                        {
                            pageDocument.Save(ms, SaveFormat.Svg);
                            //Reset position of stream to beginning
                            ms.Seek(0, SeekOrigin.Begin);

                            var svgDocument = new SVGDocument(ms, ".");

                            //Reset position of stream to beginning
                            ms.Seek(0, SeekOrigin.Begin);

                            using ( var pngStream = new MemoryStream() )
                            {
                                using ( var image = Aspose.Imaging.Image.Load(ms) )
                                {
                                    image.Save(pngStream, new PngOptions()
                                    {
                                        VectorRasterizationOptions = new SvgRasterizationOptions()
                                    });

                                    svgPageTextures.Add((image.Width, image.Height, pngStream.ToArray()));
                                }
                            }

                            svgDocumentData.Add(((int) svgDocument.RootElement.Width.BaseVal.Value, (int) svgDocument.RootElement.Height.BaseVal.Value, svgDocument));
                        }
                    }
                }
            }
        }

Parsing SVG to extract coordinates of SVGPath elements,
image.png (235.3 KB)
and this is how i get those points
foreach ( SVGPathElement pathElement in pathElements )
{
var pathPattern = pathElement.GetAttribute(“d”).Replace(“L”, " L").Trim(’ ', ‘Z’, ‘z’);
var segments = pathPattern.Count(x => x == ‘L’);
var totalLength = pathElement.GetTotalLength();
var ctm = pathElement.GetScreenCTM();
var firstPoint = pathElement.GetPointAtLength(0);
var lastPoint = pathElement.GetPointAtLength(totalLength);

				// Similarly for points along the path element
				var pathSegList = pathElement.PathSegList;
                var lengthPerSegment = totalLength / pathSegList.Length;
                var points = new List<DblVector2D>();
				for ( ulong i = 0 ; i < pathSegList.Length ; i++ )
				{
					var point = pathElement.GetPointAtLength(lengthPerSegment * i);
					points.Add(new DblVector2D( point.X, point.Y));
				}
			}

Can anyone help optimizing this ?

After drilling down with resharper i see that internally, String.concat is being called multiple times which is ending up creating a lot of garbage, can String builder or a better implementation be used here. It should fix most if not all of the problems I am having ?

image.png (760.4 KB)

This is just one such example, i found multiple such occurrences amounting to a lot of memory being allocated and garbage collected.

@chiraagbangera

Would you kindly share the sample file used in the shared code snippet. We will test the scenario in our environment and address it accordingly.

Hi @asad.ali Please refer to the pdf document below, converting pdf-> svg and then saving svg -> to png or while parsing png using the mentioned methods for getting PathLength or point at length is where the issues are.
Sample_Tech_Pack_Advanced.pdf (637.4 KB)

@chiraagbangera

We have logged an issue as SVG-106 in our issue tracking system for further investigation. We will look into its details and keep you posted with the status of its correction. Please be patient and spare us some time.

We are sorry for the inconvenience.