diff options
author | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2014-01-29 01:08:00 +0000 |
---|---|---|
committer | vlefevre <vlefevre@280ebfd0-de03-0410-8827-d642c229c3f4> | 2014-01-29 01:08:00 +0000 |
commit | 81df27a996db09ce9b13c0df2065af52fd0299a0 (patch) | |
tree | 77dd7c52f7cd883149d1c7ee628c543b5027cd0e /src/free_cache.c | |
parent | ff761b5aa17149acae76b0be9b756ce07310bffa (diff) | |
download | mpfr-81df27a996db09ce9b13c0df2065af52fd0299a0.tar.gz |
[src] mpz_t caching (modified patch by Patrick PĂ©lissier).
git-svn-id: svn://scm.gforge.inria.fr/svn/mpfr/trunk@8911 280ebfd0-de03-0410-8827-d642c229c3f4
Diffstat (limited to 'src/free_cache.c')
-rw-r--r-- | src/free_cache.c | 69 |
1 files changed, 53 insertions, 16 deletions
diff --git a/src/free_cache.c b/src/free_cache.c index e6f1a039b..84413bb11 100644 --- a/src/free_cache.c +++ b/src/free_cache.c @@ -22,28 +22,66 @@ http://www.gnu.org/licenses/ or write to the Free Software Foundation, Inc., #include "mpfr-impl.h" -#if 0 -static void -free_l2b (void) +/* Default value for the cache of mpz_t */ +#ifndef MPFR_MY_MPZ_INIT +# define MPFR_MY_MPZ_INIT 32 +#endif + +/* If the number of value to cache is not zero */ +#if MPFR_MY_MPZ_INIT + +/* Index in the stack table of mpz_t and stack table of mpz_t */ +static MPFR_THREAD_ATTR int n_alloc = 0; +static MPFR_THREAD_ATTR __mpz_struct mpz_tab[MPFR_MY_MPZ_INIT]; + +MPFR_HOT_FUNCTION_ATTR void +mpfr_mpz_init (mpz_t z) +{ + if (MPFR_LIKELY (n_alloc > 0)) + { + /* Get a mpz_t from the MPFR stack of previously used mpz_t. + It reduces memory pressure, and it allows to reuse + a mpz_t which should be sufficiently big. */ + MPFR_ASSERTD (n_alloc <= numberof (mpz_tab)); + memcpy (z, &mpz_tab[--n_alloc], sizeof (mpz_t)); + SIZ(z) = 0; + } + else + { + /* Call real GMP function */ + (__gmpz_init)(z); + } +} + +MPFR_HOT_FUNCTION_ATTR void +mpfr_mpz_clear (mpz_t z) { - int i, b; - - for (b = 2; b <= BASE_MAX; b++) - for (i = 0; i < 2; i++) - { - mpfr_ptr p = __gmpfr_l2b[b-2][i]; - if (p != NULL) - { - mpfr_clear (p); - (*__gmp_free_func) (p, sizeof (mpfr_t)); - } - } + if (MPFR_LIKELY (n_alloc < numberof (mpz_tab))) + { + /* Push back the mpz_t inside the stack of the used mpz_t */ + MPFR_ASSERTD (n_alloc >= 0); + memcpy (&mpz_tab[n_alloc++], z, sizeof (mpz_t)); + } + else + { + /* Call real GMP function */ + (__gmpz_clear)(z); + } } + #endif void mpfr_free_cache (void) { +#if MPFR_MY_MPZ_INIT + int i; + MPFR_ASSERTD (n_alloc >= 0 && n_alloc <= numberof (mpz_tab)); + for (i = 0; i < n_alloc; i++) + (__gmpz_clear)(&mpz_tab[i]); + n_alloc = 0; +#endif + #ifndef MPFR_USE_LOGGING mpfr_clear_cache (__gmpfr_cache_const_pi); mpfr_clear_cache (__gmpfr_cache_const_log2); @@ -55,5 +93,4 @@ mpfr_free_cache (void) #endif mpfr_clear_cache (__gmpfr_cache_const_euler); mpfr_clear_cache (__gmpfr_cache_const_catalan); - /* free_l2b (); */ } |