t_fischer: (Default)
[personal profile] t_fischer

Many years ago I wrote a parser for KBibTeX for KDE3, which would load plain text BibTeX files into an internal data structure in memory. Part of this process is to interpret strings like {\"a} or --- and to protect inline math commands. Internally, this encoder/decoder would have a long table of mappings between LaTeX representations and Unicode representations. Detecting, extracting, and replacing LaTeX representations with Unicode characters was primarily realized using regular expressions.
After ruling out most bugs, it worked well. Indeed, this piece of code has survived into KBibTeX for KDE4 as of today.

Inspired by a recent thread on excessive CPU usage, I got the recommendation to try out Intel's VTune Amplifier which is among others a CPU profiler.
Of course I was eager to experiment with this tool. One use case I tested was to open a single BibTeX file with about 300 entries and exiting KBibTeX right after.

VTune, having a graphical user interface, allowed me to visualize the spent CPU time and track down the hotspot as shown in the screenshot below:

CPU profiling – before optimization

Apart from the program start, represented by the main function, the single most expensive function is EncoderLaTeX::decode. It makes heavy use of regular expressions, which as anyone with some theoretical computer science knows are both powerful tools but computationally expensive.

It became obvious that only a rewrite of this class could improve the situation. I spent most of last Sunday for the rewrite plus the last two evenings for some debugging and adding comments to the code. The new version has been commited to SVN as of revision 930.
The new code may still lack support for some border cases (as every rewrite does), but it is not only well-documented, but also considerable faster. How much faster shows the screenshot from VTune below, running the same use case as above:

CPU profiling – after optimization

Now, the decode function cause negligible costs to parse the same file. That's what I call an improvement ;-)

Profile

t_fischer: (Default)
Thomas Fischer

September 2017

S M T W T F S
     12
34567 89
10111213141516
17181920212223
24252627282930

Most Popular Tags

Style Credit

Expand Cut Tags

No cut tags