From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.blaine.gmane.org!not-for-mail From: Pip Cet Newsgroups: gmane.emacs.devel Subject: Re: MPS: weak hash tables Date: Tue, 02 Jul 2024 09:15:44 +0000 Message-ID: References: <2syUQ04IbTWqDJjMfKSrtzWMWmFGq1GIOwSxv_r6BEyNDtk7ADADKjZk-90g9tSS9SKWppkiq6_zihUtsoE1spiopaOI6-v9inQrGxwMyCs=@protonmail.com> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="blaine.gmane.org:116.202.254.214"; logging-data="21682"; mail-complaints-to="usenet@ciao.gmane.io" Cc: Eli Zaretskii , Helmut Eller , Emacs Devel To: =?utf-8?Q?Gerd_M=C3=B6llmann?= Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Tue Jul 02 13:07:31 2024 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 1sObMA-0005Qw-S1 for ged-emacs-devel@m.gmane-mx.org; Tue, 02 Jul 2024 13:07:30 +0200 Original-Received: from localhost ([::1] helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1sObLS-0004rP-Io; Tue, 02 Jul 2024 07:06:46 -0400 Original-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 1sOZcG-00009g-A8 for emacs-devel@gnu.org; Tue, 02 Jul 2024 05:16:00 -0400 Original-Received: from mail-4316.protonmail.ch ([185.70.43.16]) by eggs.gnu.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.90_1) (envelope-from ) id 1sOZc9-0004z8-KS for emacs-devel@gnu.org; Tue, 02 Jul 2024 05:15:58 -0400 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=protonmail.com; s=protonmail3; t=1719911748; x=1720170948; bh=5SUcOiQaLcl2CdQm62IdytHcxN/RdbS8vSY7e1uH/XY=; 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; b=Vk7rkyt/T8ylO0zHcihj+5f2cOcPbUih9WaN4Xl8D/3rIMCtXe1GAZoqPlhUd5NOk vWgrDBfe80hB7XTpKh144h6n9aawPstvghFfsLJr+Sb9rzm7UAc7xuj8y+Dq6Uiv9X R3I59v5q0bHFr8A5Sv62RgxMFsW48GmeLAERcK3MHdh0weeFt+ZOEgfHUKIaqNrVaq MUQKDa/h0sdsXFzPuia4O/NS++Y9KgpasQVBn9Icz/MtsNe6MToMc5a1hR8a9BKm+i bGi9ZWLh6lXQxCftH//4nW9y8ILr/sJ7E1i0bS6TKaxqziUogXgPAxMY0IImOKtGCh D2g4OKHcN7q6A== In-Reply-To: Feedback-ID: 112775352:user:proton X-Pm-Message-ID: c1a80e0514de40dd85804e83494218b416a51b31 Received-SPF: pass client-ip=185.70.43.16; envelope-from=pipcet@protonmail.com; helo=mail-4316.protonmail.ch X-Spam_score_int: -20 X-Spam_score: -2.1 X-Spam_bar: -- X-Spam_report: (-2.1 / 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_DNSWL_NONE=-0.0001, RCVD_IN_MSPIKE_H4=0.001, RCVD_IN_MSPIKE_WL=0.001, SPF_HELO_PASS=-0.001, SPF_PASS=-0.001 autolearn=unavailable autolearn_force=no X-Spam_action: no action X-Mailman-Approved-At: Tue, 02 Jul 2024 07:06:44 -0400 X-BeenThere: emacs-devel@gnu.org X-Mailman-Version: 2.1.29 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-bounces+ged-emacs-devel=m.gmane-mx.org@gnu.org Xref: news.gmane.io gmane.emacs.devel:321119 Archived-At: On Tuesday, July 2nd, 2024 at 06:55, Gerd M=C3=B6llmann wrote: > Pip Cet pipcet@protonmail.com writes: > > On Tuesday, July 2nd, 2024 at 05:47, Gerd M=C3=B6llmann gerd.moellmann@= gmail.com wrote: > >=20 > > > Pip Cet pipcet@protonmail.com writes: > > >=20 > > > > Also, any opinions on :weakness 'key-or-value? Can we just not supp= ort it and use strong hash tables instead? > > >=20 > > > So, after some more reading, I think I have an opinion. Let me read w= hat > > > it is :-) > > >=20 > > > The design supports 1 vector of weak references, and 1 vector of > > > strong references. So either keys or values can be weak but not both. > >=20 > > Why not? I may be missing something obvious, but the scan method is > > allowed to modify the scanned object, right? We're doing that in the > > weak-key case anyway. >=20 > I may be missing something as well, as usual :-). And yes, we have > exclusive access to the object while it is being scanned. So this is what I think the current branch is doing. Please tell me if I'm = totally missing something. * weak-key and weak-value tables are allocated as you describe * if weakness is Key_And_Value (and I'm not sure that's correct, but it mat= ches the source code comment), the strong part contains the hash values, ne= xt table, and index table, and the weak part contains both the keys and the= values, in a single array of length 2 * size. Rather than adjusting the st= ride and keeping things in a single cache line in this (slow, anyway) case,= we store all keys in weak->entries[0..table_size], and all values in weak-= >entries[table_size..2*table_size]. * when MPS splats a key, we (not MPS) splat the value before it is scanned * when MPS splats a value, we splat the key after it has been scanned=20 * it appears to work: (setq table (make-hash-table :weakness 'key-and-value)) (puthash t (cons 1 2) table) (puthash (cons 3 4) t table) (puthash (cons 5 6) (cons 7 8) table) (hash-table-count table) ;; =3D> 3 (igc--collect) (hash-table-count table) ;; =3D> 1 (setq values nil) (igc--collect) (hash-table-count table) ;; =3D> 0 What confused me was the "after it has been scanned" part. I believe that m= eans we keep the objects alive until we scan again, but as splatting isn't = perfectly eager anyway, that should rarely become a problem. Maybe we need = to call igc--collect twice for a full full collection. > > So what I'm currently doing is creating a single vector containing all > > keys, then all values, for key-and-value hash tables. If the key gets > > splatted, the value gets splatted right away and everything works as > > it should. If the value gets splatted, we've already decided to keep > > alive the key, but that's okay as it's only a weak reference. >=20 >=20 > My understanding so far is that the strong part is always allocated with > rank strong, right? The strong part contains one array of references, > the entries, which are therefore also strong. The strong part contains no references in the key-and-value case, just fixn= ums. > The pointers to the > entries for key/value are set up to refer to the entries arrays of > either the strong part or the weak part, as needed. So my understanding > is that one of them is always strong and other weak. The W predicate can > then only be implemented in one of them. No, they can both be weak. > > > In Emacs itself, I see from git grep that :weakness key, value, and t > > > are being used, where t means key-and-value. Haven't checked for whic= h > > > use cases t is used, though. > >=20 > > What surprised me is that there are plenty of hash tables that are > > both weak and use equal as a predicate. That doesn't make much sense > > to me... >=20 > Hm, maybe they are value weak? Key-weak would make no sense, I think, > indeed. emacs-lisp/macroexp.el:(defvar macroexp--warned (make-hash-table :test #'eq= ual :weakness 'key)) emacs-lisp/map-ynp.el:(defconst read-answer-map--memoize (make-hash-table := weakness 'key :test 'equal)) emacs-lisp/pcase.el:;; (defconst pcase--memoize-2 (make-hash-table :weaknes= s 'key :test 'equal)) org/org-macs.el:(defvar org-sxhash-hashes (make-hash-table :weakness 'key := test 'equal)) are key-weak... > > > Anyway, we're better off than before :-). > >=20 > > On 64-bit systems. 32-bit systems are still broken, I'm afraid, and > > while my patch fixes the crash I've seen on i386 Debian, Eli's crash > > looked very different and may not be fixed by it. >=20 > From my POV, it's not a deal breaker if 32-bit systems don't work with > igc, They still have the old GC, and 32-bit systems are kind of obsolete > even Intel says and so on. It would be nice if we could that working of > course. My concern is that Ravenbrook may get around to implementing emulation for = aarch64 or amd64. > > Also, there's the whole caution thing about weak objects containing > > only unaligned words or words pointing directly to a base object, > > which is only relevant on Unix/i386, IIRC. (MPS emulates instructions > > to simulate fine-grained barriers, which is a really cool idea; I'd > > still like an option to turn it off though...). That would mean we > > have to replace Lisp_Objects and use the ptr member of our union (and > > that's the reason I'm using fixnums rather than plain integers for the > > hash). >=20 > Yeah, that's another point I forgot about. The trickery MPS does on > Windows and Linux with 32-bits. The consequences of which are another > very good reason to declare 32-bit systems as not supproted, from my > POV. Oh, that's on Windows, too? I believe that might mean Eli might run into we= ird bugs... Thanks again, and thanks for the asserts. Pip