bug-gnu-emacs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

bug#20154: 25.0.50; json-encode-string is too slow for large strings


From: Dmitry Gutov
Subject: bug#20154: 25.0.50; json-encode-string is too slow for large strings
Date: Sat, 21 Mar 2015 00:26:03 +0200
User-agent: Mozilla/5.0 (X11; Linux x86_64; rv:36.0) Gecko/20100101 Thunderbird/36.0

As per your comment, this seems to be the best we can do for long strings, without diving into C:

(defun json-encode-string-1 (string)
  "Return a JSON representation of STRING."
  (with-temp-buffer
    (insert string)
    (goto-char (point-min))
    ;; Skip over ASCIIish printable characters.
    (while (re-search-forward "\\([\"\\/\b\f\n\r\t]\\)\\|[^ -~]" nil t)
      (replace-match
       (if (match-beginning 1)
           ;; Special JSON character (\n, \r, etc.).
           (format "\\%c" (char-after (match-beginning 0)))
         ;; Fallback: UCS code point in \uNNNN form.
         (format "\\u%04x" (char-after (match-beginning 0))))
       t t))
    (format "\"%s\"" (buffer-string))))

It brings the execution time down to ~0.14s here, on the same example.

And there'll need to be a fallback for short strings, because `with-temp-buffer' overhead is non-trivial.





reply via email to

[Prev in Thread] Current Thread [Next in Thread]