From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: akater Newsgroups: gmane.emacs.devel Subject: [PATCH] Improve detection of local function calls in methods Date: Fri, 27 Aug 2021 21:41:05 +0000 Message-ID: <878s0mva4u.fsf@gmail.com> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="24436"; mail-complaints-to="usenet@ciao.gmane.io" To: emacs-devel@gnu.org Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Fri Aug 27 23:53:31 2021 Return-path: Envelope-to: ged-emacs-devel@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 1mJjn0-0006Ap-NL for ged-emacs-devel@m.gmane-mx.org; Fri, 27 Aug 2021 23:53:30 +0200 Original-Received: from localhost ([::1]:49900 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1mJjmy-00086O-GZ for ged-emacs-devel@m.gmane-mx.org; Fri, 27 Aug 2021 17:53:28 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37596) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1mJjlz-0007Pg-HR for emacs-devel@gnu.org; Fri, 27 Aug 2021 17:52:27 -0400 Original-Received: from mail-ej1-x630.google.com ([2a00:1450:4864:20::630]:33465) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.90_1) (envelope-from ) id 1mJjly-0008QM-2g for emacs-devel@gnu.org; Fri, 27 Aug 2021 17:52:27 -0400 Original-Received: by mail-ej1-x630.google.com with SMTP id x11so16911190ejv.0 for ; Fri, 27 Aug 2021 14:52:25 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20161025; h=from:to:subject:date:message-id:mime-version; bh=rP9uWHnSRT9u504Ic5UaK4biLf/nR9so5mTFS4QZv9I=; b=bZBsHmyIjw7Jzpa/lbOSpSxC05RjeP5grrtlKkyO8PS2qo+PAN0B+yJ2IrBJ2O18Ij R1MtxlZzU+0EkxkzUZnu7A/hasqu3wA+ZvXVSjjTRMfLX7nQ17+Bx5lbIA2A2U/iutDa J9nREukQfkdQCsEOVn2LjDWs10hM9Taq7Zohq2uHcWxpfGUI2puklsCS9K3JAKsi13LA V3hLMgsOBIVjhNMV9CBz0MeF8heFQFAd4RcKKBIGkVKcWapRXAPNNaDQxwFYqLoVIo3f nCoYx0a87+tbO73lBj3EUUnu3EP4/s4RrUiPEBvm+xPJM88diUSUwMjpavgGHx1NsoqD 20gw== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20161025; h=x-gm-message-state:from:to:subject:date:message-id:mime-version; bh=rP9uWHnSRT9u504Ic5UaK4biLf/nR9so5mTFS4QZv9I=; b=WtLBzv11VWAh30GSol5Yz/enH2Q3PPemN/aGQKTx5ZGNGM3Lcrss3AXJh9vFRAjfGg yyr84yXqh0t4SSZvO2nfW85ld34ocEAb3tINmI6xfEBhajEOS+vmeNb/hvoj0zFzRt3c Sc1+9CQ9MinTSgPc9yHk0gC+vRPUHVo4cuEGBHFmYGEJw9EygVrGpbmTy9+qxLAxfuHn pxJBgo259JoinRQJPTAdKhqZg5PQWmOzCBnPbIheIZREYwgvdarTwAWktJTgq1YFMYI/ rPFpq51kcfb9IVJFKpUQ4SBbKVBJszB1i/FO+ByFhFa1FEGRXYiBhoysADERRpUiwoP1 BEqA== X-Gm-Message-State: AOAM532vy1y+Yp8kU9Pwlm4CjaG1+DLS7hmA5dAyUkuChTjXTrmR9/Hn 9idFxZHRQmD2TKMvt/ZsVKeBaOXmS3g= X-Google-Smtp-Source: ABdhPJws7OJjW6OT1OKjhVCvVBnVBZpv+Xkah50BxH3O2/4oYNGF1oHGQ+xQsOvdSESdR81SP0Z6SA== X-Received: by 2002:a17:906:7a0d:: with SMTP id d13mr2483407ejo.70.1630101143539; Fri, 27 Aug 2021 14:52:23 -0700 (PDT) Original-Received: from localhost ([45.153.160.2]) by smtp.googlemail.com with ESMTPSA id r5sm4181871edx.6.2021.08.27.14.52.22 for (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Fri, 27 Aug 2021 14:52:23 -0700 (PDT) Received-SPF: pass client-ip=2a00:1450:4864:20::630; envelope-from=nuclearspace@gmail.com; helo=mail-ej1-x630.google.com X-Spam_score_int: -7 X-Spam_score: -0.8 X-Spam_bar: / X-Spam_report: (-0.8 / 5.0 requ) BAYES_00=-1.9, DKIM_SIGNED=0.1, DKIM_VALID=-0.1, DKIM_VALID_AU=-0.1, DKIM_VALID_EF=-0.1, FREEMAIL_FROM=0.001, RCVD_IN_BL_SPAMCOP_NET=1.347, RCVD_IN_DNSWL_NONE=-0.0001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=no autolearn_force=no X-Spam_action: no action X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.23 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-mx.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.io gmane.emacs.devel:273190 Archived-At: --=-=-= Content-Type: multipart/signed; boundary="==-=-="; micalg=pgp-sha512; protocol="application/pgp-signature" --==-=-= Content-Type: text/plain Instead of using unreliale and expensive macroexp--fgrep, we record the relevant calls in the macroexpansion, as suggested in the FIXME entry. --==-=-= Content-Type: application/pgp-signature; name="signature.asc" -----BEGIN PGP SIGNATURE----- iQJLBAEBCgA1FiEEgu5SJRdnQOF34djNsr6xYbHsf0QFAmEpW/QXHG51Y2xlYXJz cGFjZUBnbWFpbC5jb20ACgkQsr6xYbHsf0QoEg/9GMeLaQSfXu/dtldEN13dSWws 5m6/mHjW4wKwzrJ3ZFw2GwZyWsyq/xUVwDpEvZzJRGGVAowz6cWjQr6gO9U/cVfO EYbcAhjzvjqCZbg9wUNY57NJ+jGtV/yhzytrm+mBYKsWTQAllWDZ6QC/9i1nlQQp V/EvcboJ4L8IsRawFdcNhaZiJFtruKIVYijo7ULE3vfJZ9+kEFrFE08afVGxTI32 gP/tXc7aVB55tv9Q1nttizFGOSv1ZaMEpmqg4eux2SVEEQ/WsbG+nDboxBXeYuv7 2Fq+syAV0sLUwlr+H8kpPKh0GunkJIyKMN84GoTACDhd29+EOs/9j7my2X35NGmY LoBbu64WEroYBpz2O/SoW02K+XoFNhT1vvFxKcgFtCtkFDlwvI3d1MV83QcNcw2r YhB6MJ8VaNpYEKYNgbdCdoc427Ft7lWx9RICxBCZZzEe2OL/43NOHq2gzFBth0Ek gqbt6ROKaJ1g/fs3+vSlT3z3rBwfNM3tIKJTH2yr+VALjfsK792EvZK12QBOGN89 J4m5PHbqlV2FENRMjZFv0K2B1qbsRSDvpr2/Ae6QJXN/NCe9KQkyyu0K4EF0Klh7 +vD6NzWQ1hu8PUW2P2BhFpZDFf5zTWnGjthg71RPsb9y8iMTVL+Qnowjcee3oD50 dVfbdulMVPeALDvbgY4= =ruk5 -----END PGP SIGNATURE----- --==-=-=-- --=-=-= Content-Type: text/x-diff Content-Disposition: attachment; filename=0001-Improve-detection-of-local-function-calls-in-methods.patch Content-Description: cl--generic-lambda fix >From 099c63eb1c107531252fde859dee7466de05f210 Mon Sep 17 00:00:00 2001 From: akater Date: Thu, 26 Aug 2021 06:09:07 +0000 Subject: [PATCH] Improve detection of local function calls in methods * lisp/emacs-lisp/cl-generic.el (cl--generic-lambda): Rather than `grep' after the fact, the macroexpansion records directly when cl-call-next-method or cl-next-method-p are used. --- lisp/emacs-lisp/cl-generic.el | 34 +++++++++++++++++++++++++--------- 1 file changed, 25 insertions(+), 9 deletions(-) diff --git a/lisp/emacs-lisp/cl-generic.el b/lisp/emacs-lisp/cl-generic.el index 4a69df15bc..d5d77fe553 100644 --- a/lisp/emacs-lisp/cl-generic.el +++ b/lisp/emacs-lisp/cl-generic.el @@ -361,6 +361,14 @@ defun cl--generic-split-args (args) (cons (nreverse specializers) (nreverse (delq nil plain-args))))) + (defvar cl-generic--uses-cnm nil + ;; It would be better to declare the variable special + ;; locally where it's used + ;; but there is no support for local special declarations in Elisp. + "In a runtime environment, keeps a list of flags that indicate +the presence of `cl-call-next-method' or `cl-next-method-p' +in a method body.") + (defun cl--generic-lambda (args body) "Make the lambda expression for a method with ARGS and BODY." (pcase-let* ((`(,spec-args . ,plain-args) @@ -369,7 +377,7 @@ defun cl--generic-lambda (args body) (macroenv (cons `(cl-generic-current-method-specializers . ,(lambda () spec-args)) macroexpand-all-environment))) - (require 'cl-lib) ;Needed to expand `cl-flet' and `cl-function'. + (require 'cl-lib) ;Needed to expand `cl-function', `cl-macrolet'. (when (interactive-form (cadr fun)) (message "Interactive forms unsupported in generic functions: %S" (interactive-form (cadr fun)))) @@ -380,21 +388,29 @@ defun cl--generic-lambda (args body) (let* ((parsed-body (macroexp-parse-body body)) (cnm (make-symbol "cl--cnm")) (nmp (make-symbol "cl--nmp")) + (cl-generic--uses-cnm) (nbody (macroexpand-all - `(cl-flet ((cl-call-next-method ,cnm) - (cl-next-method-p ,nmp)) + `(cl-macrolet ((cl-call-next-method + (&rest args) + (prog1 `(funcall ,',cnm ,@args) + (cl-pushnew + ',cnm cl-generic--uses-cnm + :test #'eq))) + (cl-next-method-p + () + (prog1 `(funcall ,',nmp) + (cl-pushnew + ',nmp cl-generic--uses-cnm + :test #'eq)))) ,@(cdr parsed-body)) macroenv)) - ;; FIXME: Rather than `grep' after the fact, the - ;; macroexpansion should directly set some flag when cnm - ;; is used. - ;; FIXME: Also, optimize the case where call-next-method is + ;; FIXME: Optimize the case where call-next-method is ;; only called with explicit arguments. - (uses-cnm (macroexp--fgrep `((,cnm) (,nmp)) nbody))) + (uses-cnm cl-generic--uses-cnm)) (cons (not (not uses-cnm)) `#'(lambda (,@(if uses-cnm (list cnm)) ,@args) ,@(car parsed-body) - ,(if (not (assq nmp uses-cnm)) + ,(if (not (memq nmp uses-cnm)) nbody `(let ((,nmp (lambda () (cl--generic-isnot-nnm-p ,cnm)))) -- 2.31.1 --=-=-=--