unofficial mirror of emacs-devel@gnu.org 
 help / color / mirror / code / Atom feed
From: Stefan Monnier <monnier@iro.umontreal.ca>
To: emacs-devel@gnu.org
Subject: Re: possible json.el optimization: json-alist-p and json-plist-p recursion
Date: Thu, 13 Oct 2011 10:31:30 -0400	[thread overview]
Message-ID: <jwvr52h7yz1.fsf-monnier+emacs@gnu.org> (raw)
In-Reply-To: <877h49t2y8.fsf@lifelogs.com> (Ted Zlatanov's message of "Thu, 13 Oct 2011 09:51:11 -0400")

> 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.

Emacs Lisp doesn't do any tail recursion optimization (to some extent
because it's pretty difficult to do with a dynamically scoped languages,
so hopefully we'll be able to change this for the lexically scoped
dialect) and its function calls are expensive, so it does not handle
recursion very well, indeed (yes, this is sad).

> #+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

I guess it's OK tho "car-safe" seems unneeded since you've just tested
consp before (note that the original code didn't even check (consp p)
and just signaled an error if `list' is not a proper list).

Oh, and you don't need to copy `list' into `p', you can work on `list' directly.

>     (while (consp p)
>       (setq p (if (and (keywordp (car-safe list))
>                        (consp (cdr-safe p)))

Same here about car-safe and cdr-safe, and additionally, I think you
don't want to test `list' but `p' instead (tho here again, you probably
want to work on `list' directly without using a `p').


        Stefan



  reply	other threads:[~2011-10-13 14:31 UTC|newest]

Thread overview: 9+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
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 [this message]
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

  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=jwvr52h7yz1.fsf-monnier+emacs@gnu.org \
    --to=monnier@iro.umontreal.ca \
    --cc=emacs-devel@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).