From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED!not-for-mail From: Stefan Monnier Newsgroups: gmane.emacs.devel Subject: Re: [PATCH] Add new lisp function length= with bytecode support Date: Sun, 12 Mar 2017 23:20:58 -0400 Message-ID: References: <64Kl8OYdaKer-3Ey7GHVD9He6bX8yYHaS_NjEwp7Wqc4Zb7xu8IQV3ExvjCLKlBWHVVr_HNUhd55i_BVXNHnpxjnXc6hPgWvWkc3bIO8e7s=@protonmail.com> <99375f4d-f627-8e29-2c1f-9d3bfdf3ddd1@gmail.com> <0mz3T7gRvLpYGD7MDAZGZjxUi5QdLAkXtZ7y8UX4a3tDMKu1KvlRZ2IYKRUlQh01TvDsPfG1Fl_l4MIlmBWtHW9A_nQ9liIx6Mz6vcT6lGw=@protonmail.com> <4pTgtp7P0udVYhXvmkdE96q4eNHNWW6ZQ1o7woPbQTJbXd5scmDTlaLrg4BtjD_YzTTX5qHyHhXUg3gX0jW5yenj-a2gnT0m8QiFQETB284=@protonmail.com> <3AD8FC51-ABB3-4FC7-A5EF-A30EBEA27E9E@raeburn.org> NNTP-Posting-Host: blaine.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: blaine.gmane.org 1489375326 14710 195.159.176.226 (13 Mar 2017 03:22:06 GMT) X-Complaints-To: usenet@blaine.gmane.org NNTP-Posting-Date: Mon, 13 Mar 2017 03:22:06 +0000 (UTC) User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.0.50 (gnu/linux) To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Mon Mar 13 04:21:59 2017 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([208.118.235.17]) by blaine.gmane.org with esmtp (Exim 4.84_2) (envelope-from ) id 1cnGYX-0002od-ET for ged-emacs-devel@m.gmane.org; Mon, 13 Mar 2017 04:21:57 +0100 Original-Received: from localhost ([::1]:49907 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnGYb-0005W2-LO for ged-emacs-devel@m.gmane.org; Sun, 12 Mar 2017 23:22:01 -0400 Original-Received: from eggs.gnu.org ([2001:4830:134:3::10]:53608) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1cnGXs-0005Vx-HW for emacs-devel@gnu.org; Sun, 12 Mar 2017 23:21:18 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1cnGXn-0004u9-J9 for emacs-devel@gnu.org; Sun, 12 Mar 2017 23:21:16 -0400 Original-Received: from [195.159.176.226] (port=35689 helo=blaine.gmane.org) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1cnGXn-0004sX-BD for emacs-devel@gnu.org; Sun, 12 Mar 2017 23:21:11 -0400 Original-Received: from list by blaine.gmane.org with local (Exim 4.84_2) (envelope-from ) id 1cnGXb-0005X5-1D for emacs-devel@gnu.org; Mon, 13 Mar 2017 04:20:59 +0100 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 138 Original-X-Complaints-To: usenet@blaine.gmane.org Cancel-Lock: sha1:ZI7Vlskw83X5bN/Me78SmK5bpIM= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 195.159.176.226 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.21 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" Xref: news.gmane.org gmane.emacs.devel:212975 Archived-At: > When the bytecode interpreter encounters a length bytecode with a list > argument followed by a comparison bytecode it defers to the new special > purpose length comparison functions. > * src/bytecode.c (exec_byte_code): Change the Blength bytecode case and add > the new functions. > * lisp/emacs-lisp/byte-opt.el (byte-optimize-binary-predicate, > byte-optimize-predicate): Make the byte-compiler put the length and > comparison bytecodes next to each other when possible. > * src/lisp.h (length_Beqlsign, length_Bgtr, length_Blss, length_Bleq, > length_Bgeq, length_Beq): Declare new C functions. I like that. > + switch (PEEK) > + { > + case Beqlsign: > + op = FETCH; > + v1 = POP; > + TOP = length_Beqlsign (TOP, v1); > + break; > + > + case Bgtr: > + op = FETCH; > + v1 = POP; > + TOP = length_Bgtr (TOP, v1); > + break; > + > + case Blss: > + op = FETCH; > + v1 = POP; > + TOP = length_Blss (TOP, v1); > + break; > + > + case Bleq: > + op = FETCH; > + v1 = POP; > + TOP = length_Bleq (TOP, v1); > + break; > + > + case Bgeq: > + op = FETCH; > + v1 = POP; > + TOP = length_Bgeq (TOP, v1); > + break; > + > + case Beq: > + case Bequal: > + op = FETCH; > + v1 = POP; > + TOP = length_Beq (TOP, v1); > + break; > + > + default: > + TOP = Flength (TOP); > + } > + } Please move most of that to a separate function (which I guess will take the list, the op and the value to which to compare the list). > +/* The following are used above in the Blength case. Each assumes s1 > + is a number or marker and s2 is a list. */ > + > +Lisp_Object > +length_Beqlsign (Lisp_Object s1, Lisp_Object s2) > +{ > + Lisp_Object val = Qnil; > + > + CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (s1); > + > + if (__builtin_expect (FLOATP (s1), 0)) > + { > + s2 = Flength(s2); > + val = arithcompare (s1, s2, ARITH_EQUAL); > + } > + else > + { > + intptr_t n = XINT (s1); > + intptr_t i = 0; > + FOR_EACH_TAIL (s2) > + { > + i++; > + if (i > n) > + return val; > + } > + CHECK_LIST_END (s2, s2); > + if (i == n) > + val = Qt; > + } > + > + return val; > +} > + > +Lisp_Object > +length_Bgtr (Lisp_Object s1, Lisp_Object s2) > +{ > + Lisp_Object val = Qnil; > + > + CHECK_NUMBER_OR_FLOAT_COERCE_MARKER (s1); > + > + if (__builtin_expect (FLOATP (s1), 0)) > + { > + s2 = Flength(s2); > + val = arithcompare (s1, s2, ARITH_GRTR); > + } > + else > + { > + intptr_t n = XINT (s1); > + intptr_t i = 0; > + FOR_EACH_TAIL (s2) > + { > + i++; > + if (i >= n) > + return val; > + } > + CHECK_LIST_END (s2, s2); > + if (i < n) > + val = Qt; > + } > + > + return val; > +} Similarly, here (and below), I'm wondering if we can't reduce the code duplication. Furthermore, you might like to declare them static, so the compiler is more likely to inline them. > +Lisp_Object length_Beqlsign (Lisp_Object, Lisp_Object); > +Lisp_Object length_Bgtr (Lisp_Object, Lisp_Object); > +Lisp_Object length_Blss (Lisp_Object, Lisp_Object); > +Lisp_Object length_Bleq (Lisp_Object, Lisp_Object); > +Lisp_Object length_Bgeq (Lisp_Object, Lisp_Object); > +Lisp_Object length_Beq (Lisp_Object, Lisp_Object); Don't expose them in lisp.h, since they're only used in bytecode.c. Stefan