From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!.POSTED.blaine.gmane.org!not-for-mail From: Paul Eggert Newsgroups: gmane.emacs.devel Subject: Re: Changes in GC and in pure space (was: [Emacs-diffs] master 5d4dd55: Fix lifetime error in previous patch) Date: Wed, 21 Aug 2019 19:06:20 -0700 Organization: UCLA Computer Science Department Message-ID: <397faace-c84b-78e8-1b13-69a10471ffec@cs.ucla.edu> References: <20190721193221.1964.53182@vcs0.savannah.gnu.org> <20190721193222.8C19E20BE2@vcs0.savannah.gnu.org> <83blxmqfkq.fsf@gnu.org> <9568ca7d-854f-1971-bbe8-03ba8c64af42@cs.ucla.edu> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="------------EC8452AB897E28DDF0D8375D" Injection-Info: blaine.gmane.org; posting-host="blaine.gmane.org:195.159.176.226"; logging-data="62785"; mail-complaints-to="usenet@blaine.gmane.org" User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:60.0) Gecko/20100101 Thunderbird/60.8.0 Cc: Eli Zaretskii , Daniel Colascione , emacs-devel@gnu.org To: Pip Cet Original-X-From: emacs-devel-bounces+ged-emacs-devel=m.gmane.org@gnu.org Thu Aug 22 04:06:37 2019 Return-path: Envelope-to: ged-emacs-devel@m.gmane.org Original-Received: from lists.gnu.org ([209.51.188.17]) by blaine.gmane.org with esmtps (TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim 4.89) (envelope-from ) id 1i0cUn-000GEI-46 for ged-emacs-devel@m.gmane.org; Thu, 22 Aug 2019 04:06:37 +0200 Original-Received: from localhost ([::1]:37678 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0cUm-0000cP-19 for ged-emacs-devel@m.gmane.org; Wed, 21 Aug 2019 22:06:36 -0400 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:37654) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1i0cUf-0000c7-4E for emacs-devel@gnu.org; Wed, 21 Aug 2019 22:06:30 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1i0cUd-0006rT-Cx for emacs-devel@gnu.org; Wed, 21 Aug 2019 22:06:28 -0400 Original-Received: from zimbra.cs.ucla.edu ([131.179.128.68]:47810) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1i0cUZ-0006q0-SJ; Wed, 21 Aug 2019 22:06:24 -0400 Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id 26668160267; Wed, 21 Aug 2019 19:06:22 -0700 (PDT) Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10032) with ESMTP id aZVo6ECvu8O7; Wed, 21 Aug 2019 19:06:21 -0700 (PDT) Original-Received: from localhost (localhost [127.0.0.1]) by zimbra.cs.ucla.edu (Postfix) with ESMTP id F2A1F162715; Wed, 21 Aug 2019 19:06:20 -0700 (PDT) X-Virus-Scanned: amavisd-new at zimbra.cs.ucla.edu Original-Received: from zimbra.cs.ucla.edu ([127.0.0.1]) by localhost (zimbra.cs.ucla.edu [127.0.0.1]) (amavisd-new, port 10026) with ESMTP id uE0WIFMUNNTp; Wed, 21 Aug 2019 19:06:20 -0700 (PDT) Original-Received: from [192.168.1.9] (cpe-23-242-74-103.socal.res.rr.com [23.242.74.103]) by zimbra.cs.ucla.edu (Postfix) with ESMTPSA id AA67B160267; Wed, 21 Aug 2019 19:06:20 -0700 (PDT) In-Reply-To: Content-Language: en-US X-detected-operating-system: by eggs.gnu.org: GNU/Linux 3.x X-Received-From: 131.179.128.68 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.org@gnu.org Original-Sender: "Emacs-devel" Xref: news.gmane.org gmane.emacs.devel:239492 Archived-At: This is a multi-part message in MIME format. --------------EC8452AB897E28DDF0D8375D Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit Pip Cet wrote: > (To reproduce the promised segfault, add > > (defvar custom-dummy (make-hash-table :test 'eq)) > (puthash custom-dummy custom-dummy custom-dummy) > > to custom.el, then rebuild and run (clrhash custom-dummy) in Emacs -Q. > Trivial to fix, but apparently not obvious enough to have been caught > in the original changes). Thanks for reporting that. I installed the attached patch, which should fix that bug and clean up a few nearby glitches. This patch is a bit less trivial but should be a smidge faster than the trivial patch would have been. --------------EC8452AB897E28DDF0D8375D Content-Type: text/x-patch; name="0001-Fix-clrhash-bug-when-hash-table-needs-rehashing.patch" Content-Disposition: attachment; filename*0="0001-Fix-clrhash-bug-when-hash-table-needs-rehashing.patch" Content-Transfer-Encoding: quoted-printable >From ac71011bc1726bd767446407500c20cbbdca74a2 Mon Sep 17 00:00:00 2001 From: Paul Eggert Date: Wed, 21 Aug 2019 18:54:08 -0700 Subject: [PATCH] Fix clrhash bug when hash table needs rehashing MIME-Version: 1.0 Content-Type: text/plain; charset=3DUTF-8 Content-Transfer-Encoding: 8bit Problem reported by Pip Cet in: https://lists.gnu.org/r/emacs-devel/2019-08/msg00316.html * src/fns.c (maybe_resize_hash_table): Prefer ASET to gc_aset where either will do. Simplify appending of Qunbound values. Put index_size calculation closer to where it=E2=80=99s needed. (hash_clear): If hash_rehash_needed_p (h), don=E2=80=99t clear the nonexistent hash vector. Use memclear to speed up clearing. * src/lisp.h (HASH_TABLE_SIZE): Check that the size is positive, and tell that to the compiler. --- src/fns.c | 28 ++++++++++++---------------- src/lisp.h | 9 +++++---- 2 files changed, 17 insertions(+), 20 deletions(-) diff --git a/src/fns.c b/src/fns.c index b606d6299c..8ca0953fe8 100644 --- a/src/fns.c +++ b/src/fns.c @@ -4198,21 +4198,20 @@ maybe_resize_hash_table (struct Lisp_Hash_Table *= h) new_size); ptrdiff_t next_size =3D ASIZE (next); for (ptrdiff_t i =3D old_size; i < next_size - 1; i++) - gc_aset (next, i, make_fixnum (i + 1)); - gc_aset (next, next_size - 1, make_fixnum (-1)); - ptrdiff_t index_size =3D hash_index_size (h, next_size); + ASET (next, i, make_fixnum (i + 1)); + ASET (next, next_size - 1, make_fixnum (-1)); =20 /* Build the new&larger key_and_value vector, making sure the new fields are initialized to `unbound`. */ Lisp_Object key_and_value =3D larger_vecalloc (h->key_and_value, 2 * (next_size - old_size), 2 * next_size); - for (ptrdiff_t i =3D ASIZE (h->key_and_value); - i < ASIZE (key_and_value); i++) + for (ptrdiff_t i =3D 2 * old_size; i < 2 * next_size; i++) ASET (key_and_value, i, Qunbound); =20 Lisp_Object hash =3D larger_vector (h->hash, next_size - old_size, next_size); + ptrdiff_t index_size =3D hash_index_size (h, next_size); h->index =3D make_vector (index_size, make_fixnum (-1)); h->key_and_value =3D key_and_value; h->hash =3D hash; @@ -4404,17 +4403,14 @@ hash_clear (struct Lisp_Hash_Table *h) { if (h->count > 0) { - ptrdiff_t i, size =3D HASH_TABLE_SIZE (h); - - for (i =3D 0; i < size; ++i) - { - set_hash_next_slot (h, i, i < size - 1 ? i + 1 : -1); - set_hash_key_slot (h, i, Qunbound); - set_hash_value_slot (h, i, Qnil); - set_hash_hash_slot (h, i, Qnil); - } - - for (i =3D 0; i < ASIZE (h->index); ++i) + ptrdiff_t size =3D HASH_TABLE_SIZE (h); + if (!hash_rehash_needed_p (h)) + memclear (XVECTOR (h->hash)->contents, size * word_size); + memclear (XVECTOR (h->key_and_value)->contents, size * 2 * word_si= ze); + for (ptrdiff_t i =3D 0; i < size; i++) + set_hash_next_slot (h, i, i < size - 1 ? i + 1 : -1); + + for (ptrdiff_t i =3D 0; i < ASIZE (h->index); i++) ASET (h->index, i, make_fixnum (-1)); =20 h->next_free =3D 0; diff --git a/src/lisp.h b/src/lisp.h index 56ad99b8e3..ae5a81e7b5 100644 --- a/src/lisp.h +++ b/src/lisp.h @@ -2307,7 +2307,7 @@ #define DEFSYM(sym, name) /* empty */ weakness of the table. */ Lisp_Object weak; =20 - /* Vector of hash codes. + /* Vector of hash codes, or nil if the table needs rehashing. If the I-th entry is unused, then hash[I] should be nil. */ Lisp_Object hash; =20 @@ -2327,8 +2327,7 @@ #define DEFSYM(sym, name) /* empty */ 'index' are special and are either ignored by the GC or traced in a special way (e.g. because of weakness). */ =20 - /* Number of key/value entries in the table. This number is - negated if the table needs rehashing. */ + /* Number of key/value entries in the table. */ ptrdiff_t count; =20 /* Index of first free entry in free list, or -1 if none. */ @@ -2413,7 +2412,9 @@ HASH_HASH (const struct Lisp_Hash_Table *h, ptrdiff= _t idx) INLINE ptrdiff_t HASH_TABLE_SIZE (const struct Lisp_Hash_Table *h) { - return ASIZE (h->next); + ptrdiff_t size =3D ASIZE (h->next); + eassume (0 < size); + return size; } =20 void hash_table_rehash (struct Lisp_Hash_Table *h); --=20 2.17.1 --------------EC8452AB897E28DDF0D8375D--