Subject: | sieve_primes() malloc instead of stack |
Date: | Fri, 11 Jun 2010 08:52:19 +1000 |
To: | bug-Math-Prime-XS [...] rt.cpan.org |
From: | Kevin Ryde <user42 [...] zip.com.au> |
It'd be good if sieve_primes() used malloc() instead of putting the
composite[] array on the stack. With recent debian i386 the stack is
limited to about 8Mbytes, whereas the sieve algorithm can go bigger than
that without too much trouble. Perhaps per below.
Incidentally I believe in the current code the size composite[number] is
one too small. composite[i] can be written with i==number, and that's
one past the end of the array.
--- XS.xs.orig 2010-06-09 10:27:40.000000000 +1000
+++ XS.xs 2010-06-09 10:34:23.000000000 +1000
@@ -51,7 +51,7 @@
PROTOTYPE: $;$
INIT:
long i, n;
- bool composite[number];
+ bool *composite;
PPCODE:
if (items == 1)
base = 2;
@@ -59,7 +59,7 @@
base = SvIV (ST(1));
if (base >= number)
croak ("Base is greater or equal number");
- memset (&composite, 0, number);
+ Newxz (composite, number+1, bool);
for (n = 2; n <= number;)
{
if (n >= base)
@@ -72,6 +72,9 @@
while (composite[n] == 1)
n++;
}
+ /* Or would SAVEFREEPV() ensure a free if EXTEND() or newSViv() hits
+ out of memory? */
+ Safefree(composite);
void
xs_sum_primes (number, ...)