From 16fa8350ad55f8031b56e85ede473544d4485da8 Mon Sep 17 00:00:00 2001 From: Jackson Ray Hamilton Date: Sun, 31 May 2015 15:54:05 -0700 Subject: [PATCH] Optimize JSON parser. --- context-coloring.el | 44 ++++++++++++++++++++++------------- test/context-coloring-test.el | 2 +- 2 files changed, 29 insertions(+), 17 deletions(-) diff --git a/context-coloring.el b/context-coloring.el index 45868322f..753a7da1e 100644 --- a/context-coloring.el +++ b/context-coloring.el @@ -409,6 +409,7 @@ provide visually \"instant\" updates at 60 frames per second.") arg-string) (funcall callback arg-string)))) +;; TODO: These seem to spiral into an infinite loop sometimes. (defun context-coloring-elisp-parse-let-varlist (type) (let ((varlist '()) syntax-code) @@ -723,36 +724,47 @@ provide visually \"instant\" updates at 60 frames per second.") (save-excursion (context-coloring-elisp-colorize (point-min) (point-max))))) -(defalias 'ccecb 'context-coloring-elisp-colorize-buffer) - ;;; Shell command scopification / colorization (defun context-coloring-apply-tokens (tokens) - "Process a vector of TOKENS to apply context-based coloring to + "Process a list of TOKENS to apply context-based coloring to the current buffer. Tokens are 3 integers: start, end, level. -The vector is flat, with a new token occurring after every 3rd +The list is flat, with a new token occurring after every 3rd element." (with-silent-modifications - (let ((i 0) - (len (length tokens))) - (while (< i len) - (context-coloring-colorize-region - (elt tokens i) - (elt tokens (+ i 1)) - (elt tokens (+ i 2))) - (setq i (+ i 3)))) + (while tokens + (context-coloring-colorize-region + (prog1 (car tokens) (setq tokens (cdr tokens))) + (prog1 (car tokens) (setq tokens (cdr tokens))) + (prog1 (car tokens) (setq tokens (cdr tokens))))) (context-coloring-maybe-colorize-comments-and-strings))) (defun context-coloring-parse-array (array) "Parse ARRAY as a flat JSON array of numbers." - (let ((braceless (substring (context-coloring-trim array) 1 -1))) + (let ((braceless (substring-no-properties (context-coloring-trim array) 1 -1))) (cond ((> (length braceless) 0) - (vconcat - (mapcar 'string-to-number (split-string braceless ",")))) + (let* (;; Use a leading comma to simplify the below loop's + ;; delimiter-checking. + (chars (vconcat (concat "," braceless))) + (index (length chars)) + (number 0) + (multiplier 1) + numbers) + (while (> index 0) + (setq index (1- index)) + (cond + ((= (elt chars index) context-coloring-COMMA-CHAR) + (setq numbers (cons number numbers)) + (setq number 0) + (setq multiplier 1)) + (t + (setq number (+ number (* (- (elt chars index) 48) multiplier))) + (setq multiplier (* multiplier 10))))) + numbers)) (t - (vector))))) + (list))))) (defvar-local context-coloring-scopifier-process nil "The single scopifier process that can be running.") diff --git a/test/context-coloring-test.el b/test/context-coloring-test.el index 57264217d..a74ae6c52 100644 --- a/test/context-coloring-test.el +++ b/test/context-coloring-test.el @@ -423,7 +423,7 @@ override it." (context-coloring-define-dispatch 'define-dispatch-scopifier :modes '(context-coloring-test-define-dispatch-scopifier-mode) - :scopifier (lambda () (vector))) + :scopifier (lambda () (list))) (context-coloring-test-define-dispatch-scopifier-mode) (context-coloring-mode) (context-coloring-colorize))) -- 2.39.2