From: Ted Zlatanov <tzz@lifelogs.com>
To: emacs-devel@gnu.org
Cc: Edward O'Connor <ted@oconnor.cx>
Subject: possible json.el optimization: json-alist-p and json-plist-p recursion
Date: Thu, 13 Oct 2011 09:51:11 -0400 [thread overview]
Message-ID: <877h49t2y8.fsf@lifelogs.com> (raw)
I ran into a very deep recursion with `json-encode' because
`json-alist-p' is unnecessarily recursive. This is not a bug, just
something that can be optimized because (it seems) Emacs Lisp doesn't do
good tail recursion optimization in this case.
#+begin_src lisp
(defun json-alist-p (list)
"Non-null if and only if LIST is an alist."
(or (null list)
(and (consp (car list))
(json-alist-p (cdr list)))))
#+end_src
I wanted to ask if this was an OK replacement:
#+begin_src lisp
(defun gnus-sync-json-alist-p (list)
"Non-null if and only if LIST is an alist."
(let ((p list))
(while (consp p)
(setq p (if (consp (car-safe p))
(cdr p)
'not-alist)))
(null p)))
#+end_src
`json-plist-p' needs a similar treatment:
#+begin_src lisp
(defun json-plist-p (list)
"Non-null if and only if LIST is a plist."
(or (null list)
(and (keywordp (car list))
(consp (cdr list))
(json-plist-p (cddr list)))))
#+end_src
Could be:
#+begin_src lisp
(defun gnus-sync-json-plist-p (list)
"Non-null if and only if LIST is a plist."
(let ((p list))
(while (consp p)
(setq p (if (and (keywordp (car-safe list))
(consp (cdr-safe p)))
(cddr p)
'not-plist)))
(null p)))
#+end_src
I don't know json.el so maybe I missed something subtle. CC to Edward
O'Connor. Let me know and I'll install in trunk if this is OK.
Thanks
Ted
next reply other threads:[~2011-10-13 13:51 UTC|newest]
Thread overview: 9+ messages / expand[flat|nested] mbox.gz Atom feed top
2011-10-13 13:51 Ted Zlatanov [this message]
2011-10-13 14:31 ` possible json.el optimization: json-alist-p and json-plist-p recursion Stefan Monnier
2011-10-13 16:07 ` Ted Zlatanov
2011-10-14 14:09 ` Ted Zlatanov
2011-10-14 14:58 ` Stefan Monnier
2011-10-14 16:33 ` Ted Zlatanov
2011-10-14 17:02 ` Edward O'Connor
2011-10-17 17:41 ` Ted Zlatanov
2011-10-13 16:02 ` Edward O'Connor
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
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=877h49t2y8.fsf@lifelogs.com \
--to=tzz@lifelogs.com \
--cc=emacs-devel@gnu.org \
--cc=ted@oconnor.cx \
/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 external index
https://git.savannah.gnu.org/cgit/emacs.git
https://git.savannah.gnu.org/cgit/emacs/org-mode.git
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.