siefkenj / unified-latex

Utilities for parsing and manipulating LaTeX ASTs with the Unified.js framework
MIT License
85 stars 20 forks source link

Parser takes an excessive amount of time for particular file #47

Closed dwillhalm closed 11 months ago

dwillhalm commented 11 months ago

I have a document with a bit more than 300 lines of definitions that takes an excessive amount of time to parse. Testing it on the playground raises the same issue. It seems to be a bug in which the parser cannot handle the definitions of begin and end environment very well. Below I copied all lines of the file.

\newcommand{\abs}[1]{|#1|}
\newcommand{\norm}[1]{|#1|}
\newcommand{\blue}[1]{{\textcolor{wiasblue}{{\bf \emph{#1}}}}{}}
\newcommand{\bblue}[1]{{\textcolor{wiasblue}{{\bfseries \emph{#1}}}}{}}
\newcommand{\green}[1]{{\textcolor{wiasgreen}{{\bf \emph{#1}}}}{}}
\newcommand{\mlu}[1]{{\textcolor{mlugreen}{{\bf \emph{#1}}}}{}}
\newcommand{\white}[1]{{\textcolor{white}{{\bf \emph{#1}}}}{}}
\newcommand{\red}[1]{{\textcolor{red}{{\bf \emph{#1}}}}{}}

\newcommand{\tblue}[1]{\text{\blue{$#1$}}}
\newcommand{\tgreen}[1]{\text{\green{$#1$}}}
\newcommand{\tred}[1]{\text{\red{$#1$}}}
\newcommand{\tblack}[1]{\text{{$#1$}}}
\newcommand\ime[1]{\item\emph{#1}}
\newcommand\bo[1]{{#1}}
\newcommand\bq[1]{{\bfseries  #1}}
\newcommand\bp[1]{{\bf #1}}
%\newcommand\br[1]{\noindent{\textcolor[rgb]{0.2,0.4,0.65}{\underline{\bfseries #1}}}}
\newcommand\pps[1]{\prod_{\psi \su#1}}

\newcounter{itemrefcounter}
\makeatletter
\newcommand\myitem[1][]{\item[#1]\refstepcounter{itemrefcounter}\renewcommand\@currentlabel{#1}}
\makeatother

%\setbeamertemplate{bibliography item}{[\theenumiv]}

\renewcommand\ni{\noindent}
\newcommand\E{\mathbb E}
\newcommand\M{\mathbb M}
\newcommand\K{\mathbb K}
\renewcommand\P{\mathbb P}
\newcommand\Q{\mathbb Q}
\newcommand\R{\mathbb R}
\newcommand\Z{\mathbb Z}
\newcommand\mc{\mathcal}
\newcommand\ms{\mathsf}
\newcommand\mf{\mathfrak}
\newcommand\mb{\mathbf}

\renewcommand\c{\mathsf{c}}

\newcommand\rsa{\rightsquigarrow}
\newcommand\lrsa{\leftrightsquigarrow}
\newcommand\la{\lambda}
\newcommand\La{\Lambda}
\renewcommand\a{\alpha}
\newcommand\pha{\phantom{}\\}
\newcommand\Bi{\Big}
\newcommand\ld{\dots}
\newcommand\cd{\cdots}
\newcommand\s{\sigma}
\newcommand\su{\subseteq}
\newcommand\bs{\boldsymbol}
\newcommand\e{\varepsilon}
\renewcommand\t{\tau}
\newcommand\g{\gamma}
\renewcommand\b{\beta}
\newcommand\de{\delta}
\newcommand\ol{\overline}
\newcommand\om{\omega}
\newcommand\Om{\Omega}
\newcommand\es{\varnothing}
\newcommand\one{\mathbbmss{1}}
\newcommand\tw{\textcolor{white}{.}}
\newcommand\Gl{G^{\ms L}}
\newcommand\Gr{G^{\ms R}}
\newcommand\Gd{G^\De}
\newcommand\De{\Delta}
\newcommand\Del{\ms{Del}}
\newcommand\G{\Gamma}
\newcommand\const{\ms{const}}
\newcommand\ff{\infty}
\newcommand\tff{\uparrow\infty}
\newcommand\Ra{\Rightarrow}
\newcommand\lc{\lceil}
\newcommand\rc{\rceil}
\newcommand\mt{\mapsto}
\newcommand\vp{\varphi}
\newcommand\vps{\varphi^{\ms s}}
\newcommand\vpp{\varphi^{\ms p}}
\newcommand\Poi{\ms{Poi}}
\newcommand\Ber{\ms{Ber}}
\newcommand\Exp{\ms{Exp}}
\newcommand\Leb{\ms{Leb}}
\renewcommand\d{{\rm d}}
\renewcommand\k{\kappa}
\newcommand\EE{\mc E}
\newcommand\GG{\mc G}
\newcommand\YY{\mc Y}
\newcommand\LL{\mc L}
\newcommand\HH{\mc H}
\newcommand\MM{\mc M}
\newcommand\NN{\mc N}
\newcommand\BB{\mc B}
\newcommand\PP{\mc P}
\newcommand\FF{\mc F}
\newcommand\CC{\mc C}
\newcommand\CCC{\mb C}
\newcommand\tb{\mb t}
\newcommand\DD{\mc D}
\newcommand\ddd{\mathfrak d}
\newcommand\XX{\mc X}
\newcommand\WW{\mc W}
\renewcommand\AA{\mc A}
\newcommand\VV{\mc V}
\newcommand\UUU{\mc U}
\newcommand\NU{N(\UUU)}
\newcommand\dmmm{\de_{\mu, m}}
\newcommand\dmm{\de_{\mu, m}(x)}
\newcommand\dmmp{\de_{\mu_P, m}(x)}
\newcommand\dm{d_{\mu, m_0}(x)}
\newcommand\Rd{\R^d}
\newcommand\PPP{\mb P}
\newcommand\TT{\mc T}
\newcommand\FFo{\mc F^o}
\renewcommand\lc{\lceil}
\renewcommand\rc{\rceil}
\newcommand\lf{\lfloor}
\newcommand\rf{\rfloor}
\newcommand\f{\frac}
\newcommand\lan{\langle}
\newcommand\ran{\rangle}
\newcommand\xra{\xrightarrow}
\newcommand\off{[0,\ff)}
\newcommand\U{\ms{Unif}(0, 1)}
\newcommand\fs{\footnotesize}
\renewcommand\r{\rho}
\newcommand\ua{\uparrow}
\newcommand\be{\begin}
\newcommand\en{\end}
\newcommand\im{\item}
\newcommand\sm{\setminus}
\renewcommand\th{\theta}
\newcommand\bep{\begin{proof}}
\newcommand\enp{\end{proof}}
\newcommand\bepr{\begin{proposition}}
\newcommand\enpr{\end{proposition}}
\newcommand\bec{\begin{corollary}}
\newcommand\enc{\end{corollary}}
\newcommand\bea{\begin{align}}
\newcommand\eea{\end{align}}
\newcommand\beas{\begin{align*}}
\newcommand\eeas{\end{align*}}
\newcommand\bet{\begin{theorem}}
\newcommand\ent{\end{theorem}}
\newcommand\bee{\begin{example}}
\newcommand\ene{\end{example}}
\newcommand\zz{\ms z}
\newcommand\da{\downarrow}
%\newcommand\bede{\begin{definition}}
%\def\ende{\end{definition}}
\newcommand\ber{\begin{remark}}
\newcommand\enr{\end{remark}}
\newcommand\beca{\begin{cases}}
\newcommand\enca{\end{cases}}
\newcommand\bel{\begin{lemma}}
\renewcommand\enl{\end{lemma}}
\newcommand\been{\begin{enumerate}}
\newcommand\enen{\end{enumerate}}
\newcommand\li{\liminf}
\newcommand\ls{\limsup}
\newcommand\w{\wedge}
%\newcommand\i{{\ms i}}
\renewcommand\S{\mathbb S}
\newcommand\Si{\Sigma}
\renewcommand\SS{\mc S}
\renewcommand\dim{\ms{dim}}
\newcommand\beit{\begin{itemize}}
\newcommand\enit{\end{itemize}}
\newcommand\befr{\begin{frame}}
\newcommand\enfr{\end{frame}}
\newcommand\ti{\times}
\newcommand\di{\ms{disc}}
\newcommand\Esu{E_{\ms{surv}}}
\renewcommand\L{\mathbb L}
\newcommand\Var{\ms{Var}}
\newcommand\Cov{\ms{Cov}}
\newcommand\tf{\tfrac}
\newcommand\ba{\,|\,}
\newcommand\bfe{\bf \emph}
\newcommand\bi{\big}
\newcommand\vX{\hat X}
\newcommand\vx{\hat x}
\newcommand\vy{\hat y}
\newcommand\Xb{\mb X}
\newcommand\Xs{X^{\ms s}}
\newcommand\Xp{X^{\ms p}}
\newcommand\Xps{X^{\ms{ps}}}
\newcommand\Xss{\hat X^{\ms s}}
\newcommand\diam{\ms{diam}}
\renewcommand\ker{\ms{ker}}
\renewcommand\Im{\ms{Im}}
\newcommand\Ib{I_{\ms{b}}}
\newcommand\wt{\widetilde}
\newcommand\wh{\widehat}
\newcommand\entr{\ms{ent}}
\newcommand\Cech{\ms{ Cech}}
\newcommand\VR{\ms{VR}}
\newcommand\supp{\ms{supp}}
\renewcommand\le{\leqslant}
\renewcommand\ge{\geqslant}
\newcommand\Sd{\ms{Sd}}

\newcommand\X{\mathbb X}
\newcommand\Y{\mathbb Y}
\newcommand\x{x}
\newcommand\bX{{\bs X}}
\newcommand\bN{{\mathbf N}}
\newcommand\bth{{\bs \th}}
\newcommand\bbth{{\bar \th}}
\newcommand\nukin{\nu_{k, i, n}}
\newcommand\pikin{\pi_{k, i, n}}
\newcommand\Xkin{\bar X_{k, i, n}}
\newcommand\bx{{\bs x}}
\newcommand\bxi{\bar X^{i, N}}
\newcommand\na{\nabla}
\newcommand\xx{{\bs x}}
\renewcommand\aa{{\bs a}}
\newcommand\UU{\bs U}
\newcommand\y{y}
\newcommand\yy{{\bs y}}
\newcommand\bH{H(\bX; \bth)}
\newcommand\bU{\bs U}
\newcommand\nat{\nabla_{\bth}}
\newcommand\nal{\widehat{\na\ell}(\bth)}
\newcommand\z{\zeta}
\newcommand\zi{\z_{\ms{in}}}
\newcommand\zm{\z_{\ms M}}
\newcommand\zo{\z_{\ms{out}}}
\newcommand\xind{\xi_n^\Delta}
\newcommand\xinda{\xi_n^{\Delta, 1}}
\newcommand\xindb{\xi_n^{\Delta, 2}}
\newcommand\xindc{\xi_n^{\Delta, 3}}
\newcommand\Nlf{\ms N_{\ms{lf}}}
\newcommand\NNlf{\NN_{\ms{lf}}}
\newcommand\Nf{N_{\ms f}}
\newcommand\NNf{\NN_{\ms f}}
\newcommand\argmax{\ms{argmax}}
\newcommand\Thide{}
\newcommand\Prob{\P}
\newcommand\Efr{E_{\ms b}}
\newcommand\Era{E_{\ms c}}
\newcommand\dd{\,{\rm d}}
\newcommand\PL{\ms{PL}}
\newcommand\var{\Var}
\newcommand\KK{\ms{K}}
\newcommand\dist{\ms{dist}}
\newcommand\expr{I}
\newcommand\becbb{\begin{center}\begin{tcolorbox}[{colback=Dandelion!20}]}
\newcommand\encbb{\end{tcolorbox}\end{center}}
\newcommand\becb{\begin{center}\begin{tcbox}[{colback=Dandelion!20}]}
\newcommand\encb{\end{tcbox}\end{center}}
\newcommand\tcbm{\tcboxmath[colback=Dandelion!20]}
\newcommand\tcm{\tcboxmath[colback=Dandelion!20]}
\newcommand{\Pnr}{\mathcal P_n|}
\newcommand\low{\ms{low}}
\newcommand\Bd{\ms{Bd}}
\newcommand\anc{\ms{anc}}
\newcommand\lw{\leftarrow}
\newcommand\id{\ms{id}}
\newcommand\bef{\begin{figure}[!h]}
\newcommand\enf{\end{figure}}
\newcommand\bels{\begin{lstlisting}}
\newcommand\enls{\end{lstlisting}}
\newcommand\gu{g_{\ms u}}
\newcommand\betp{\begin{tikzpicture}}
\newcommand\entp{\end{tikzpicture}}
\newcommand\co{\colon}
%\def\endo{\end{document}}
\newcommand\fm{\bf}
\newcommand\Inf{\ms{Inf}}
\newcommand\Piv{\ms{Piv}}
\newcommand\Pivd{\ms{Piv}^{\ms {del}}}
\newcommand\cmm{c_{\ms{MM}}}
\newcommand\Pivs{\Piv^{\ms s}}
\newcommand\Pivp{\Piv^{\ms p}}
\newcommand\Pivq{\Piv'}
\newcommand\pcm{{\bfseries HYTEC}}
\newcommand\pa{\partial}
\newcommand\PD{\ms{PD}}
\newcommand\bcu{\bigcup}
\newcommand\law{\la^{\ms f}}
\newcommand\lau{\la^{\ms w}}
%\newcommand\las{\la_{\ms S}}
\newcommand\las{\la^{\ms{cy}}}
\newcommand\lass{\la_{\ms{Del}}}
\newcommand\last{\la_{\ms s}}
\newcommand\lap{\la_{\ms p}}
\newcommand\lad{\la_{\ms{Del}}}
\newcommand\lav{\la^{\ms{vac}}}
\newcommand\Tb{T_{0,b }}
\renewcommand\it{\item}
\renewcommand\labelitemi{$\rhd$}
\renewcommand\mt{\mapsto}
\newcommand\lra{\Leftrightarrow}
\newcommand\ZZ{\mc Z}
\newcommand\RR{\mc R}
\newcommand\ot{[0, T]}
\newcommand\lac{\la_c}
\newcommand\Rips{\ms{Rips}}
\newcommand\N{\mathbb N}
\newcommand\dxx{\d\xx}
\newcommand\uu{\bs u}
\newcommand\Xd{X^{\Del}}
\newcommand\Ld{L_{\Del}}
\newcommand\wde{w_{\Del}}
\newcommand\ap{\a_{\ms p}}
\newcommand\rstab{r_{\ms{stab}}}

\renewcommand\Rips{\ms{VR}}
\newcommand\Comp{\ms{Comp}}
\newcommand\II{\mc A^{(m)}}
\newcommand\cdo{c_{\ms{Dom}}}
\newcommand\blitz{{\Large \text{\red\Lightning}}}
\newcommand\by{{\bs y}}
\renewcommand\bx{{\bs x}}
\newcommand\ells{\hat\ell^{(s)}}
\newcommand\Mt{\M \ti [0, T]}
\newcommand\mub{\bar\mu}
\newcommand\thb{\bar\th}
\newcommand\rb{\bar\rho}
\newcommand\vpb{\bar\vp}
\newcommand\Th{\Theta}
\newcommand\QQ{\mc Q}
\newcommand\tpn{\tilde\Phi_{\mathbf C^n}}
\renewcommand\lra{\leftrightarrow}
\renewcommand\wh{\widehat}
\newcommand\dtv{d_{\ms{TV}}}
siefkenj commented 11 months ago

I have analyzed your source, and it appears the slowness is coming from using commands like

\newcommand\bep{\begin{proof}}
\newcommand\enp{\end{proof}}

since as soon as the parser sees a \begin it scans for the corresponding \end. Removing those definitions makes parsing go much faster.

I will look into what can be done in general to speed things up.

siefkenj commented 11 months ago

This has been fixed in 1.4.2 :-)