GNU bug report logs -
#20154
25.0.50; json-encode-string is too slow for large strings
Previous Next
Reported by: Dmitry Gutov <dgutov <at> yandex.ru>
Date: Fri, 20 Mar 2015 14:27:01 UTC
Severity: normal
Found in version 25.0.50
Done: Dmitry Gutov <dgutov <at> yandex.ru>
Bug is archived. No further changes may be made.
Full log
Message #80 received at 20154 <at> debbugs.gnu.org (full text, mbox):
Here's the version I've arrived at:
(defun json-encode-string-3 (string)
"Return a JSON representation of STRING."
;; Reimplement the meat of `replace-regexp-in-string', for
;; performance (bug#20154).
(let ((l (length string))
(start 0)
(res (list "\"")))
;; Skip over ASCIIish printable characters.
(while (string-match "[\"\\/\b\f\n\r\t]\\|[^ -~]" string start)
(let* ((mb (match-beginning 0))
(c (aref string mb))
(special (rassoc c json-special-chars)))
(push (substring string start mb) res)
(push (if special
;; Special JSON character (\n, \r, etc.).
(string ?\\ (car special))
;; Fallback: UCS code point in \uNNNN form.
(format "\\u%04x" c))
res)
(setq start (1+ mb))))
(push (substring string start l) res)
(push "\"" res)
(apply #'concat (nreverse res))))
A bit slower than the temp-buffer version (90ms vs 80ms), but
consistently faster than the current version, even on small strings.
Probably the best we can do in Lisp.
If no one has any better ideas, I'm going to install it.
This bug report was last modified 10 years and 38 days ago.
Previous Next
GNU bug tracking system
Copyright (C) 1999 Darren O. Benham,
1997,2003 nCipher Corporation Ltd,
1994-97 Ian Jackson.