Improve speed of emails-on-push with syntax highlighting
From https://gitlab.com/gitlab-org/gitlab-ce/issues/15137#note_4818400:
It looks like the premailer gem is trying to generate an HTML page from a Nokogiri document, which causes an excessive amount of CPU and memory usage. I think the goal of this gem is to generate an HTML e-mail with inline CSS, since e-mail providers generally don't allow stylesheets.
Based on the perf trace and the Sidekiq log, it looks like the Nokogiri search function calls the libxml2 routine xmlXPathNodeSetMergeAndClear
. This message on the libxml2 mailing list sums up the problem with this routine:
The problem here is that the evaluator finds several matches for "xs:complexType", searches each subtree, and then goes into merging the subresults and removing duplicates along the way, using a quadratic algorithm. The runtime of this approach quickly explodes with the number of nodes in the node set, especially since it can get applied several times while going up the expression tree.
This problem manifests particularly with the e-mails on push diffs, which generate a ton of inline CSS (a possible reason for gitlab-org/gitlab-ce#15178).