From unknown Sun Jun 22 17:12:26 2025 Content-Disposition: inline Content-Transfer-Encoding: quoted-printable MIME-Version: 1.0 X-Mailer: MIME-tools 5.509 (Entity 5.509) Content-Type: text/plain; charset=utf-8 From: bug#78162 <78162@debbugs.gnu.org> To: bug#78162 <78162@debbugs.gnu.org> Subject: Status: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Reply-To: bug#78162 <78162@debbugs.gnu.org> Date: Mon, 23 Jun 2025 00:12:26 +0000 retitle 78162 [PATCH] hash-table-contains-p: Avoid creating a symbol on eve= ry call reassign 78162 emacs submitter 78162 Daniel Mendler severity 78162 normal tag 78162 patch thanks From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 30 06:04:00 2025 Received: (at submit) by debbugs.gnu.org; 30 Apr 2025 10:04:00 +0000 Received: from localhost ([127.0.0.1]:39916 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uA4IK-0000Jb-98 for submit@debbugs.gnu.org; Wed, 30 Apr 2025 06:04:00 -0400 Received: from lists.gnu.org ([2001:470:142::17]:53376) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uA4IG-0000J3-Ux for submit@debbugs.gnu.org; Wed, 30 Apr 2025 06:03:57 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uA4IA-0000XM-OA for bug-gnu-emacs@gnu.org; Wed, 30 Apr 2025 06:03:51 -0400 Received: from server.qxqx.de ([2a01:4f8:c012:9177::1] helo=mail.qxqx.de) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uA4I8-0007FJ-EB for bug-gnu-emacs@gnu.org; Wed, 30 Apr 2025 06:03:50 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=daniel-mendler.de; s=key; h=Content-Type:MIME-Version:Message-ID:Date: Subject:Cc:To:From:Sender:Reply-To:Content-Transfer-Encoding:Content-ID: Content-Description:Resent-Date:Resent-From:Resent-Sender:Resent-To:Resent-Cc :Resent-Message-ID:In-Reply-To:References:List-Id:List-Help:List-Unsubscribe: List-Subscribe:List-Post:List-Owner:List-Archive; bh=wx6ONVVpFScmcU54fhvO2SlmpKIeWcSAJmAi6zkvQ98=; b=p/iZMxMcGfMY9P6X4mp2cE+ijl CZe/zuCRMdGWb01zxsh/pLY31UHA25MgAQLiRNIzjUMpuzE/XCNxFn+dBUK+9zLoMACwlb9f19PtS Ypdpt9BdDAQWaFndqadDhR9V1hat695EwWsZC7RKlYJxIx6kWAcR244cRqL1LAOAAluI=; From: Daniel Mendler To: bug-gnu-emacs@gnu.org Subject: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Date: Wed, 30 Apr 2025 12:03:27 +0200 Message-ID: <871ptage4w.fsf@daniel-mendler.de> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Received-SPF: pass client-ip=2a01:4f8:c012:9177::1; envelope-from=mail@daniel-mendler.de; helo=mail.qxqx.de X-Spam_score_int: -27 X-Spam_score: -2.8 X-Spam_bar: -- X-Spam_report: (-2.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, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.9 (/) X-Debbugs-Envelope-To: submit Cc: Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.1 (/) --=-=-= Content-Type: text/plain Tags: patch For performance reasons avoid creating a symbol on every call to `hash-table-contains-p'. See also https://github.com/emacs-compat/compat/issues/71. --=-=-= Content-Type: text/patch Content-Disposition: attachment; filename=0001-hash-table-contains-p-Avoid-creating-a-symbol-on-eve.patch >From b5a16167a27a355b506d53655816b0c22979acf7 Mon Sep 17 00:00:00 2001 From: Daniel Mendler Date: Wed, 30 Apr 2025 12:00:59 +0200 Subject: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call For performance reasons avoid creating a symbol on every call to `hash-table-contains-p'. lisp/subr.el (hash-table--missing): New symbol. (hash-table-contains-p): Use it. --- lisp/subr.el | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lisp/subr.el b/lisp/subr.el index a5c850ebe5e..892ddfaa66a 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -7436,12 +7436,12 @@ string-trim (declare (important-return-value t)) (string-trim-left (string-trim-right string trim-right) trim-left)) +(defconst hash-table--missing (make-symbol "hash-table--missing")) (defsubst hash-table-contains-p (key table) "Return non-nil if TABLE has an element with KEY." (declare (side-effect-free t) (important-return-value t)) - (let ((missing (make-symbol "missing"))) - (not (eq (gethash key table missing) missing)))) + (not (eq (gethash key table hash-table--missing) hash-table--missing))) ;; The initial anchoring is for better performance in searching matches. (defconst regexp-unmatchable "\\`a\\`" -- 2.47.2 --=-=-=-- From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 30 07:41:41 2025 Received: (at 78162) by debbugs.gnu.org; 30 Apr 2025 11:41:42 +0000 Received: from localhost ([127.0.0.1]:40351 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uA5or-0003Bn-I2 for submit@debbugs.gnu.org; Wed, 30 Apr 2025 07:41:41 -0400 Received: from mail-24416.protonmail.ch ([109.224.244.16]:30473) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uA5om-0003BQ-JY for 78162@debbugs.gnu.org; Wed, 30 Apr 2025 07:41:39 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1746013289; x=1746272489; bh=thPra4qYF4qctnNhe7w8K/WTNU2nV6jCZ5kOQO/jEPI=; h=Date:To:From:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=R/Rm5qImDt7nQiFoV+mdNbTh20H2PpZO+yysyya7Gjw0rko75tUgV4neC49OKgiv+ iAhEQ9C8EIM/oYJ6s/O9wAxDiGEgw0KP7Y8qwpKMhbuheexOOaxWxBcB2MXDSrb70Z Toy4vq5ZCIlqABdVAc+UFc7CVRKB9sWW4GUNScGv8QE6nWq/vFhiFbeGMoIz8D4tij pIUJxn0T37yo7uumP1NxK7ZHInrhUIHOrxqPm6hQm3THTqg+av26IsMG9oFI8JvhUd 24Y6YcoLOElsYpKyI46PMRFTNZ863oT8d6PLwhnKlR5+R0xNIbhFfij/UNhgS7J0F4 G9DtmP/LICUIg== Date: Wed, 30 Apr 2025 11:41:23 +0000 To: 78162@debbugs.gnu.org, Daniel Mendler , Stefan Kangas From: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Message-ID: <87tt65lvvk.fsf@protonmail.com> In-Reply-To: <871ptage4w.fsf@daniel-mendler.de> References: <871ptage4w.fsf@daniel-mendler.de> Feedback-ID: 112775352:user:proton X-Pm-Message-ID: 703c3da6ec783cea6c82c06e3be2ee508582fd42 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78162 X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) "Daniel Mendler via \"Bug reports for GNU Emacs, the Swiss army knife of te= xt editors\"" writes: > Tags: patch > > For performance reasons avoid creating a symbol on every call to > `hash-table-contains-p'. See also https://github.com/emacs-compat/compat= /issues/71. If the performance of hash-table-contains-p is an issue, we should reimplement it in C, which can use magic values which aren't accessible from Lisp. Exposing the magic value to Lisp will cause trouble, even if it's "just" a defconst as in your patch. The current code is somewhat inefficient but seems to me to be correct. Even using an uninterned symbol using its special read syntax (#:missing) may be a problem because of the position table the reader sometimes keeps. I say the code "seems" to be correct because depending on how the defsubst is inlined, an environment may be accessible to the debugger in which the magic value is leaked. Byte-compiling the function results in code which never leaks the value to Lisp, so in practice there shouldn't be a problem once subr.elc exists. (As the problem is the defun, not the bytecode, turning the defsubst into a defun wouldn't help). My guess is most code using hash-table-contains-p will be inefficient anyway, because it will either attempt to retrieve the entry, to create a new entry, or to remove the entry, all of which mean we have to hash the key again. All of that can be optimized, of course. Pip From debbugs-submit-bounces@debbugs.gnu.org Wed Apr 30 10:00:59 2025 Received: (at 78162) by debbugs.gnu.org; 30 Apr 2025 14:00:59 +0000 Received: from localhost ([127.0.0.1]:43415 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uA7ze-0005n6-Mx for submit@debbugs.gnu.org; Wed, 30 Apr 2025 10:00:59 -0400 Received: from server.qxqx.de ([2a01:4f8:c012:9177::1]:33171 helo=mail.qxqx.de) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uA7zV-0005mk-NA for 78162@debbugs.gnu.org; Wed, 30 Apr 2025 10:00:55 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=daniel-mendler.de; s=key; h=Content-Type:MIME-Version:Message-ID:Date: References:In-Reply-To:Subject:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=HEMilGv1Trgix7Y7sae6HbbArsSbo74siMgg0h8/QQo=; b=diAYbEF29j/mTuc6oKh0nMkx/f JAAm3Z8S/L++TJJoguKGeDwdEKZiy6L+zOSSVa4wtw+EwZ2wH/5XamdzbXHR2r5I66af5SzhCj8qI UCVjaC+xvHqpcQnBMTEV3zXAiwGqteM5X8kFq/Q4OIq0kE/9uYeX6Ak/pP93Kqw4Lfto=; From: Daniel Mendler To: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: <87tt65lvvk.fsf@protonmail.com> (Pip Cet's message of "Wed, 30 Apr 2025 11:41:23 +0000") References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> Date: Wed, 30 Apr 2025 16:00:40 +0200 Message-ID: <87y0vhg35j.fsf@daniel-mendler.de> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 78162 Cc: 78162@debbugs.gnu.org, Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) Pip Cet writes: > "Daniel Mendler via \"Bug reports for GNU Emacs, the Swiss army knife of text editors\"" writes: > >> Tags: patch >> >> For performance reasons avoid creating a symbol on every call to >> `hash-table-contains-p'. See also https://github.com/emacs-compat/compat/issues/71. > > If the performance of hash-table-contains-p is an issue, we should > reimplement it in C, which can use magic values which aren't accessible > from Lisp. For a proper hash table I expect this function to be efficient and not allocate. A magic value becomes a problem if it is actually used in Lisp. Is this likely? This should not happen by accident, only if `hash-table--missing` is used intentionally. Note that the actual symbol which is used for the check is uninterned. > Pip From debbugs-submit-bounces@debbugs.gnu.org Thu May 01 05:38:18 2025 Received: (at 78162) by debbugs.gnu.org; 1 May 2025 09:38:18 +0000 Received: from localhost ([127.0.0.1]:48670 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uAQMz-00047U-TT for submit@debbugs.gnu.org; Thu, 01 May 2025 05:38:18 -0400 Received: from mail-24418.protonmail.ch ([109.224.244.18]:11987) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uAQMw-00047A-6E for 78162@debbugs.gnu.org; Thu, 01 May 2025 05:38:15 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1746092287; x=1746351487; bh=sgEqbQClit+WVyyrfrFMxIb61R+4N6Ud2mv75lO1uHk=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=icXsg643pIiI+PSCNn4KaFdwIRBKScQ/ehFol0jg9V8wgbnjjaRp0ddWPqDRIHOQq Lvw4jP3YEXcVeV0McVQK1axdw5dWA0HEc/XZ4KQ9DicLWhWPHs4wvu/JJIvm28ijge dplURvlrcSkjrsCmwB4J30wQ8xGO+MSZg2B5IdJksg4OGKAzeabE46b9VPkosKbw/0 lTLYtopJ8sjpsEfqTvpg7wqoVe+ym47Xiv6kC+Hs2sn95ctGSItFAY17Zy/SeteRWg e++aT4eSwvPJ56tF2XwBznPl3RteWL8thOy6NSbk1Hqg+/izCSCGQLyoVOd3ioS6uZ SK/LwG0Y6RLBQ== Date: Thu, 01 May 2025 09:38:04 +0000 To: Daniel Mendler From: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Message-ID: <878qngllhj.fsf@protonmail.com> In-Reply-To: <87y0vhg35j.fsf@daniel-mendler.de> References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> Feedback-ID: 112775352:user:proton X-Pm-Message-ID: 7713aa9e648494897b2549ff66758e3fdd198b4f MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78162 Cc: 78162@debbugs.gnu.org, Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) "Daniel Mendler" writes: > Pip Cet writes: > >> "Daniel Mendler via \"Bug reports for GNU Emacs, the Swiss army knife of= text editors\"" writes: >> >>> Tags: patch >>> >>> For performance reasons avoid creating a symbol on every call to >>> `hash-table-contains-p'. See also https://github.com/emacs-compat/comp= at/issues/71. >> >> If the performance of hash-table-contains-p is an issue, we should >> reimplement it in C, which can use magic values which aren't accessible >> from Lisp. > > For a proper hash table I expect this function to be efficient and not > allocate. C would work, then. As it avoids both your performance concerns and my concerns about leaking magic values to Lisp, how about this? >From 2a0103a8e67268eed0860baca397a0713335ba0d Mon Sep 17 00:00:00 2001 From: Pip Cet Subject: [PATCH] Implement 'hash-table-contains-p' in C (bug#78162) * lisp/emacs-lisp/byte-opt.el (side-effect-free-fns): Add 'hash-table-contains-p'. * lisp/subr.el (hash-table-contains-p): Function removed. * src/fns.c (Fhash_table_contains_p): New function. (syms_of_fns): Register it. * test/lisp/subr-tests.el (hash-table-contains-p): Move... * test/src/fns-tests.el (test-hash-table-contains-p): ...here. --- lisp/emacs-lisp/byte-opt.el | 2 +- lisp/subr.el | 7 ------- src/fns.c | 12 ++++++++++++ test/lisp/subr-tests.el | 12 ------------ test/src/fns-tests.el | 12 ++++++++++++ 5 files changed, 25 insertions(+), 20 deletions(-) diff --git a/lisp/emacs-lisp/byte-opt.el b/lisp/emacs-lisp/byte-opt.el index 652c79e9c93..f93946635a0 100644 --- a/lisp/emacs-lisp/byte-opt.el +++ b/lisp/emacs-lisp/byte-opt.el @@ -1761,7 +1761,7 @@ byte-optimize-set compare-strings concat copy-alist copy-hash-table copy-sequence e= lt equal equal-including-properties featurep get - gethash hash-table-count hash-table-rehash-size + gethash hash-table-contains-p hash-table-count hash-table-rehash-= size hash-table-rehash-threshold hash-table-size hash-table-test hash-table-weakness length length< length=3D length> diff --git a/lisp/subr.el b/lisp/subr.el index a5c850ebe5e..6a0487e96ee 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -7436,13 +7436,6 @@ string-trim (declare (important-return-value t)) (string-trim-left (string-trim-right string trim-right) trim-left)) =20 -(defsubst hash-table-contains-p (key table) - "Return non-nil if TABLE has an element with KEY." - (declare (side-effect-free t) - (important-return-value t)) - (let ((missing (make-symbol "missing"))) - (not (eq (gethash key table missing) missing)))) - ;; The initial anchoring is for better performance in searching matches. (defconst regexp-unmatchable "\\`a\\`" "Standard regexp guaranteed not to match any string at all.") diff --git a/src/fns.c b/src/fns.c index 21916b6fb46..a3b45232700 100644 --- a/src/fns.c +++ b/src/fns.c @@ -5866,6 +5866,17 @@ DEFUN ("gethash", Fgethash, Sgethash, 2, 3, 0, } =20 =20 +DEFUN ("hash-table-contains-p", Fhash_table_contains_p, Shash_table_contai= ns_p, + 2, 2, 0, + doc: /* Return non-nil if TABLE has an element with KEY. */) + (Lisp_Object key, Lisp_Object table) +{ + struct Lisp_Hash_Table *h =3D check_hash_table (table); + ptrdiff_t i =3D hash_find (h, key); + return i >=3D 0 ? Qt : Qnil; +} + + DEFUN ("puthash", Fputhash, Sputhash, 3, 3, 0, doc: /* Associate KEY with VALUE in hash table TABLE. If KEY is already present in table, replace its current value with @@ -6686,6 +6697,7 @@ syms_of_fns (void) defsubr (&Shash_table_p); defsubr (&Sclrhash); defsubr (&Sgethash); + defsubr (&Shash_table_contains_p); defsubr (&Sputhash); defsubr (&Sremhash); defsubr (&Smaphash); diff --git a/test/lisp/subr-tests.el b/test/lisp/subr-tests.el index 024cbe85bba..f3adfcaed62 100644 --- a/test/lisp/subr-tests.el +++ b/test/lisp/subr-tests.el @@ -1493,17 +1493,5 @@ subr--subst-char-in-string (props-out (object-intervals out))) (should (equal props-out props-in)))))))) =20 -(ert-deftest hash-table-contains-p () - (let ((h (make-hash-table))) - (should-not (hash-table-contains-p 'problems h)) - (should-not (hash-table-contains-p 'cookie h)) - (should-not (hash-table-contains-p 'milk h)) - (puthash 'problems 99 h) - (puthash 'cookie nil h) - (puthash 'milk 'missing h) - (should (hash-table-contains-p 'problems h)) - (should (hash-table-contains-p 'cookie h)) - (should (hash-table-contains-p 'milk h)))) - (provide 'subr-tests) ;;; subr-tests.el ends here diff --git a/test/src/fns-tests.el b/test/src/fns-tests.el index e6abcdc34e7..d3e9512434a 100644 --- a/test/src/fns-tests.el +++ b/test/src/fns-tests.el @@ -1149,6 +1149,18 @@ test-hash-table-wrong-keywords (should (make-hash-table :rehash-threshold 123)) ; obsolete and ignored (should-error (make-hash-table :some-random-keyword 123))) =20 +(ert-deftest test-hash-table-contains-p () + (let ((h (make-hash-table))) + (should-not (hash-table-contains-p 'problems h)) + (should-not (hash-table-contains-p 'cookie h)) + (should-not (hash-table-contains-p 'milk h)) + (puthash 'problems 99 h) + (puthash 'cookie nil h) + (puthash 'milk 'missing h) + (should (hash-table-contains-p 'problems h)) + (should (hash-table-contains-p 'cookie h)) + (should (hash-table-contains-p 'milk h)))) + (ert-deftest test-remhash () (let ((h (make-hash-table)) (val "anything")) --=20 2.48.1 > A magic value becomes a problem if it is actually used in Lisp. Is > this likely? This should not happen by accident, only if > `hash-table--missing` is used intentionally. I think it's reasonable to build a hash table associating each symbol's name to its value, in order to detect which symbol values change as code is executed. With your patch, this code will result in a hash table which has a false negative for (hash-table-contains-p "hash-table--missing" h): (defun dump-obarray () (let ((h (make-hash-table :test 'equal))) (mapatoms (lambda (sym) (when (boundp sym) (puthash (symbol-name sym) (= symbol-value sym) h)))) h)) (let ((old (dump-obarray)) (new (dump-obarray))) (maphash (lambda (key value) (if (hash-table-contains-p key old) (unless (eq (gethash key old) (gethash key new)) (message "value of %S changed" key)) (message "%S newly defined to be %S" key value))) new)) With the C implementation, this would work, of course. > Note that the actual symbol which is used for the check is uninterned. I understand that it's traditional to use uninterned symbols for cases like this one, but strings or conses (depending on whether you want extra information) would work just as well, and be a bit cheaper. Floats would work and be cheapest, but might break if our representation of them changes, as it has in my local tree. In any case, IMHO, it's not the magic value that's the problem here, it's that it's stored as a global symbol's value and thus becomes reachable via the obarray. Pip From debbugs-submit-bounces@debbugs.gnu.org Thu May 01 11:37:31 2025 Received: (at 78162) by debbugs.gnu.org; 1 May 2025 15:37:31 +0000 Received: from localhost ([127.0.0.1]:52400 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uAVyc-0002n0-Q8 for submit@debbugs.gnu.org; Thu, 01 May 2025 11:37:31 -0400 Received: from server.qxqx.de ([2a01:4f8:c012:9177::1]:58445 helo=mail.qxqx.de) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uAVyZ-0002mV-HK for 78162@debbugs.gnu.org; Thu, 01 May 2025 11:37:28 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=daniel-mendler.de; s=key; h=Content-Type:MIME-Version:Message-ID:Date: References:In-Reply-To:Subject:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=ShYPbfCdS+L5Y6u62INVtuiD2BeRboh/PM9LYqF2H9Y=; b=HGcl0SWv/DZnotEK5XRGNhMCkG X5l+XeIH6VyTu6jK04AvvgQH/Y7WY0dHeuWUmA7fIrOQAjTi2Imlf1vTadCpXqlXtbc6IFXZ2Cx14 2nwHODMbKhMjzEUiKBx6sFp3ZetQ5B0OclxCcYgdcoq0Kf5EyDI1/U/SVdUOJX/zTrAA=; From: Daniel Mendler To: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: <878qngllhj.fsf@protonmail.com> (Pip Cet's message of "Thu, 01 May 2025 09:38:04 +0000") References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> Date: Thu, 01 May 2025 17:37:19 +0200 Message-ID: <877c30tk9c.fsf@daniel-mendler.de> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 78162 Cc: 78162@debbugs.gnu.org, Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) Pip Cet writes: > "Daniel Mendler" writes: > >> Pip Cet writes: >> >>> "Daniel Mendler via \"Bug reports for GNU Emacs, the Swiss army knife of text editors\"" writes: >>> >>>> Tags: patch >>>> >>>> For performance reasons avoid creating a symbol on every call to >>>> `hash-table-contains-p'. See also https://github.com/emacs-compat/compat/issues/71. >>> >>> If the performance of hash-table-contains-p is an issue, we should >>> reimplement it in C, which can use magic values which aren't accessible >>> from Lisp. >> >> For a proper hash table I expect this function to be efficient and not >> allocate. > > C would work, then. As it avoids both your performance concerns and my > concerns about leaking magic values to Lisp, how about this? Looks good. Thanks. > Pip From debbugs-submit-bounces@debbugs.gnu.org Sat May 10 06:19:22 2025 Received: (at 78162) by debbugs.gnu.org; 10 May 2025 10:19:22 +0000 Received: from localhost ([127.0.0.1]:44263 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uDhIg-0002lo-9T for submit@debbugs.gnu.org; Sat, 10 May 2025 06:19:22 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]:34278) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uDhIe-0002la-AC for 78162@debbugs.gnu.org; Sat, 10 May 2025 06:19:21 -0400 Received: from fencepost.gnu.org ([2001:470:142:3::e]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uDhIX-00067v-Rw; Sat, 10 May 2025 06:19:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=gnu.org; s=fencepost-gnu-org; h=References:Subject:In-Reply-To:To:From:Date: mime-version; bh=+jOzEn1pbjTT4jcF0fc5p1g2efqs2iz7+Z1u/x7AQRs=; b=re+hAkXW2R0E kSNlBpkLQ8u5GFGeEzXWuyvjJdZH23xYzYKgVVEYKEKzZ5LePUIRlKkPwPeKelUttmpxZCI4yFYCO iQuIAiuTVQg3pHa9etXnskvH2Z5AgYm+2rNNAuWk8EjQ8/gyTruxczHTpQtnKMXVUJbNt3/VUHPcm QovQ2pTqKLH5QB3zPO41c214SNQSuxbe4tan9ukq6HdG1u3kYL8pftoq0lWfQTJkw7vYXmNtwYyzK +XAPMbp7EKzhoSLGb1y+N6YuuPQyyr3h8Omp9zEJRzO4lLy1rrdqBAJpUGMbEUyF7JFelziY+MeOO g6I9xTr99zHD9kfrQWSa/w==; Date: Sat, 10 May 2025 13:19:10 +0300 Message-Id: <86frhcahup.fsf@gnu.org> From: Eli Zaretskii To: Daniel Mendler , Stefan Monnier In-Reply-To: <877c30tk9c.fsf@daniel-mendler.de> (bug-gnu-emacs@gnu.org) Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> <877c30tk9c.fsf@daniel-mendler.de> X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78162 Cc: 78162@debbugs.gnu.org, pipcet@protonmail.com, stefankangas@gmail.com X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > Cc: 78162@debbugs.gnu.org, Stefan Kangas > Date: Thu, 01 May 2025 17:37:19 +0200 > From: Daniel Mendler via "Bug reports for GNU Emacs, > the Swiss army knife of text editors" > > Pip Cet writes: > > > "Daniel Mendler" writes: > > > >> Pip Cet writes: > >> > >>> "Daniel Mendler via \"Bug reports for GNU Emacs, the Swiss army knife of text editors\"" writes: > >>> > >>>> Tags: patch > >>>> > >>>> For performance reasons avoid creating a symbol on every call to > >>>> `hash-table-contains-p'. See also https://github.com/emacs-compat/compat/issues/71. > >>> > >>> If the performance of hash-table-contains-p is an issue, we should > >>> reimplement it in C, which can use magic values which aren't accessible > >>> from Lisp. > >> > >> For a proper hash table I expect this function to be efficient and not > >> allocate. > > > > C would work, then. As it avoids both your performance concerns and my > > concerns about leaking magic values to Lisp, how about this? > > Looks good. Thanks. Stefan, any comments to the patch? From debbugs-submit-bounces@debbugs.gnu.org Sun May 11 01:31:44 2025 Received: (at 78162) by debbugs.gnu.org; 11 May 2025 05:31:44 +0000 Received: from localhost ([127.0.0.1]:56153 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uDzHr-0002zi-S0 for submit@debbugs.gnu.org; Sun, 11 May 2025 01:31:44 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:29988) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uDzHo-0002zU-SU for 78162@debbugs.gnu.org; Sun, 11 May 2025 01:31:41 -0400 Received: from pmg2.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 980D8808A4; Sun, 11 May 2025 01:31:34 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1746941493; bh=TC9AXucGekuoooqx+hyZuRpdWQ6ZPLv1PnYeUINkczo=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=YTtKM44hINDgWEY91p7xNewMf8ot+jYF3MwMaIdfEpCVRbHVsLTEVMrIzMkzJPPd4 81dOh0OKDZAjU99VNWWceC+W8bnib4ZYFYisl/Jo6EX3froAa5VqXS2pZWwkUiX1nM vCREPBr08SHvA636oApcpN3HrQcBKwmmQWJElV4e3PS9kYnDdNj8r10XBoezAVSWk+ rhIDrPn/6bVOitkR99xkmaYihYbF+0gZJfeGXX+C6DAUGM/UYDJzETLGbSRchalc6X lBFYUf2yB6pw/shbApZKIoFGfRWwykrb8hLWySQXAvBJnaI81slbLNKXPZmcXaXB5T V1oOs0ysvDeqQ== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg2.iro.umontreal.ca (Proxmox) with ESMTP id 86F2D803A3; Sun, 11 May 2025 01:31:33 -0400 (EDT) Received: from pastel (104-195-232-56.cpe.teksavvy.com [104.195.232.56]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 4016F120426; Sun, 11 May 2025 01:31:33 -0400 (EDT) From: Stefan Monnier To: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: <878qngllhj.fsf@protonmail.com> Message-ID: References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> Date: Sun, 11 May 2025 01:31:31 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.090 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78162 Cc: Daniel Mendler , Stefan Kangas , 78162@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) > In any case, IMHO, it's not the magic value that's the problem here, > it's that it's stored as a global symbol's value and thus becomes > reachable via the obarray. Then an alternative would be the simpler patch below? > Stefan, any comments to the patch? At this point we're firmly in bikeshed territory. I agree with Daniel that the function should not allocate, but I also agree with Pip Cet that the performance of the function shouldn't matter very much, so maybe my ELisp version provides a middle ground? [ Ideally, this function would be replaced with one which doesn't return just `t` but an actual "handle" on the hash-table slot, so that we can then access that slot without redoing the key lookup. It would allow doing thing like `push` or `incf` on a hash-table entry without looking up the key twice. But doing so without allocating an object is hard with the current hash-table design (and would come with its own downsides). ] Stefan diff --git a/lisp/subr.el b/lisp/subr.el index 79288921bed..962ae61cfd8 100644 --- a/lisp/subr.el +++ b/lisp/subr.el @@ -7436,11 +7436,11 @@ (declare (important-return-value t)) (string-trim-left (string-trim-right string trim-right) trim-left)) -(defsubst hash-table-contains-p (key table) +(let ((missing (make-symbol "missing"))) + (defsubst hash-table-contains-p (key table) "Return non-nil if TABLE has an element with KEY." (declare (side-effect-free t) (important-return-value t)) - (let ((missing (make-symbol "missing"))) (not (eq (gethash key table missing) missing)))) ;; The initial anchoring is for better performance in searching matches. From debbugs-submit-bounces@debbugs.gnu.org Sun May 11 02:02:47 2025 Received: (at submit) by debbugs.gnu.org; 11 May 2025 06:02:47 +0000 Received: from localhost ([127.0.0.1]:56511 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uDzlu-0005MH-Mj for submit@debbugs.gnu.org; Sun, 11 May 2025 02:02:46 -0400 Received: from lists.gnu.org ([2001:470:142::17]:54650) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uDzls-0005M0-PM for submit@debbugs.gnu.org; Sun, 11 May 2025 02:02:45 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uDzli-0008SX-GJ for bug-gnu-emacs@gnu.org; Sun, 11 May 2025 02:02:38 -0400 Received: from ciao.gmane.io ([116.202.254.214]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uDzlg-0002JG-Ip for bug-gnu-emacs@gnu.org; Sun, 11 May 2025 02:02:34 -0400 Received: from list by ciao.gmane.io with local (Exim 4.92) (envelope-from ) id 1uDzlc-0007E8-M5 for bug-gnu-emacs@gnu.org; Sun, 11 May 2025 08:02:28 +0200 X-Injected-Via-Gmane: http://gmane.org/ To: bug-gnu-emacs@gnu.org From: Rudolf Schlatte Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Date: Sun, 11 May 2025 08:02:03 +0200 Message-ID: References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> Mime-Version: 1.0 Content-Type: text/plain User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:UvAKFYG7kFGgiBHw36S3stuagGM= Received-SPF: pass client-ip=116.202.254.214; envelope-from=geb-bug-gnu-emacs@m.gmane-mx.org; helo=ciao.gmane.io X-Spam_score_int: -18 X-Spam_score: -1.9 X-Spam_bar: - X-Spam_report: (-1.9 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.001, RCVD_IN_VALIDITY_CERTIFIED_BLOCKED=0.001, RCVD_IN_VALIDITY_RPBL_BLOCKED=0.001, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: submit X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" writes: > [ Ideally, this function would be replaced with one which doesn't return > just `t` but an actual "handle" on the hash-table slot, so that we can > then access that slot without redoing the key lookup. It would allow > doing thing like `push` or `incf` on a hash-table entry without > looking up the key twice. > But doing so without allocating an object is hard with the current > hash-table design (and would come with its own downsides). ] A micro-optimization could be for the hash table to remember the last key lookup, so that something like this hits a fast path for the second lookup of the same key: (when (hash-table-contains-p key table) (push 'x (gethash table key))) From debbugs-submit-bounces@debbugs.gnu.org Sun May 11 04:02:42 2025 Received: (at submit) by debbugs.gnu.org; 11 May 2025 08:02:42 +0000 Received: from localhost ([127.0.0.1]:57512 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uE1dx-0007d6-Lz for submit@debbugs.gnu.org; Sun, 11 May 2025 04:02:42 -0400 Received: from lists.gnu.org ([2001:470:142::17]:49046) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uE1dn-0007cM-7R for submit@debbugs.gnu.org; Sun, 11 May 2025 04:02:31 -0400 Received: from eggs.gnu.org ([2001:470:142:3::10]) by lists.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uE1dd-0005NV-KC for bug-gnu-emacs@gnu.org; Sun, 11 May 2025 04:02:22 -0400 Received: from mail-out.m-online.net ([2001:a60:0:28:0:1:25:1]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1uE1db-0006Jg-C9 for bug-gnu-emacs@gnu.org; Sun, 11 May 2025 04:02:21 -0400 Received: from frontend01.mail.m-online.net (unknown [192.168.8.182]) by mail-out.m-online.net (Postfix) with ESMTP id 4ZwFZb2NZkz1sG88; Sun, 11 May 2025 10:02:15 +0200 (CEST) Received: from localhost (dynscan1.mnet-online.de [192.168.6.68]) by mail.m-online.net (Postfix) with ESMTP id 4ZwFZb1XG6z1qqlW; Sun, 11 May 2025 10:02:15 +0200 (CEST) X-Virus-Scanned: amavis at mnet-online.de Received: from mail.mnet-online.de ([192.168.8.182]) by localhost (dynscan1.mail.m-online.net [192.168.6.68]) (amavis, port 10024) with ESMTP id XCzGF_-KIxPn; Sun, 11 May 2025 10:02:04 +0200 (CEST) X-Auth-Info: RC30UAfgeC0KeDnpSF1y/WsXiIt8uB8k31lYWjYhKtIKblrfYxu3nmdkUzIQcGUC Received: from tiger (aftr-82-135-83-89.dynamic.mnet-online.de [82.135.83.89]) (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits) key-exchange ECDHE (P-256) server-signature RSA-PSS (2048 bits) server-digest SHA256) (No client certificate requested) by mail.mnet-online.de (Postfix) with ESMTPSA; Sun, 11 May 2025 10:02:04 +0200 (CEST) From: Andreas Schwab To: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: (Stefan Monnier via's message of "Sun, 11 May 2025 01:31:31 -0400") References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> X-Yow: I represent a sardine!! Date: Sun, 11 May 2025 10:02:03 +0200 Message-ID: <87bjrzmv7o.fsf@linux-m68k.org> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain Received-SPF: pass client-ip=2001:a60:0:28:0:1:25:1; envelope-from=whitebox@nefkom.net; helo=mail-out.m-online.net X-Spam_score_int: -25 X-Spam_score: -2.6 X-Spam_bar: -- X-Spam_report: (-2.6 / 5.0 requ) BAYES_00=-1.9, HEADER_FROM_DIFFERENT_DOMAINS=0.001, RCVD_IN_DNSWL_LOW=-0.7, SPF_HELO_NONE=0.001, SPF_PASS=-0.001 autolearn=ham autolearn_force=no X-Spam_action: no action X-Spam-Score: 1.0 (+) X-Debbugs-Envelope-To: submit Cc: Daniel Mendler , Pip Cet , 78162@debbugs.gnu.org, Stefan Monnier , Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -0.0 (/) On Mai 11 2025, Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" wrote: > diff --git a/lisp/subr.el b/lisp/subr.el > index 79288921bed..962ae61cfd8 100644 > --- a/lisp/subr.el > +++ b/lisp/subr.el > @@ -7436,11 +7436,11 @@ > (declare (important-return-value t)) > (string-trim-left (string-trim-right string trim-right) trim-left)) > > -(defsubst hash-table-contains-p (key table) > +(let ((missing (make-symbol "missing"))) > + (defsubst hash-table-contains-p (key table) > "Return non-nil if TABLE has an element with KEY." > (declare (side-effect-free t) > (important-return-value t)) > - (let ((missing (make-symbol "missing"))) Or use '#:missing instead of (make-symbol "missing"). -- Andreas Schwab, schwab@linux-m68k.org GPG Key fingerprint = 7578 EB47 D4E5 4D69 2510 2552 DF73 E780 A9DA AEC1 "And now for something completely different." From debbugs-submit-bounces@debbugs.gnu.org Sun May 11 05:45:02 2025 Received: (at 78162) by debbugs.gnu.org; 11 May 2025 09:45:02 +0000 Received: from localhost ([127.0.0.1]:58302 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uE3Ez-0005xJ-OV for submit@debbugs.gnu.org; Sun, 11 May 2025 05:45:02 -0400 Received: from mail-4316.protonmail.ch ([185.70.43.16]:39135) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uE3Ex-0005wY-8n for 78162@debbugs.gnu.org; Sun, 11 May 2025 05:45:00 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1746956691; x=1747215891; bh=BZn5mrUgn0RpmSFACsQIi6/Z6ulwt7ToLoecBYcxn0Y=; h=Date:To:From:Cc:Subject:Message-ID:In-Reply-To:References: Feedback-ID:From:To:Cc:Date:Subject:Reply-To:Feedback-ID: Message-ID:BIMI-Selector:List-Unsubscribe:List-Unsubscribe-Post; b=c+iQpotGarrU4ZlAUlET03QohlCH+/56XBYRV174wLLBfQrGjDiGwfhQIny0doj09 N+vdgxDiO/opoyDeBAVSQKf8MitmOHOzZzVLOQsmvYI3/YyoGm613DmFrAliBxRW9+ GtC2rLZuvDR6Fm9t3ulHxTlRtfswNZ3/GQnRyXW1U9UQG2MncYLngMaPCssWhXzpyt 8mZnN34r2gVx/f+Aec5IUipq3hYrXmC68s1B+JvMauUvI/Mm0PZX2dHIKhubfggJz3 kR24nVk+tPlNCWWFQv9AFkSBcZqiF6RzUa1s02vW3ll3AF90xDsRzQZawFZrRicftj ok602CZP+0gdQ== Date: Sun, 11 May 2025 09:44:46 +0000 To: Stefan Monnier From: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call Message-ID: <8734dbh46t.fsf@protonmail.com> In-Reply-To: References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> Feedback-ID: 112775352:user:proton X-Pm-Message-ID: d653f806a4b7059d8edeb02c7bc4cc2c3a336ec2 MIME-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78162 Cc: Daniel Mendler , Stefan Kangas , 78162@debbugs.gnu.org X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) "Stefan Monnier" writes: >> In any case, IMHO, it's not the magic value that's the problem here, >> it's that it's stored as a global symbol's value and thus becomes >> reachable via the obarray. > > Then an alternative would be the simpler patch below? I tried hard to find something wrong with it, but couldn't. The debugging experience when single-stepping through that function isn't very good, but that's a separate bug (the debugger should probably bind print-gensym). I have no opinion on make-symbol vs #: syntax. As it's all evaluated prior to dumping, it doesn't matter much how we construct the symbol: we dump the final byte code, which looks good to me. > [ Ideally, this function would be replaced with one which doesn't return > just `t` but an actual "handle" on the hash-table slot, so that we can > then access that slot without redoing the key lookup. It would allow > doing thing like `push` or `incf` on a hash-table entry without > looking up the key twice. > But doing so without allocating an object is hard with the current > hash-table design (and would come with its own downsides). ] I think the low-hanging fruit is probably changing the hash table implementation so it caches the last key looked up and its index, but even that minor complication hasn't been necessary so far, I think. This might change if we make sxhash more resilient against hash collisions by increasing SXHASH_MAX_DEPTH or SXHASH_MAX_LENGTH. Pip From debbugs-submit-bounces@debbugs.gnu.org Mon May 12 04:17:44 2025 Received: (at 78162) by debbugs.gnu.org; 12 May 2025 08:17:44 +0000 Received: from localhost ([127.0.0.1]:49878 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uEOM3-0001u2-Qf for submit@debbugs.gnu.org; Mon, 12 May 2025 04:17:44 -0400 Received: from mail-ed1-x52a.google.com ([2a00:1450:4864:20::52a]:43389) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_128_GCM_SHA256:128) (Exim 4.84_2) (envelope-from ) id 1uEOM0-0001te-1v for 78162@debbugs.gnu.org; Mon, 12 May 2025 04:17:40 -0400 Received: by mail-ed1-x52a.google.com with SMTP id 4fb4d7f45d1cf-5fbf52aad74so8695625a12.1 for <78162@debbugs.gnu.org>; Mon, 12 May 2025 01:17:40 -0700 (PDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=20230601; t=1747037854; x=1747642654; darn=debbugs.gnu.org; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:from:to:cc:subject:date:message-id:reply-to; bh=qj+OvMOZyoi+anrX30aQ5s6BQ4hBTbQz3Nt27pQgdmQ=; b=ndy0lyB1YK+C42kbYv5sS/t+dUoHNuRsj14ZStOZsVakKsRmkyRMa9yByzs1c0ae9c dBO6+wTG3gFyxIiqEq+FAKSRXL2B5B9oOofi18yxYuW0RYA8bsji7AQ7Sve8C0Th+/AN f5f64wzZ/a2dA6Kcqoz802G3ujezqB/1kp1mT+YqupKexJ0HdTLxtGl30yEx4+LVrCQD 7QMVHD39ELUON3m1GtvXE8vGNH6lot+nW4kyislXebANl3feNdXqlp+tHRgVmdAM1iI6 F/WThzRWsJ/HB7YBCH23K4aV3BP1y7E/q97eZuSYAfzgbWmLKZzprgMHnRYKDSrUW93X smBg== X-Google-DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=1e100.net; s=20230601; t=1747037854; x=1747642654; h=mime-version:user-agent:message-id:date:references:in-reply-to :subject:cc:to:from:x-gm-message-state:from:to:cc:subject:date :message-id:reply-to; bh=qj+OvMOZyoi+anrX30aQ5s6BQ4hBTbQz3Nt27pQgdmQ=; b=Un9TlMTb3kvF95w7r2OmqdH73EyJjXeEuFaIymFDnvQHNZsg5SPEFRvSiNbKHTAxnK BPQpoLiN39DrpkuB7QpuXEuGQ7Cv9NIkgTqJqgVE6bOL8/2D1dGw4K+usOw7c5uc/ZSr s3wZ4NwVXaqBSFymOS3JLFmsRTEg4kbPZ/sLl9gp4B+4tmEq3EamYs9Kp0s4BFVI61XX ide5kDODKkiOzCfxLQszoz7xy5FqRpenFwuxwyHy9LfzgiEI0kKL1kcZMcLjXuJePy93 IXjME+WbbvshXTG+Zy2I3z5iDm8FOo932Yvs5cn31J1pdiVlaGYR8j+MJcuen/MhFpxL 71sA== X-Forwarded-Encrypted: i=1; AJvYcCWIYC8Pfb9Sr6lobTwmja7S6H5MgsMlnhIp3MEFk0C3vXRuX1wQHBQx/uBYexLTKY4wvSq0qw==@debbugs.gnu.org X-Gm-Message-State: AOJu0YwWIp9jBkfkTenZw9cFirACQ31/1Q0FF/VRGHprCp76wzMG9Ovd 5MRLoYg9uCfdRn4OR7gKQ+V0p3kWMNYmsAXLINrf5qvbq9CyJDLNYrFSwQ== X-Gm-Gg: ASbGncvbrd/NLgI9NJQwOKef253aEkgUrdLpFgV+32hKJtDBuW0nM24CC6fvA5Zin// Fz1UTe+oke56RVjMhJxVW2O9FzPZzJp/Nd5W45tPxauIolGzWrvStpKWY1OI9Abltm1shegnYrR zjrUaPpUoTQjY6MuOoNBcJ0d4SPI0Dqp8nkAu+SyTAfV57wbx1aLyZxAs7EH3MbtCjqXtxwXbhw 4eVKC9QkYtwx/hJ35Z9TqJFbEzuQ4PoD/QLAooGP/qBLfr1XyUGZtUdv8QdypB/b4dseEK4iorC QeEeQkFz3q3IYQ+WOX0OdynT4JJQiLO7qcAIMPEq+kfR3V9HstJUIhY+LziyKhf8/bSsc735800 2TcQ8y3pQYBaZ2IlkdE8= X-Google-Smtp-Source: AGHT+IHgatXyXi21oiwUyBC+b9rnC1V5UUPOZdN6uJXgbeCSUlVtcsIfOIyEuBFr17hXPk3hqQpV/A== X-Received: by 2002:a17:907:3cd6:b0:ad2:5504:8b35 with SMTP id a640c23a62f3a-ad255048caemr247013566b.19.1747037853402; Mon, 12 May 2025 01:17:33 -0700 (PDT) Received: from caladan (dialin-233080.rol.raiffeisen.net. [195.254.233.80]) by smtp.gmail.com with ESMTPSA id a640c23a62f3a-ad21947abcasm580022466b.84.2025.05.12.01.17.32 (version=TLS1_3 cipher=TLS_AES_256_GCM_SHA384 bits=256/256); Mon, 12 May 2025 01:17:32 -0700 (PDT) From: Helmut Eller To: Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> Date: Mon, 12 May 2025 10:17:32 +0200 Message-ID: <87ecwus0o3.fsf@gmail.com> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: 0.0 (/) X-Debbugs-Envelope-To: 78162 Cc: Daniel Mendler , Pip Cet , 78162@debbugs.gnu.org, Stefan Monnier , Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.0 (-) On Sun, May 11 2025, Stefan Monnier via "Bug reports for GNU Emacs, the Swiss army knife of text editors" wrote: > [ Ideally, this function would be replaced with one which doesn't return > just `t` but an actual "handle" on the hash-table slot, so that we can > then access that slot without redoing the key lookup. It would allow > doing thing like `push` or `incf` on a hash-table entry without > looking up the key twice. > But doing so without allocating an object is hard with the current > hash-table design (and would come with its own downsides). ] Maybe something like the hashtable-update! function from R6RS would work: (hashtable-update! hashtable key proc default) Proc should accept one argument, should return a single value, and should not mutate hashtable. The hashtable-update! procedure applies proc to the value in hashtable associated with key, or to default if hashtable does not contain an association for key. The hashtable is then changed to associate key with the value returned by proc. The behavior of hashtable-update! is equivalent to the following code, but may be implemented more efficiently in cases where the implementation can avoid multiple lookups of the same key: (hashtable-set! hashtable key (proc (hashtable-ref hashtable key default))) A problem with hashtable-update! seems to be that the proc argument is often a lambda expression and would allocate. Though, people who care about that could perhaps use dynamic variables to avoid allocation a closure. On the positive side, this it doesn't need an explicit "handle". Helmut From debbugs-submit-bounces@debbugs.gnu.org Mon May 12 17:21:10 2025 Received: (at 78162-done) by debbugs.gnu.org; 12 May 2025 21:21:10 +0000 Received: from localhost ([127.0.0.1]:55392 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uEaaD-0002TL-QG for submit@debbugs.gnu.org; Mon, 12 May 2025 17:21:10 -0400 Received: from mailscanner.iro.umontreal.ca ([132.204.25.50]:15244) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uEaa7-0002SS-55 for 78162-done@debbugs.gnu.org; Mon, 12 May 2025 17:21:07 -0400 Received: from pmg1.iro.umontreal.ca (localhost.localdomain [127.0.0.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 1A5A810013E; Mon, 12 May 2025 17:20:57 -0400 (EDT) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=iro.umontreal.ca; s=mail; t=1747084856; bh=TfQgNhkBsiLe+ncYaKrM36Cq9fkTLAHHr/0aw+IKoKM=; h=From:To:Cc:Subject:In-Reply-To:References:Date:From; b=O0Wzp9ettX7bbA0gyN1jgOzHJvJmRLo49D9vUhuXNvXy2uMMzMOXlou4QdLmSha16 wrrAc7W/Kz1gUrIvVmspHj4BwwtLKkZ+Mh7+l1xU6hpuH3rHExVTwTHmMpQdzFESJt WDQw0ncg21JG8xPbjbv2AZYfwhzU5mKRKoTGpPqDfwHdD1OGgBpPt8rsS0t1vzLcGl DL8JA/9z6yg7kCynyZ5fESVBVNnOXWj+rOufz+po3O8OpTYjA3UBfSM3EQQP0LmhtE g10mi1vuEoh+x4ko/NwyyUADjXSYBd2a4URJU3hccNkYd9xuRNEnobOzMT/ibYolMA BcOLUvxrp4I3Q== Received: from mail01.iro.umontreal.ca (unknown [172.31.2.1]) by pmg1.iro.umontreal.ca (Proxmox) with ESMTP id 4DA4210002E; Mon, 12 May 2025 17:20:56 -0400 (EDT) Received: from pastel (104-195-232-56.cpe.teksavvy.com [104.195.232.56]) by mail01.iro.umontreal.ca (Postfix) with ESMTPSA id 1ABDB12041D; Mon, 12 May 2025 17:20:56 -0400 (EDT) From: Stefan Monnier To: Pip Cet Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: <8734dbh46t.fsf@protonmail.com> Message-ID: References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> <8734dbh46t.fsf@protonmail.com> Date: Mon, 12 May 2025 17:20:54 -0400 User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-SPAM-INFO: Spam detection results: 0 ALL_TRUSTED -1 Passed through trusted hosts only via SMTP AWL -0.207 Adjusted score from AWL reputation of From: address BAYES_00 -1.9 Bayes spam probability is 0 to 1% DKIM_SIGNED 0.1 Message has a DKIM or DK signature, not necessarily valid DKIM_VALID -0.1 Message has at least one valid DKIM or DK signature DKIM_VALID_AU -0.1 Message has a valid DKIM or DK signature from author's domain DKIM_VALID_EF -0.1 Message has a valid DKIM or DK signature from envelope-from domain X-SPAM-LEVEL: X-Spam-Score: -2.3 (--) X-Debbugs-Envelope-To: 78162-done Cc: Daniel Mendler , 78162-done@debbugs.gnu.org, Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -3.3 (---) >> Then an alternative would be the simpler patch below? > I tried hard to find something wrong with it, but couldn't. Push to `master`, closing, Stefan From debbugs-submit-bounces@debbugs.gnu.org Tue May 13 04:59:14 2025 Received: (at 78162-done) by debbugs.gnu.org; 13 May 2025 08:59:14 +0000 Received: from localhost ([127.0.0.1]:57922 helo=debbugs.gnu.org) by debbugs.gnu.org with esmtp (Exim 4.84_2) (envelope-from ) id 1uElTl-0005xa-W5 for submit@debbugs.gnu.org; Tue, 13 May 2025 04:59:14 -0400 Received: from server.qxqx.de ([2a01:4f8:c012:9177::1]:34519 helo=mail.qxqx.de) by debbugs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.84_2) (envelope-from ) id 1uElTj-0005xI-Ve for 78162-done@debbugs.gnu.org; Tue, 13 May 2025 04:59:13 -0400 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=daniel-mendler.de; s=key; h=Content-Type:MIME-Version:Message-ID:Date: References:In-Reply-To:Subject:Cc:To:From:Sender:Reply-To: Content-Transfer-Encoding:Content-ID:Content-Description:Resent-Date: Resent-From:Resent-Sender:Resent-To:Resent-Cc:Resent-Message-ID:List-Id: List-Help:List-Unsubscribe:List-Subscribe:List-Post:List-Owner:List-Archive; bh=6pTWvEHcRHOg+hiNJflk1CAsSyPVnrGLESZUQk05HzQ=; b=tIXERQUyj4IwL6MUrcYVXvNm/d caYl9TSiCsdOjIeRj8jmDoZ2iwoG0tC0coDvwHhvig8myCUZFSradKrSI0xUXXgG39cZX1XKHRPQo TzrmIWJRmYNc5Ogn933R6+JbRKTv4SL2VnFbeRAzVTVHdt3lzLsZLXIblEOKvKNL/wT8=; From: Daniel Mendler To: Stefan Monnier Subject: Re: bug#78162: [PATCH] hash-table-contains-p: Avoid creating a symbol on every call In-Reply-To: (Stefan Monnier's message of "Mon, 12 May 2025 17:20:54 -0400") References: <871ptage4w.fsf@daniel-mendler.de> <87tt65lvvk.fsf@protonmail.com> <87y0vhg35j.fsf@daniel-mendler.de> <878qngllhj.fsf@protonmail.com> <8734dbh46t.fsf@protonmail.com> Date: Tue, 13 May 2025 10:59:03 +0200 Message-ID: <87zffg7up4.fsf@daniel-mendler.de> User-Agent: Gnus/5.13 (Gnus v5.13) MIME-Version: 1.0 Content-Type: text/plain X-Spam-Score: -0.7 (/) X-Debbugs-Envelope-To: 78162-done Cc: Pip Cet , 78162-done@debbugs.gnu.org, Stefan Kangas X-BeenThere: debbugs-submit@debbugs.gnu.org X-Mailman-Version: 2.1.18 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: debbugs-submit-bounces@debbugs.gnu.org Sender: "Debbugs-submit" X-Spam-Score: -1.7 (-) Stefan Monnier writes: >>> Then an alternative would be the simpler patch below? >> I tried hard to find something wrong with it, but couldn't. > > Push to `master`, closing, Thanks. I've ported back the function to Compat on the emacs-31 branch. > Stefan From unknown Sun Jun 22 17:12:26 2025 Received: (at fakecontrol) by fakecontrolmessage; To: internal_control@debbugs.gnu.org From: Debbugs Internal Request Subject: Internal Control Message-Id: bug archived. Date: Tue, 10 Jun 2025 11:24:14 +0000 User-Agent: Fakemail v42.6.9 # This is a fake control message. # # The action: # bug archived. thanks # This fakemail brought to you by your local debbugs # administrator