From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Newsgroups: gmane.emacs.bugs Subject: bug#42147: 28.0.50; pure vs side-effect-free, missing optimizations? Date: Sun, 5 Jul 2020 15:00:32 +0200 Message-ID: <374F2E9C-E8C2-4362-8BEF-E6AA5EEE5C79@acm.org> References: <3A9CC2A3-8307-47B2-8D80-795C0AF020E1@acm.org> <0433A879-C98D-4B1A-B85C-A15DA9289099@acm.org> <1621669100.2102667.1593639091621@mail.yahoo.com> <775819003.2516724.1593687594435@mail.yahoo.com> <5F2B4684-34D1-4474-8909-9F435369FE54@acm.org> <705260433.2731607.1593698199171@mail.yahoo.com> <6CF8EE58-9A49-40E7-AA86-48AB39BF94BA@acm.org> <28B19D86-343C-4126-B95F-1F38735F73F2@acm.org> <1288c6a5-545b-f68c-ff6b-7683db3e54c1@cs.ucla.edu> <1690361185.4397117.1593879228865@mail.yahoo.com> Mime-Version: 1.0 (Mac OS X Mail 12.4 \(3445.104.14\)) Content-Type: multipart/mixed; boundary="Apple-Mail=_8AE3DB23-A143-4FDD-B6CD-42CF468A6EDA" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="15149"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Paul Eggert , Stefan Monnier , 42147@debbugs.gnu.org To: Andrea Corallo Original-X-From: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Sun Jul 05 15:01:15 2020 Return-path: Envelope-to: geb-bug-gnu-emacs@m.gmane-mx.org Original-Received: from lists.gnu.org ([209.51.188.17]) by ciao.gmane.io with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.92) (envelope-from ) id 1js4Gh-0003pe-4w for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 05 Jul 2020 15:01:15 +0200 Original-Received: from localhost ([::1]:53446 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1js4Gf-000122-GG for geb-bug-gnu-emacs@m.gmane-mx.org; Sun, 05 Jul 2020 09:01:13 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:43062) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1js4GU-00011q-O0 for bug-gnu-emacs@gnu.org; Sun, 05 Jul 2020 09:01:02 -0400 Original-Received: from debbugs.gnu.org ([209.51.188.43]:48632) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1js4GU-0005K7-F4 for bug-gnu-emacs@gnu.org; Sun, 05 Jul 2020 09:01:02 -0400 Original-Received: from Debian-debbugs by debbugs.gnu.org with local (Exim 4.84_2) (envelope-from ) id 1js4GU-0002CS-Cu for bug-gnu-emacs@gnu.org; Sun, 05 Jul 2020 09:01:02 -0400 X-Loop: help-debbugs@gnu.org Resent-From: Mattias =?UTF-8?Q?Engdeg=C3=A5rd?= Original-Sender: "Debbugs-submit" Resent-CC: bug-gnu-emacs@gnu.org Resent-Date: Sun, 05 Jul 2020 13:01:02 +0000 Resent-Message-ID: Resent-Sender: help-debbugs@gnu.org X-GNU-PR-Message: followup 42147 X-GNU-PR-Package: emacs Original-Received: via spool by 42147-submit@debbugs.gnu.org id=B42147.15939540558436 (code B ref 42147); Sun, 05 Jul 2020 13:01:02 +0000 Original-Received: (at 42147) by debbugs.gnu.org; 5 Jul 2020 13:00:55 +0000 Original-Received: from localhost ([127.0.0.1]:60178 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1js4GM-0002C0-Hf for submit@debbugs.gnu.org; Sun, 05 Jul 2020 09:00:55 -0400 Original-Received: from mail212c50.megamailservers.eu ([91.136.10.222]:35668 helo=mail194c50.megamailservers.eu) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1js4GK-0002Bl-Ob for 42147@debbugs.gnu.org; Sun, 05 Jul 2020 09:00:53 -0400 X-Authenticated-User: mattiase@bredband.net DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=megamailservers.eu; s=maildub; t=1593954037; bh=JucY2oXBkJkGZLUPsiFj8YIZvfVMxxWTGSGvecGTRQY=; h=From:Subject:Date:In-Reply-To:Cc:To:References:From; b=PdvMbLvqhZm41/fKQYGiTKcyCtQKouQBOleCF6+rWCtBL5qk+FTKXxzpm6i2lKXKQ wUJNUVmjeqqyBWCMEIuiCX6t95amrjfMjXptj1QKwvWJNv39AHS21RAZUWQ4b9Rqfl F5UxfRycfrcYX9x/YYx2dZCNbYbXsOyMuWe1H1nE= Feedback-ID: mattiase@acm.or Original-Received: from [192.168.0.4] (c188-150-171-71.bredband.comhem.se [188.150.171.71]) (authenticated bits=0) by mail194c50.megamailservers.eu (8.14.9/8.13.1) with ESMTP id 065D0XxE026262; Sun, 5 Jul 2020 13:00:35 +0000 In-Reply-To: <1690361185.4397117.1593879228865@mail.yahoo.com> X-Mailer: Apple Mail (2.3445.104.14) X-CTCH-RefID: str=0001.0A782F16.5F01CEF5.0057, ss=1, re=0.000, recu=0.000, reip=0.000, cl=1, cld=1, fgs=0 X-CTCH-VOD: Unknown X-CTCH-Spam: Unknown X-CTCH-Score: 0.000 X-CTCH-Flags: 0 X-CTCH-ScoreCust: 0.000 X-CSC: 0 X-CHA: v=2.3 cv=KsozJleN c=1 sm=1 tr=0 a=SF+I6pRkHZhrawxbOkkvaA==:117 a=SF+I6pRkHZhrawxbOkkvaA==:17 a=M51BFTxLslgA:10 a=xqM3wO8aiJJCaqFe2MkA:9 a=CjuIK1q_8ugA:10 a=U7s5LNZHvtAvlAXazu8A:9 a=B2y7HmGcmWMA:10 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list X-BeenThere: bug-gnu-emacs@gnu.org List-Id: "Bug reports for GNU Emacs, the Swiss army knife of text editors" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: bug-gnu-emacs-bounces+geb-bug-gnu-emacs=m.gmane-mx.org@gnu.org Original-Sender: "bug-gnu-emacs" Xref: news.gmane.io gmane.emacs.bugs:182736 Archived-At: --Apple-Mail=_8AE3DB23-A143-4FDD-B6CD-42CF468A6EDA Content-Transfer-Encoding: quoted-printable Content-Type: text/plain; charset=us-ascii 4 juli 2020 kl. 18.13 skrev Andrea Corallo : > (defun bbb () > (let ((x (list 1 2))) > (f x) ; f is not pure > (1+ (car x)) ; <=3D cannot optimize > )) (A more precise property for 'f' in your examples would be = 'side-effect-free' rather than 'pure'.) > BTW reading the code of the native compiler I realized I am already > extrapolating for use a very similar list of optimizable functions to = the one > proposed. I still think would quite cleaner to classify these in > byte-opt.el. Certainly, but be sure to state your criteria with clarity. There must = be no doubt whether or not a function should have a certain property! > Attached the updated patch where I'm adding car, car-safe, cdr, > cdr-safe, max, min. Thank you Andrea! Attached is an update with the following = modifications: * I tried to segregate pure functions that operate on mutable objects, = such as car, length and equal, from the rest. This way we can more = easily separate them entirely (using different properties) later on if = desired. * The list of pure functions was expanded further. Related functions = were grouped rather than ordered alphabetically, because I found it = easier to read this way -- you may disagree. * 'expt' was prudently removed because it doesn't necessarily give = portable results for arbitrary floating-point arguments. (exp, sin etc = were not included either for the same reason.) * It turned out that in order to bootstrap, we have to prevent the = constant evaluation in the byte compiler from raising errors on invalid = input. For example, the macro dired-map-over-marks expands to = (essentially) (if (integerp ARG) (< ARG 0) where ARG is a macro argument that can be nil. Since < is now pure, = compilation would fail despite the offending code being unreachable. As = this style of code exists and is not unreasonable, the error has to be = suppressed. --Apple-Mail=_8AE3DB23-A143-4FDD-B6CD-42CF468A6EDA Content-Disposition: attachment; filename=0001-Mark-more-functions-pure-bug-42147.patch Content-Type: application/octet-stream; x-unix-mode=0644; name="0001-Mark-more-functions-pure-bug-42147.patch" Content-Transfer-Encoding: quoted-printable =46rom=20bc387b56233798fb5a2a821c58632ca9c8a9044b=20Mon=20Sep=2017=20= 00:00:00=202001=0AFrom:=20=3D?UTF-8?q?Mattias=3D20Engdeg=3DC3=3DA5rd?=3D=20= =0ADate:=20Sun,=205=20Jul=202020=2013:47:34=20+0200=0A= Subject:=20[PATCH]=20Mark=20more=20functions=20pure=20(bug#42147)=0A=0A= Extend=20the=20list=20of=20'pure'=20functions=20to=20many=20predicates=20= and=20numerical=0Afunctions=20that=20we=20are=20reasonably=20confident=20= will=20give=20portable=20results.=0AAlso=20include=20various=20list=20= and=20array=20accessors,=20because=20our=20use=20of=20purity=0Ain=20the=20= byte=20compiler=20isn't=20affected=20by=20the=20mutability=20of=20= arguments.=0A=0A*=20lisp/emacs-lisp/byte-opt.el:=20Update=20example=20in=20= comment.=0A(pure-fns):=20Add=20many=20functions.=0A= (byte-optimize-form-code-walker)=20Don't=20signal=20errors=20during=20= evaluation=0Aof=20calls=20to=20pure=20functions=20with=20constant=20= arguments=20at=20compile=20time,=0Asince=20such=20calls=20are=20not=20= necessarily=20reachable.=0A---=0A=20lisp/emacs-lisp/byte-opt.el=20|=2049=20= +++++++++++++++++++++++++++++++------=0A=201=20file=20changed,=2042=20= insertions(+),=207=20deletions(-)=0A=0Adiff=20--git=20= a/lisp/emacs-lisp/byte-opt.el=20b/lisp/emacs-lisp/byte-opt.el=0Aindex=20= bf9e6a728a..92c2374a07=20100644=0A---=20a/lisp/emacs-lisp/byte-opt.el=0A= +++=20b/lisp/emacs-lisp/byte-opt.el=0A@@=20-557,7=20+557,10=20@@=20= byte-optimize-form-code-walker=0A=20=09=20=20=20(let=20((args=20(mapcar=20= #'byte-optimize-form=20(cdr=20form))))=0A=20=09=20=20=20=20=20(if=20(and=20= (get=20fn=20'pure)=0A=20=09=09=20=20=20=20=20=20= (byte-optimize-all-constp=20args))=0A-=09=09=20=20=20(list=20'quote=20= (apply=20fn=20(mapcar=20#'eval=20args)))=0A+=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20(let=20((arg-values=20(mapcar=20#'eval=20args)))=0A= +=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= (condition-case=20nil=0A+=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20= =20=20=20=20=20=20=20(list=20'quote=20(apply=20fn=20arg-values))=0A+=20=20= =20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20=20(error=20(cons=20= fn=20args))))=0A=20=09=20=20=20=20=20=20=20(cons=20fn=20args)))))))=0A=20= =0A=20(defun=20byte-optimize-all-constp=20(list)=0A@@=20-1274,9=20= +1277,9=20@@=20byte-optimize-set=0A=20;;=20Pure=20functions=20are=20= side-effect=20free=20functions=20whose=20values=20depend=0A=20;;=20only=20= on=20their=20arguments,=20not=20on=20the=20platform.=20=20For=20these=20= functions,=0A=20;;=20calls=20with=20constant=20arguments=20can=20be=20= evaluated=20at=20compile=20time.=0A-;;=20This=20may=20shift=20runtime=20= errors=20to=20compile=20time.=20=20For=20example,=20logand=0A-;;=20is=20= pure=20since=20its=20results=20are=20machine-independent,=20whereas=20= ash=20is=0A-;;=20not=20pure=20because=20(ash=201=2029)'s=20value=20= depends=20on=20machine=20word=20size.=0A+;;=20For=20example,=20ash=20is=20= pure=20since=20its=20results=20are=20machine-independent,=0A+;;=20= whereas=20lsh=20is=20not=20pure=20because=20(lsh=20-1=20-1)'s=20value=20= depends=20on=20the=0A+;;=20fixnum=20range.=0A=20;;=0A=20;;=20When=20= deciding=20whether=20a=20function=20is=20pure,=20do=20not=20worry=20= about=0A=20;;=20mutable=20strings=20or=20markers,=20as=20they=20are=20so=20= unlikely=20in=20real=20code=0A@@=20-1286,9=20+1289,41=20@@=20= byte-optimize-set=0A=20;;=20values=20if=20a=20marker=20is=20moved.=0A=20=0A= =20(let=20((pure-fns=0A-=20=20=20=20=20=20=20'(%=20concat=20logand=20= logcount=20logior=20lognot=20logxor=0A-=09=20regexp-opt=20regexp-quote=0A= -=09=20string-to-char=20string-to-syntax=20symbol-name)))=0A+=20=20=20=20= =20=20=20'(concat=20regexp-opt=20regexp-quote=0A+=09=20string-to-char=20= string-to-syntax=20symbol-name=0A+=20=20=20=20=20=20=20=20=20eq=20eql=0A= +=20=20=20=20=20=20=20=20=20=3D=20/=3D=20<=20<=3D=20=3D>=20>=20min=20max=0A= +=20=20=20=20=20=20=20=20=20+=20-=20*=20/=20%=20mod=20abs=20ash=201+=20= 1-=20sqrt=0A+=20=20=20=20=20=20=20=20=20logand=20logior=20lognot=20= logxor=20logcount=0A+=20=20=20=20=20=20=20=20=20copysign=20isnan=20ldexp=20= float=20logb=0A+=20=20=20=20=20=20=20=20=20floor=20ceiling=20round=20= truncate=0A+=20=20=20=20=20=20=20=20=20ffloor=20fceiling=20fround=20= ftruncate=0A+=20=20=20=20=20=20=20=20=20string=3D=20string-equal=20= string<=20string-lessp=0A+=20=20=20=20=20=20=20=20=20consp=20atom=20= listp=20nlistp=20propert-list-p=0A+=20=20=20=20=20=20=20=20=20sequencep=20= arrayp=20vectorp=20stringp=20bool-vector-p=20hash-table-p=0A+=20=20=20=20= =20=20=20=20=20null=20not=0A+=20=20=20=20=20=20=20=20=20numberp=20= integerp=20floatp=20natnump=20characterp=0A+=20=20=20=20=20=20=20=20=20= integer-or-marker-p=20number-or-marker-p=20char-or-string-p=0A+=20=20=20=20= =20=20=20=20=20symbolp=20keywordp=0A+=20=20=20=20=20=20=20=20=20type-of=0A= +=20=20=20=20=20=20=20=20=20identity=20ignore=0A+=0A+=20=20=20=20=20=20=20= =20=20;;=20The=20following=20functions=20are=20pure=20up=20to=20mutation=20= of=20their=0A+=20=20=20=20=20=20=20=20=20;;=20arguments.=20=20This=20is=20= pure=20enough=20for=20the=20purposes=20of=0A+=20=20=20=20=20=20=20=20=20= ;;=20constant=20folding,=20but=20not=20necessarily=20for=20all=20kinds=20= of=0A+=20=20=20=20=20=20=20=20=20;;=20code=20motion.=0A+=20=20=20=20=20=20= =20=20=20car=20cdr=20car-safe=20cdr-safe=20nth=20nthcdr=20last=0A+=20=20=20= =20=20=20=20=20=20equal=0A+=20=20=20=20=20=20=20=20=20length=20= safe-length=0A+=20=20=20=20=20=20=20=20=20memq=20memql=20member=0A+=20=20= =20=20=20=20=20=20=20;;=20`assoc'=20and=20`assoc-default'=20are=20= excluded=20since=20they=20are=0A+=20=20=20=20=20=20=20=20=20;;=20impure=20= if=20the=20test=20function=20is=20(consider=20`string-match').=0A+=20=20=20= =20=20=20=20=20=20assq=20assql=20rassq=20rassoc=0A+=20=20=20=20=20=20=20=20= =20plist-get=20lax-plist-get=20plist-member=0A+=20=20=20=20=20=20=20=20=20= aref=20elt=0A+=20=20=20=20=20=20=20=20=20bool-vector-subsetp=0A+=20=20=20= =20=20=20=20=20=20bool-vector-count-population=20= bool-vector-count-consecutive=0A+=20=20=20=20=20=20=20=20=20)))=0A=20=20=20= (while=20pure-fns=0A=20=20=20=20=20(put=20(car=20pure-fns)=20'pure=20t)=0A= =20=20=20=20=20(setq=20pure-fns=20(cdr=20pure-fns)))=0A--=20=0A2.21.1=20= (Apple=20Git-122.3)=0A=0A= --Apple-Mail=_8AE3DB23-A143-4FDD-B6CD-42CF468A6EDA--