Re-use compiled regex for block level checks by HebaruSan · Pull Request #1169 · Python-Markdown/markdown (original) (raw)

Problem

This page takes several seconds to load:

Cause

We've hit a somewhat exceptional case in that there are 76+ different markdown documents to render to HTML for that page (the author of the mod has been very prolific with creating releases, see the "changelog" list at the bottom).

With profiling enabled, it turns out that about one second is spent (re-)compiling a regex inside of isblocklevel, once per markdown document:

image

Other ideas

We (site developers) will probably take a number of other actions to address this slowness, such as paginating the releases and pre-rendering the markdown to HTML when saving to SQL rather than at render. But that regex still represents some low-hanging fruit for other users with many documents to render.

Changes

Now that regex is compiled once at startup and re-used in compiled form as needed. Should speed up rendering multiple documents modestly.

FYI to @DasSkelett, going to see how this goes.