all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
* possible json.el optimization: json-alist-p and json-plist-p recursion
@ 2011-10-13 13:51 Ted Zlatanov
  2011-10-13 14:31 ` Stefan Monnier
  2011-10-13 16:02 ` Edward O'Connor
  0 siblings, 2 replies; 9+ messages in thread
From: Ted Zlatanov @ 2011-10-13 13:51 UTC (permalink / raw)
  To: emacs-devel; +Cc: Edward O'Connor

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




^ permalink raw reply	[flat|nested] 9+ messages in thread

end of thread, other threads:[~2011-10-17 17:41 UTC | newest]

Thread overview: 9+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2011-10-13 13:51 possible json.el optimization: json-alist-p and json-plist-p recursion Ted Zlatanov
2011-10-13 14:31 ` 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

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.