unofficial mirror of bug-gnu-emacs@gnu.org 
 help / color / mirror / code / Atom feed
From: "Basil L. Contovounesios" <contovob@tcd.ie>
To: 46761@debbugs.gnu.org
Subject: bug#46761: 28.0.50; Speed up json.el encoding
Date: Thu, 25 Feb 2021 18:21:20 +0000	[thread overview]
Message-ID: <87ft1kgg9r.fsf@tcd.ie> (raw)
In-Reply-To: <87a6rtszgk.fsf@tcd.ie> (Basil L. Contovounesios's message of "Thu, 25 Feb 2021 01:33:47 +0000")

[-- Attachment #1: Type: text/plain, Size: 745 bytes --]

"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:


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: json.diff --]
[-- Type: text/x-diff, Size: 1167 bytes --]

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)

[-- Attachment #3: Type: text/plain, Size: 498 bytes --]


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	other threads:[~2021-02-25 18:21 UTC|newest]

Thread overview: 11+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2021-02-25  1:33 bug#46761: 28.0.50; Speed up json.el encoding Basil L. Contovounesios
2021-02-25 18:21 ` Basil L. Contovounesios [this message]
2021-02-25 19:36   ` Basil L. Contovounesios
2021-03-06 18:37     ` Basil L. Contovounesios
2021-02-27 18:51 ` Dmitry Gutov
2021-02-27 19:30   ` Basil L. Contovounesios
2021-02-27 20:01     ` Dmitry Gutov
2021-02-27 21:09       ` Basil L. Contovounesios
2021-02-27 21:10         ` Dmitry Gutov
2021-02-27 21:16   ` Basil L. Contovounesios
2021-02-27 21:21     ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/emacs/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87ft1kgg9r.fsf@tcd.ie \
    --to=contovob@tcd.ie \
    --cc=46761@debbugs.gnu.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this public inbox

	https://git.savannah.gnu.org/cgit/emacs.git

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).