From mboxrd@z Thu Jan 1 00:00:00 1970 Path: news.gmane.io!.POSTED.ciao.gmane.io!not-for-mail From: Andy Wingo Newsgroups: gmane.lisp.guile.devel Subject: Re: CPU and GC cost of bignums Date: Thu, 06 Feb 2020 10:37:54 +0100 Message-ID: <87y2tgawel.fsf@igalia.com> References: <87imkmwass.fsf@gnu.org> <87zhdxknf9.fsf@gnu.org> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8 Content-Transfer-Encoding: quoted-printable Injection-Info: ciao.gmane.io; posting-host="ciao.gmane.io:159.69.161.202"; logging-data="60160"; mail-complaints-to="usenet@ciao.gmane.io" User-Agent: Gnus/5.13 (Gnus v5.13) Emacs/26.3 (gnu/linux) Cc: Guile Devel To: Ludovic =?utf-8?Q?Court=C3=A8s?= Original-X-From: guile-devel-bounces+guile-devel=m.gmane-mx.org@gnu.org Thu Feb 06 10:38:51 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 1izdcZ-000FZM-NE for guile-devel@m.gmane-mx.org; Thu, 06 Feb 2020 10:38:51 +0100 Original-Received: from localhost ([::1]:34506 helo=lists1p.gnu.org) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izdcY-0000Pi-Pn for guile-devel@m.gmane-mx.org; Thu, 06 Feb 2020 04:38:50 -0500 Original-Received: from eggs.gnu.org ([2001:470:142:3::10]:44721) by lists.gnu.org with esmtp (Exim 4.90_1) (envelope-from ) id 1izdcG-0000PK-LM for guile-devel@gnu.org; Thu, 06 Feb 2020 04:38:34 -0500 Original-Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1izdcD-0002Lr-VD for guile-devel@gnu.org; Thu, 06 Feb 2020 04:38:31 -0500 Original-Received: from fanzine.igalia.com ([178.60.130.6]:39699) by eggs.gnu.org with esmtps (TLS1.0:RSA_AES_128_CBC_SHA1:16) (Exim 4.71) (envelope-from ) id 1izdcD-000190-BE; Thu, 06 Feb 2020 04:38:29 -0500 DKIM-Signature: v=1; a=rsa-sha256; q=dns/txt; c=relaxed/relaxed; d=igalia.com; s=20170329; h=Content-Transfer-Encoding:Content-Type:MIME-Version:Message-ID:In-Reply-To:Date:References:Subject:Cc:To:From; bh=TSivUPUFThfR/CH0ODGtgxzhD6LBRAO7fi+wgYn1zhA=; b=knjz2STT55ov+M+00BA5GSjnyDTBDQHcP5VwOJNQy6TH8//JHf1rL6xgp4oLKk3UMBXu8CJZ3P8TOdYO0sCUexOfLqUsoqBY6XeCz2NOaYLHpSmQNbqlg2pLVRkKbaMNkf2lPBgrOPYk1ORQdlx4b0RXrzqWIxcqnPqsLVInkqPdxgllQ9pxrKOZWo4pVembEsd1CzIR9B/lanfGoi1EIKLsAaAYbfwO48fqY8p8xXcSRupF6t205bV7smDHa12RrkZ3bFjOmYSgIzJSRkRiqh88DGzd5mURMPxIsKpOtpmD6tcWqjpSqy+SQeQ18dR0RwUy7gEBa08FJEW0X0PGUg==; Original-Received: from lfbn-ann-1-376-195.w86-200.abo.wanadoo.fr ([86.200.91.195] helo=milano) by fanzine.igalia.com with esmtpsa (Cipher TLS1.2:ECDHE_RSA_AES_256_GCM_SHA384:256) (Exim) id 1izdbq-0005ri-Gp; Thu, 06 Feb 2020 10:38:06 +0100 In-Reply-To: <87zhdxknf9.fsf@gnu.org> ("Ludovic \=\?utf-8\?Q\?Court\=C3\=A8s\=22'\?\= \=\?utf-8\?Q\?s\?\= message of "Wed, 05 Feb 2020 17:29:30 +0100") X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x (no timestamps) [generic] [fuzzy] X-Received-From: 178.60.130.6 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:20394 Archived-At: Hi :) Nice investigation! Perhaps slot-allocation should track live variables using something that's not bigints, but who knows. On Wed 05 Feb 2020 17:29, Ludovic Court=C3=A8s writes: > /* 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"); > } >=20=20 > 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"); > } >=20=20 > 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. */ > } I think this makes sense to me as a short-term fix. The down-side is that limbs can alias Scheme objects. In the long-term I think we should be representing bignums as pointerless objects whose first word is the tag and a word count, followed by inline "limbs" (in the sense of https://gmplib.org/manual/Nomenclature-and-Types.html#Nomenclature-and-Type= s). Generally we can use the low-level API to work on these (https://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Funct= ions), and if we need to use mpz_t, we can easily create an mpz_t that points to these values. Cheers, Andy