From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: =?utf-8?Q?Ludovic_Court=C3=A8s?= Newsgroups: gmane.lisp.guile.devel Subject: Re: CPU and GC cost of bignums Date: Wed, 05 Feb 2020 17:29:30 +0100 Message-ID: <87zhdxknf9.fsf@gnu.org> References: <87imkmwass.fsf@gnu.org> Mime-Version: 1.0 Content-Type: multipart/mixed; boundary="=-=-=" Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="79606"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cc: Andy Wingo To: Guile Devel Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Wed Feb 05 17:29:50 2020 Return-path: Envelope-to: guile-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 1izNYk-000Kal-GX for guile-devel@m.gmane-mx.org; Wed, 05 Feb 2020 17:29:50 +0100 Original-Received: from localhost ([::1]:52354 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izNYj-00072K-IU for guile-devel@m.gmane-mx.org; Wed, 05 Feb 2020 11:29:49 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:34122) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izNYX-0006yp-7r for guile-devel@gnu.org; Wed, 05 Feb 2020 11:29:38 -0500 Original-Received: from fencepost.gnu.org ([2001:470:142:3::e]:59246) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1izNYW-00089O-Qx; Wed, 05 Feb 2020 11:29:36 -0500 Original-Received: from [2a01:e0a:1d:7270:af76:b9b:ca24:c465] (port=38796 helo=ribbon) by fencepost.gnu.org with esmtpsa (TLS1.2:RSA_AES_256_CBC_SHA1:256) (Exim 4.82) (envelope-from ) id 1izNYS-0006Sf-HV; Wed, 05 Feb 2020 11:29:33 -0500 X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 17 =?utf-8?Q?Pluvi=C3=B4se?= an 228 de la =?utf-8?Q?R=C3=A9volution?= X-PGP-Key-ID: 0x090B11993D9AEBB5 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 3CE4 6455 8A84 FDC6 9DB4 0CFB 090B 1199 3D9A EBB5 X-OS: x86_64-pc-linux-gnu In-Reply-To: <87imkmwass.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Tue, 04 Feb 2020 17:56:51 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.23 Precedence: list List-Id: "Developers list for Guile, the GNU extensibility library" List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Errors-To: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Original-Sender: "guile-devel" Xref: news.gmane.io gmane.lisp.guile.devel:20389 Archived-At: --=-=-= Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Hey ho! Ludovic Court=C3=A8s skribis: > =E2=80=A6 but has the disadvantage that it doesn=E2=80=99t work: =E2=80= =98numbers.test=E2=80=99 fails > badly on bignums. I think with the excitement I no longer knew what I was saying. So, here=E2=80=99s a revised patch that actually preserves memory management (a= s in: =E2=80=98mpz_t=E2=80=99 are eventually freed), while getting rid of finaliz= ers. Result: --8<---------------cut here---------------start------------->8--- scheme@(guile-user)> ,use(system base optimize) scheme@(guile-user)> ,use(statprof) scheme@(guile-user)> (gcprof (lambda () (compile-file "gnu/services/mail.sc= m" #:opts (optimizations-for-level 1)))) % cumulative self=20=20=20=20=20=20=20=20=20=20=20=20=20 time seconds seconds procedure 25.00 3.25 3.25 anon #x66e8e0 9.38 2.44 1.22 language/cps/intset.scm:551:2:union 6.25 0.81 0.81 ice-9/boot-9.scm:2201:0:%load-announce 6.25 0.81 0.81 anon #x66e468 3.13 139.83 0.41 ice-9/threads.scm:388:4 3.13 73.57 0.41 language/tree-il/peval.scm:710:2:loop 3.13 4.47 0.41 system/vm/assembler.scm:1201:0:intern-constant 3.13 1.63 0.41 ice-9/psyntax.scm:2964:6:match* 3.13 1.22 0.41 language/cps/intset.scm:270:2:adjoin 3.13 0.81 0.41 language/cps/intmap.scm:184:0:intmap-add! 3.13 0.41 0.41 language/cps/slot-allocation.scm:843:19 [...] Sample count: 32 Total time: 13.007326523 seconds (4.420918875 seconds in GC) --8<---------------cut here---------------end--------------->8--- I=E2=80=99d like to go ahead with this patch if there are no objections! Ludo=E2=80=99. --=-=-= Content-Type: text/x-patch Content-Disposition: inline diff --git a/libguile/numbers.c b/libguile/numbers.c index d1b463358..8606780a8 100644 --- a/libguile/numbers.c +++ b/libguile/numbers.c @@ -1,4 +1,4 @@ -/* Copyright 1995-2016,2018-2019 +/* Copyright 1995-2016,2018-2020 Free Software Foundation, Inc. Portions Copyright 1990-1993 by AT&T Bell Laboratories and Bellcore. @@ -218,16 +218,6 @@ static mpz_t z_negative_one; -/* Clear the `mpz_t' embedded in bignum PTR. */ -static void -finalize_bignum (void *ptr, void *data) -{ - SCM bignum; - - bignum = SCM_PACK_POINTER (ptr); - mpz_clear (SCM_I_BIG_MPZ (bignum)); -} - /* The next three functions (custom_libgmp_*) are passed to mp_set_memory_functions (in GMP) so that memory used by the digits themselves is known to the garbage collector. This is needed so @@ -237,19 +227,20 @@ finalize_bignum (void *ptr, void *data) static void * custom_gmp_malloc (size_t alloc_size) { - return scm_malloc (alloc_size); + return scm_gc_malloc (alloc_size, "GMP"); } static void * custom_gmp_realloc (void *old_ptr, size_t old_size, size_t new_size) { - return scm_realloc (old_ptr, new_size); + return scm_gc_realloc (old_ptr, old_size, new_size, "GMP"); } static void custom_gmp_free (void *ptr, size_t size) { - free (ptr); + /* Do nothing: all memory allocated by GMP is under GC control and + will be freed when needed. */ } @@ -260,12 +251,10 @@ make_bignum (void) scm_t_bits *p; /* Allocate one word for the type tag and enough room for an `mpz_t'. */ - p = scm_gc_malloc_pointerless (sizeof (scm_t_bits) + sizeof (mpz_t), - "bignum"); + p = scm_gc_malloc (sizeof (scm_t_bits) + sizeof (mpz_t), + "bignum"); p[0] = scm_tc16_big; - scm_i_set_finalizer (p, finalize_bignum, NULL); - return SCM_PACK (p); } --=-=-=--