Got an early copy of "Run Your Own Mail Server" written by @mwl and decided to check it out in #Emacs. Because, why not.
Initial load takes suspiciously long. Added some benchmarking code, it takes 3.939s to render the ToC page. This is unacceptably slow. All other pages render fast though...
Added more benchmarking code, eventually it dawned upon me: ToC rendering works by recursively walking the XML and converting it to equivalent HTML. On every re-render. This conversion takes a lot longer compared to actual rendering by shr.el. If the recursive helper function takes longer than a few milliseconds, things get noticeably slow.
Eventually, I found an obvious slowdown: The call to `xml-escape-string` is unacceptably slow. Switching it out for `url-insert-entities-in-string` improved the benchmark from 3.939s to 1.061s.
Impressive, but far from done. I found another optimization opportunity in the use of `esxml-query` in a tight loop producing tons of garbage (every GC took roughly 100ms). Rewrote the code to use `dom.el` and times have dropped from 1.061s to 0.01s :ablobcatheadbang:
I guess this means to never ever use `esxml-query` in a tight loop. To do better than that, I'd need to implement compiled selectors. But that would be fairly advanced #elisp hackery. Maybe another time...