python-docx-template icon indicating copy to clipboard operation
python-docx-template copied to clipboard

Optimize DocxTemplate for rerendering the same template multiple times

Open groves opened this issue 8 years ago • 0 comments

I have a single template that I render over and over again with different parameters in my program. It takes about half a second per render. I'm wondering if it'd be possible to do some of the parsing and transformation once and then render multiple times with that parsed state to lower the overall runtime.

A representative call to render for my template takes .53 seconds. Breaking down the components of that:

  • .24 seconds in build_xml
    • .16 seconds in get_xml and patch_xml
    • .08 seconds in render_xml
  • .26 seconds in map_tree

Of that, the .16 seconds in get and patch xml seem like they could be done once. I'm also wondering if the .26 seconds in map_tree could be made faster in lxml if we could guarantee the template were already in the namespace of the source document or something along those lines.

If I could do those pieces once, that would be ~80% of the render time, which would definitely be worthwhile for me. Assuming it didn't change the external API, would a patch to do that be interesting?

groves avatar Feb 04 '18 19:02 groves