emacs-devel
[Top][All Lists]
Advanced

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

possible json.el optimization: json-alist-p and json-plist-p recursion


From: Ted Zlatanov
Subject: possible json.el optimization: json-alist-p and json-plist-p recursion
Date: Thu, 13 Oct 2011 09:51:11 -0400
User-agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.90 (gnu/linux)

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




reply via email to

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