[commit: packages/integer-gmp] wip/T8647: Allocate initial 1-limb mpz_t on the Stack and introduce MPZ# type (12895ae)
git at git.haskell.org
git at git.haskell.org
Sun Jan 12 23:31:13 UTC 2014
Repository : ssh://git@git.haskell.org/integer-gmp
On branch : wip/T8647
Link : http://ghc.haskell.org/trac/ghc/changeset/12895aea1eeea62d6da1348c61445a7bf285c567/integer-gmp
>---------------------------------------------------------------
commit 12895aea1eeea62d6da1348c61445a7bf285c567
Author: Herbert Valerio Riedel <hvr at gnu.org>
Date: Thu Jan 9 00:19:31 2014 +0100
Allocate initial 1-limb mpz_t on the Stack and introduce MPZ# type
We now allocate a 1-limb mpz_t on the stack instead of doing a more
expensive heap-allocation (especially if the heap-allocated copy becomes
garbage right away); this addresses #8647.
In order to delay heap allocations of 1-limb `ByteArray#`s instead of
the previous `(# Int#, ByteArray# #)` pair, a 3-tuple
`(# Int#, ByteArray#, Word# #)` is returned now. This tuple is given the
type-synonym `MPZ#`.
This 3-tuple representation uses either the 1st and the 2nd element, or
the 1st and the 3rd element to represent the limb(s) (NB: undefined
`ByteArray#` elements must not be accessed as they don't point to a
proper `ByteArray#`, see also `DUMMY_BYTE_ARR`); more specifically, the
following encoding is used (where `⊥` means undefined/unused):
- (# 0#, ⊥, 0## #) -> value = 0
- (# 1#, ⊥, w #) -> value = w
- (# -1#, ⊥, w #) -> value = -w
- (# s#, d, 0## #) -> value = J# s d
The `mpzToInteger` helper takes care of converting `MPZ#` into an
`Integer`, and allocating a 1-limb `ByteArray#` in case the
value (`w`/`-w`) doesn't fit the `S# Int#` representation).
The following nofib benchmarks benefit from this optimization:
Program Size Allocs Runtime Elapsed TotalMem
------------------------------------------------------------------
bernouilli +0.2% -5.2% 0.12 0.12 +0.0%
gamteb +0.2% -1.7% 0.03 0.03 +0.0%
kahan +0.3% -13.2% 0.17 0.17 +0.0%
mandel +0.2% -24.6% 0.04 0.04 +0.0%
power +0.2% -2.6% -2.0% -2.0% -8.3%
primetest +0.1% -17.3% 0.06 0.06 +0.0%
rsa +0.2% -18.5% 0.02 0.02 +0.0%
scs +0.1% -2.9% -0.1% -0.1% +0.0%
sphere +0.3% -0.8% 0.03 0.03 +0.0%
symalg +0.2% -3.1% 0.01 0.01 +0.0%
------------------------------------------------------------------
Min +0.1% -24.6% -4.6% -4.6% -8.3%
Max +0.3% +0.0% +5.9% +5.9% +4.5%
Geometric Mean +0.2% -1.0% +0.2% +0.2% -0.0%
Signed-off-by: Herbert Valerio Riedel <hvr at gnu.org>
>---------------------------------------------------------------
12895aea1eeea62d6da1348c61445a7bf285c567
GHC/Integer/GMP/Prim.hs | 88 ++++++++++++-------
GHC/Integer/Type.lhs | 160 ++++++++++++++++------------------
cbits/gmp-wrappers.cmm | 223 +++++++++++++++++++++++++++++++++--------------
3 files changed, 285 insertions(+), 186 deletions(-)
Diff suppressed because of size. To see it, use:
git diff-tree --root --patch-with-stat --no-color --find-copies-harder --ignore-space-at-eol --cc 12895aea1eeea62d6da1348c61445a7bf285c567
More information about the ghc-commits
mailing list