From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: Ted Zlatanov Newsgroups: gmane.emacs.devel Subject: possible json.el optimization: json-alist-p and json-plist-p recursion Date: Thu, 13 Oct 2011 09:51:11 -0400 Organization: =?utf-8?B?0KLQtdC+0LTQvtGAINCX0LvQsNGC0LDQvdC+0LI=?= @ Cienfuegos Message-ID: <877h49t2y8.fsf@lifelogs.com> Reply-To: emacs-devel@gnu.org NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1318514013 24054 80.91.229.12 (13 Oct 2011 13:53:33 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Thu, 13 Oct 2011 13:53:33 +0000 (UTC) Cc: Edward O'Connor To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Oct 13 15:53:28 2011 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1RELix-00053z-92 for ged-emacs-devel@m.gmane.org; Thu, 13 Oct 2011 15:53:27 +0200 Original-Received: from localhost ([::1]:53205 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RELiw-0007T7-G2 for ged-emacs-devel@m.gmane.org; Thu, 13 Oct 2011 09:53:26 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:36692) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RELin-0007Sj-Ri for emacs-devel@gnu.org; Thu, 13 Oct 2011 09:53:23 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1RELii-0001Yg-44 for emacs-devel@gnu.org; Thu, 13 Oct 2011 09:53:17 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:36683) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1RELih-0001YY-Ok for emacs-devel@gnu.org; Thu, 13 Oct 2011 09:53:12 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1RELib-0004qA-9j for emacs-devel@gnu.org; Thu, 13 Oct 2011 15:53:05 +0200 Original-Received: from c-76-28-40-19.hsd1.vt.comcast.net ([76.28.40.19]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 13 Oct 2011 15:53:05 +0200 Original-Received: from tzz by c-76-28-40-19.hsd1.vt.comcast.net with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Thu, 13 Oct 2011 15:53:05 +0200 X-Injected-Via-Gmane: http://gmane.org/ Mail-Followup-To: emacs-devel@gnu.org Original-Lines: 56 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: c-76-28-40-19.hsd1.vt.comcast.net X-Face: bd.DQ~'29fIs`T_%O%C\g%6jW)yi[zuz6; d4V0`@y-~$#3P_Ng{@m+e4o<4P'#(_GJQ%TT= D}[Ep*b!\e,fBZ'j_+#"Ps?s2!4H2-Y"sx" Mail-Copies-To: never User-Agent: Gnus/5.110018 (No Gnus v0.18) Emacs/24.0.90 (gnu/linux) Cancel-Lock: sha1:FQG1m2pE0ZWKJnsTwkgMtYW/x8U= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.14 Precedence: list List-Id: "Emacs development discussions." List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Original-Sender: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.emacs.devel:145124 Archived-At: 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