deeplook / svglib

Read SVG files and convert them to other formats.
GNU Lesser General Public License v3.0
311 stars 80 forks source link

Unexpected delay in embedding svg to pdf while upgrading svglib to 1.2.1 #338

Closed salistha-shakya closed 2 years ago

salistha-shakya commented 2 years ago

I am currently using svglib 0.9.0 and reportlab to create pdf reports from svg images and other information. While I wanted to upgrade to the latest version of svglib 1.2.1, I notice that the creation of reports in the latest version is much slower than in the older version. This has greatly impacted the overall performance. Can anyone assist me with this issue?

The minimal code for testing is provided below:

from reportlab.pdfgen.canvas import Canvas
from reportlab.platypus import Table
from reportlab.lib.pagesizes import A4
from svglib.svglib import svg2rlg
import time

def page_body(w, h):
    image_path = <PATH_TO_SVG>
    drawing = svg2rlg(image_path)
    res = Table([[drawing]], w, h)
    res.setStyle(
        [
            ("ALIGN", (0, 0), (-1, -1), "CENTER"),
            ("VALIGN", (0, 0), (-1, -1), "MIDDLE"),
        ]
    )
    return res

def create_report():
    startTime = time.time()
    canvas = Canvas("page.pdf", pagesize=A4)
    canvas.setTitle("SVG Example")
    # table
    width, height = A4
    page = Table(
        [
            [
                "",
                page_body(width - 2 * 50, 0.78 * height),
                "",
            ]
        ],
        colWidths=[50, (width - 2 * 50), 50],
        rowHeights=[0.78 * height],
    )
    page.setStyle(
        [
            ("LEFTPADDING", (0, 0), (-1, -1), 0),
            ("BOTTOMPADDING", (0, 0), (-1, -1), 0),
        ]
    )
    page.wrapOn(canvas, 0, 0)
    page.drawOn(canvas, 0, 0)
    canvas.showPage()
    canvas.save()
    print("Time taken =", time.time() - startTime)

Environment:

Ubuntu Python 3.9 reportlab==3.6.9

Test run output:

svglib 0.9.0 Time taken = 0.05732107162475586 svglib 0.9.3 Time taken = 0.06111264228820801 svglib 1.1.0 Time taken = 2.747704267501831 svglib 1.2.1 Time taken = 2.7683348655700684

github-actions[bot] commented 2 years ago

Thank you for raising your first issue! Your help to improve svglib is much appreciated!

claudep commented 2 years ago

Note that it might be related to the type of SVG tested, so you should also provide a SVG file which demonstrates the performance regression.

deeplook commented 2 years ago

You can try using a profiling tool, like some listed here https://medium.com/@narenandu/profiling-and-visualization-tools-in-python-89a46f578989.

salistha-shakya commented 2 years ago

Note that it might be related to the type of SVG tested, so you should also provide a SVG file which demonstrates the performance regression.

The below image can be taken as a sample SVG image which can roughly be around 35 - 40 KB in size.

image

claudep commented 2 years ago

Thanks, I was able to bisect the performance regression coming from 83d2ec043e094.

claudep commented 2 years ago

It would be nice if you could test the patch in #339