unofficial mirror of guile-devel@gnu.org 
 help / color / mirror / Atom feed
From: "Ludovic Courtès" <ludo@gnu.org>
To: Andy Wingo <wingo@igalia.com>
Cc: Guile Devel <guile-devel@gnu.org>
Subject: Re: CPU and GC cost of bignums
Date: Thu, 06 Feb 2020 14:37:52 +0100	[thread overview]
Message-ID: <878slfalan.fsf@gnu.org> (raw)
In-Reply-To: <87y2tgawel.fsf@igalia.com> (Andy Wingo's message of "Thu, 06 Feb 2020 10:37:54 +0100")

Hi!

Andy Wingo <wingo@igalia.com> skribis:

> Nice investigation!  Perhaps slot-allocation should track live variables
> using something that's not bigints, but who knows.

Yeah I wondered; it’s not clear whether bitvectors would be more
efficient, for instance, although we could make it perhaps locally
imperative.

> On Wed 05 Feb 2020 17:29, Ludovic Courtès <ludo@gnu.org> 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");
>>  }
>>  
>>  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.  */
>>  }
>
> I think this makes sense to me as a short-term fix.  The down-side is
> that limbs can alias Scheme objects.

Yes.

To my surprise, on a pure bignum microbenchmark, this is
counterproductive:

--8<---------------cut here---------------start------------->8---
$ guile ~/src/guile-debugging/bignum-finalizers.scm  # 3.0.0
clock utime stime cutime cstime gctime
 2.42  6.20  0.17   0.00   0.00   5.62
heap size: 2.0 MiB
$ /data/src/guile-3.0/meta/guile  ~/src/guile-debugging/bignum-finalizers.scm
clock utime stime cutime cstime gctime
 3.97 10.91  0.15   0.00   0.00  10.60
heap size: 3.0 MiB
$ cat ~/src/guile-debugging/bignum-finalizers.scm
(use-modules (ice-9 time))

(time
 (let loop ((n (expt 2 18))
            (i 1))
   (unless (zero? n)
     ;; (display ".")
     (loop (- n 1)
           (logior 0 (ash i 1))))))

(format #t "heap size: ~a MiB~%"
        (round
         (/ (assoc-ref (gc-stats) 'heap-size) (expt 2. 20))))
--8<---------------cut here---------------end--------------->8---

(Here we’re creating ~24 bignums, no more.)
I wonder if there’s another part of the story that I’m missing here.

Perf report for 3.0.0:

--8<---------------cut here---------------start------------->8---
  46.93%  guile    libgc.so.1.3.6         [.] GC_mark_from
  17.61%  guile    libgc.so.1.3.6         [.] GC_header_cache_miss
   9.96%  guile    libgc.so.1.3.6         [.] GC_add_to_black_list_normal
   5.20%  guile    libgmp.so.10.3.2       [.] __gmpn_lshift_coreisbr
   4.13%  guile    libgc.so.1.3.6         [.] GC_find_header
   2.28%  guile    libgc.so.1.3.6         [.] GC_finalize
   2.09%  guile    libgc.so.1.3.6         [.] GC_base
--8<---------------cut here---------------end--------------->8---

With the patch:

--8<---------------cut here---------------start------------->8---
  48.40%  guile            libgc.so.1.3.6         [.] GC_mark_from
  17.74%  guile            libgc.so.1.3.6         [.] GC_header_cache_miss
  11.90%  guile            libgc.so.1.3.6         [.] GC_add_to_black_list_normal
   4.45%  guile            libgc.so.1.3.6         [.] GC_find_header
   2.31%  guile            libgmp.so.10.3.2       [.] __gmpn_lshift_coreisbr
   2.30%  guile            libgc.so.1.3.6         [.] GC_base
   1.73%  guile            libgc.so.1.3.6         [.] GC_finalize
--8<---------------cut here---------------end--------------->8---

IOW, the relative part of computations drops from 5% to 2%.

Thoughts?

> 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-Types).
> Generally we can use the low-level API to work on these
> (https://gmplib.org/manual/Low_002dlevel-Functions.html#Low_002dlevel-Functions),
> and if we need to use mpz_t, we can easily create an mpz_t that points
> to these values.

Yes, that sounds like the right approach longer-term.  Note that ‘mpz_t’
is exposed through “numbers.h”, which I guess means we cannot change
that in 3.0.x.

Thanks,
Ludo’.



  reply	other threads:[~2020-02-06 13:37 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
2020-02-05 21:28   ` Hans Åberg
2020-02-06  9:37   ` Andy Wingo
2020-02-06 13:37     ` Ludovic Courtès [this message]
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=878slfalan.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).