Patchwork [2,of,2] highlight: exit early on textual and unknown files (issue3005)

login
register
mail settings
Submitter Anton Shestakov
Date Aug. 3, 2015, 2:54 p.m.
Message ID <f64f5adf2e364fecd106.1438613645@neuro>
Download mbox | patch
Permalink /patch/10084/
State Accepted
Headers show

Comments

Anton Shestakov - Aug. 3, 2015, 2:54 p.m.
# HG changeset patch
# User Anton Shestakov <av6@dwimlabs.net>
# Date 1438514315 -28800
#      Sun Aug 02 19:18:35 2015 +0800
# Branch stable
# Node ID f64f5adf2e364fecd106a3a3e68012c01a33c108
# Parent  1426bfe350e2070e0fcaf7c0a083e4e385f83805
highlight: exit early on textual and unknown files (issue3005)

When highlight extension encountered files that pygments didn't recognize, it
used to fall back to text lexer. Also, pygments uses TextLexer for .txt files.
This lexer is noop by design.

On bigger files, however, doing the noop highlighting resulted in noticeable
extra CPU work and memory usage: to show a 1 MB text file, hgweb required about
0.7s more (on top of ~3.8s, Q8400) and consumed about 100 MB of RAM more (on
top of ~150 MB).

Let's just exit the function when it's clear that nothing will be highlighted.

Due to how this pygmentize function works (it modifies the template in-place),
we can just return from it and everything else will work as if highlight
extension wasn't enabled.
Pierre-Yves David - Aug. 4, 2015, 10:26 p.m.
On 08/03/2015 07:54 AM, Anton Shestakov wrote:
> # HG changeset patch
> # User Anton Shestakov <av6@dwimlabs.net>
> # Date 1438514315 -28800
> #      Sun Aug 02 19:18:35 2015 +0800
> # Branch stable
> # Node ID f64f5adf2e364fecd106a3a3e68012c01a33c108
> # Parent  1426bfe350e2070e0fcaf7c0a083e4e385f83805
> highlight: exit early on textual and unknown files (issue3005)

Patch 2 is pushed to the clowncopter, Thanks!

Patch

diff --git a/hgext/highlight/highlight.py b/hgext/highlight/highlight.py
--- a/hgext/highlight/highlight.py
+++ b/hgext/highlight/highlight.py
@@ -49,7 +49,12 @@  def pygmentize(field, fctx, style, tmpl)
         try:
             lexer = guess_lexer(text[:1024], stripnl=False)
         except (ClassNotFound, ValueError):
-            lexer = TextLexer(stripnl=False)
+            # Don't highlight unknown files
+            return
+
+    # Don't highlight text files
+    if isinstance(lexer, TextLexer):
+        return
 
     formatter = HtmlFormatter(nowrap=True, style=style)