From 164b55be80350938e6a546176bb6c9252094d154 Mon Sep 17 00:00:00 2001 From: "Jeremy L. Rayman" Date: Fri, 27 Apr 2012 22:30:54 -0700 Subject: [PATCH] Double the speed of main jit-lock highlighting fn (see commit msg for benchmarks); eliminate bottleneck in large files. * Benchmarks: These benchmarks are after replacing parse-partial-sexp with syntax-ppss and using defsubst in place of defun in a few more critical paths. *** Ordinary section of code: (benchmark-run 10 (rainbow-delimiters-propertize-region 16800 19000)) 0.0403 0.0355 0.0315 0.0419 0.0319 0.0376 Before this round of optimizations (syntax-ppss+defsubst) this benchmark took ~0.068 seconds. (Improvement: ~2x) *** Segment where a bottleneck was hit earlier: (benchmark-run 10 (rainbow-delimiters-propertize-region 23800 26000)) 0.0842 0.0821 0.0905 0.0843 0.0784 0.0822 Before this round of optimizations (syntax-ppss+defsubst) this benchmark took ~3.2 seconds. (Improvement: ~40x) --- rainbow-delimiters.el | 12 ++++++++---- 1 file changed, 8 insertions(+), 4 deletions(-) diff --git a/rainbow-delimiters.el b/rainbow-delimiters.el index eebd75b..6bc004c 100644 --- a/rainbow-delimiters.el +++ b/rainbow-delimiters.el @@ -322,7 +322,7 @@ major-mode. The syntax table is constructed by the function (modify-syntax-entry ?\} "){" table) table)) -(defun rainbow-delimiters-depth (loc) +(defsubst rainbow-delimiters-depth (loc) "Return # of nested levels of parens, brackets, braces LOC is inside of." (let ((depth (with-syntax-table rainbow-delimiters-syntax-table @@ -395,7 +395,7 @@ Sets text properties: rear-nonsticky nil)))) -(defun rainbow-delimiters-char-ineligible-p (loc) +(defsubst rainbow-delimiters-char-ineligible-p (loc) "Return t if char at LOC should be skipped, e.g. if inside a comment. Returns t if char at loc meets one of the following conditions: @@ -430,11 +430,11 @@ LOC is location of character (delimiter) to be colorized." ;;; JIT-Lock functionality ;; Used to skip delimiter-by-delimiter `rainbow-delimiters-propertize-region'. -(defvar rainbow-delimiters-delim-regex "\\(\(\\|\)\\|\\[\\|\\]\\|\{\\|\}\\)" +(defconst rainbow-delimiters-delim-regex "\\(\(\\|\)\\|\\[\\|\\]\\|\{\\|\}\\)" "Regex matching all opening and closing delimiters the mode highlights.") ;; main function called by jit-lock: -(defun rainbow-delimiters-propertize-region (start end) +(defsubst rainbow-delimiters-propertize-region (start end) "Highlight delimiters in region between START and END. Used by jit-lock for dynamic highlighting." @@ -500,6 +500,10 @@ Used by jit-lock for dynamic highlighting." (defun rainbow-delimiters-mode-enable () (rainbow-delimiters-mode 1)) +;;;###autoload +(defun rainbow-delimiters-mode-disable () + (rainbow-delimiters-mode 0)) + ;;;###autoload (define-globalized-minor-mode global-rainbow-delimiters-mode rainbow-delimiters-mode rainbow-delimiters-mode-enable)