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

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

bug#46761: 28.0.50; Speed up json.el encoding


From: Basil L. Contovounesios
Subject: bug#46761: 28.0.50; Speed up json.el encoding
Date: Thu, 25 Feb 2021 18:21:20 +0000
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/28.0.50 (gnu/linux)

"Basil L. Contovounesios" <contovob@tcd.ie> writes:

> The attached patch speeds up json-encode by inserting into a buffer
> rather than concatenating strings.  It does so backward compatibly by
> creating a new json--print-* namespace that mirrors the existing
> json-encode-* namespace, cleaning it up a bit and reducing code
> duplication in the process.
>
> Using my usual benchmark from bug#40693#89:
>
>   canada.json
>   old (1.412693239 96 0.736882091)
>   new (1.154423962 32 0.248241551)
>
>   citm_catalog.json
>   old (0.676292855 68 0.5285956769999993)
>   new (0.306573098 12 0.0965493740000003)
>
>   twitter.json
>   old (0.353447016 40 0.28536439900000055)
>   new (0.142140227  8 0.05943713899999992)

This additional change:

diff --git a/lisp/json.el b/lisp/json.el
index eb655162d3..461f688f9c 100644
--- a/lisp/json.el
+++ b/lisp/json.el
@@ -199,6 +199,8 @@ json--with-output-to-string
      (with-current-buffer standard-output
        ;; This affords decent performance gains.
        (setq-local inhibit-modification-hooks t)
+       ;; Ignore `read-only' property once and for all (bug#43549).
+       (setq-local inhibit-read-only t)
        ,@body)))
 
 (defmacro json--with-indentation (&rest body)
@@ -458,9 +460,9 @@ json-read-string
 (defun json--print-string (string &optional from)
   "Insert a JSON representation of STRING at point.
 FROM is the index of STRING to start from and defaults to 0."
-  (goto-char (prog1 (1+ (point))
-               ;; Strip `read-only' property (bug#43549).
-               (insert ?\" (substring-no-properties string from))))
+  (goto-char (prog1 (1+ (point)) (insert ?\" string)))
+  (set-text-properties (point) (point-max) ())
+  (and from (delete-char from))
   ;; Escape only quotation mark, backslash, and the control
   ;; characters U+0000 to U+001F (RFC 4627, ECMA-404).
   (while (re-search-forward (rx (in ?\" ?\\ cntrl)) nil 'move)
improves things slightly further (slight inconsistencies with the above
are because my laptop's currently on battery power):

  canada.json
  old (1.450930341 96 0.7616264250000002)
  new (1.161926076 32 0.24752529000000045)

  citm_catalog.json
  old (0.686048204 68 0.5394565070000006)
  new (0.267222201  6 0.048179708000000154)

  twitter.json
  old (0.362725099 40 0.2935560630000005)
  new (0.099399607  2 0.01469844000000009)

And yes, I have added a test case for this locally.

-- 
Basil

reply via email to

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