unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: Guile Devel <guile-devel@gnu.org>
Cc: Andy Wingo <wingo@igalia.com>
Subject: Re: CPU and GC cost of bignums
Date: Wed, 05 Feb 2020 17:29:30 +0100	[thread overview]
Message-ID: <87zhdxknf9.fsf@gnu.org> (raw)
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")

[-- Attachment #1: Type: text/plain, Size: 1656 bytes --]

Hey ho!

Ludovic Courtès <ludo@gnu.org> skribis:

> … but has the disadvantage that it doesn’t work: ‘numbers.test’ fails
> badly on bignums.

I think with the excitement I no longer knew what I was saying.  So,
here’s a revised patch that actually preserves memory management (as in:
‘mpz_t’ are eventually freed), while getting rid of finalizers.  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.scm" #:opts (optimizations-for-level 1))))
%     cumulative   self             
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’d like to go ahead with this patch if there are no objections!

Ludo’.


[-- Warning: decoded text below may be mangled, UTF-8 assumed --]
[-- Attachment #2: Type: text/x-patch, Size: 1813 bytes --]

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;
 
 \f
 
-/* 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);
 }
 

  reply	other threads:[~2020-02-05 16:29 UTC|newest]

Thread overview: 6+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2020-02-04 16:56 CPU and GC cost of bignums Ludovic Courtès
2020-02-05 16:29 ` Ludovic Courtès [this message]
2020-02-05 21:28   ` Hans Åberg
2020-02-06  9:37   ` Andy Wingo
2020-02-06 13:37     ` Ludovic Courtès
2020-02-08 14:05       ` Ludovic Courtès

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

  List information: https://www.gnu.org/software/guile/

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=87zhdxknf9.fsf@gnu.org \
    --to=ludo@gnu.org \
    --cc=guile-devel@gnu.org \
    --cc=wingo@igalia.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).