From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.org!not-for-mail From: ludo@gnu.org (Ludovic =?iso-8859-1?Q?Court=E8s?=) Newsgroups: gmane.lisp.guile.devel Subject: Re: [PATCH] Configure GMP to use GC allocation functions, remove bignum finalizers Date: Tue, 31 May 2011 23:52:56 +0200 Message-ID: <87k4d6edvr.fsf@gnu.org> References: <87tycaodlk.fsf@netris.org> NNTP-Posting-Host: lo.gmane.org Mime-Version: 1.0 Content-Type: text/plain X-Trace: dough.gmane.org 1306882688 16423 80.91.229.12 (31 May 2011 22:58:08 GMT) X-Complaints-To: usenet@dough.gmane.org NNTP-Posting-Date: Tue, 31 May 2011 22:58:08 +0000 (UTC) To: guile-devel@gnu.org Original-X-From: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Wed Jun 01 00:58:04 2011 Return-path: Envelope-to: guile-devel@m.gmane.org Original-Received: from lists.gnu.org ([140.186.70.17]) by lo.gmane.org with esmtp (Exim 4.69) (envelope-from ) id 1QRXst-0006ql-Cz for guile-devel@m.gmane.org; Wed, 01 Jun 2011 00:57:59 +0200 Original-Received: from localhost ([::1]:41656 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRXss-0001Pu-Fy for guile-devel@m.gmane.org; Tue, 31 May 2011 18:57:58 -0400 Original-Received: from eggs.gnu.org ([140.186.70.92]:56989) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRWsB-0007oM-VX for guile-devel@gnu.org; Tue, 31 May 2011 17:53:13 -0400 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1QRWsA-0004Sw-BT for guile-devel@gnu.org; Tue, 31 May 2011 17:53:11 -0400 Original-Received: from lo.gmane.org ([80.91.229.12]:39565) by eggs.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1QRWs9-0004Ss-R2 for guile-devel@gnu.org; Tue, 31 May 2011 17:53:10 -0400 Original-Received: from list by lo.gmane.org with local (Exim 4.69) (envelope-from ) id 1QRWs7-0006e4-Ts for guile-devel@gnu.org; Tue, 31 May 2011 23:53:07 +0200 Original-Received: from reverse-83.fdn.fr ([80.67.176.83]) by main.gmane.org with esmtp (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 May 2011 23:53:07 +0200 Original-Received: from ludo by reverse-83.fdn.fr with local (Gmexim 0.1 (Debian)) id 1AlnuQ-0007hv-00 for ; Tue, 31 May 2011 23:53:07 +0200 X-Injected-Via-Gmane: http://gmane.org/ Original-Lines: 56 Original-X-Complaints-To: usenet@dough.gmane.org X-Gmane-NNTP-Posting-Host: reverse-83.fdn.fr X-URL: http://www.fdn.fr/~lcourtes/ X-Revolutionary-Date: 12 Prairial an 219 de la =?iso-8859-1?Q?R=E9volution?= X-PGP-Key-ID: 0xEA52ECF4 X-PGP-Key: http://www.fdn.fr/~lcourtes/ludovic.asc X-PGP-Fingerprint: 83C4 F8E5 10A3 3B4C 5BEA D15D 77DD 95E2 EA52 ECF4 X-OS: x86_64-unknown-linux-gnu User-Agent: Gnus/5.110017 (No Gnus v0.17) Emacs/24.0.50 (gnu/linux) Cancel-Lock: sha1:XO6MJ0m/ygnUfApP4Z5WcOEkfic= X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.6 (newer, 3) X-Received-From: 80.91.229.12 X-BeenThere: guile-devel@gnu.org X-Mailman-Version: 2.1.14 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.org@gnu.org Original-Sender: guile-devel-bounces+guile-devel=m.gmane.org@gnu.org Xref: news.gmane.org gmane.lisp.guile.devel:12556 Archived-At: Hi! Mark H Weaver skribas: > This patch changes the way that bignums are allocated, so that digits > are now allocated using the GC allocation functions instead of standard > malloc/realloc/free. > > Without this patch, computing (factorial 100000), where factorial is > implemented using the straightforward iterative approach below, causes > Guile to allocate a huge amount of memory. This happens because the > majority of the memory used by large bignums (the digits themselves) is > not allocated by the GC, and so the GC doesn't run until a huge amount > of memory has been allocated. > > (define (factorial n) > (define (fac n acc) > (if (<= n 1) > accum > (fac (1- n) (* n acc)))) > (fac n 1)) Ouch! Thanks for tracking this down. > With this patch, (factorial 100000) takes a long time but memory usage > is modest. OK, interesting. Reminds me of the malloc hook trick: . It may still be worth considering for libguile, no? > The main reason I haven't already pushed this patch is that there is a > slight complication: when you register custom allocation functions for > use by GMP, they get used for _all_ allocation, not just for digits. In > particular, they get used to allocate the block returned by mpz_get_str > (when the first argument is NULL). This means that if the user of > mpz_get_str uses the standard "free" to deallocate this block, it will > fail badly. This could potentially affect programs which use libguile > but also use the GMP functions directly. Yes, that's a problem, probably even be a showstopper for 2.0. :-( What do others think? > +static void > +custom_gmp_free (void *ptr, size_t size) > +{ > + scm_gc_free (ptr, size, "bignum-digits"); > } May be safer to do nothing here (see .) Thanks, Ludo'.