Skip to content

Commit

Permalink
- Changed all the goto &Math::* calls to regular function calls.
Browse files Browse the repository at this point in the history
This may prevent some memory leaks in some special cases.

One such example, is the following, which used over 500 MB of RAM:

    use Sidef;

    my $n = Sidef::Types::Number::Number->new(0);
    my $d = Sidef::Types::Number::Number->new(6);

    my $digits = $d x $d;
    foreach my $k (1..1e6) {
        push @list, $n if index($n->inc->pow($d)->mul($d), $digits) >= 0
    }

Now, the above code requires only 25 MB of RAM.
  • Loading branch information
trizen committed Jan 19, 2021
1 parent 7fa8345 commit d12ab96
Showing 1 changed file with 19 additions and 25 deletions.
44 changes: 19 additions & 25 deletions lib/Sidef/Types/Number/Number.pm
Original file line number Diff line number Diff line change
Expand Up @@ -664,7 +664,7 @@ package Sidef::Types::Number::Number {
Math_GMPz: {

if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
goto &Math::GMPz::Rmpz_get_ui;
return Math::GMPz::Rmpz_get_ui($x);
}

state $t = Math::GMPz::Rmpz_init_set_str_nobless(join('', ~0), 10);
Expand All @@ -690,8 +690,7 @@ package Sidef::Types::Number::Number {
Math_MPFR: {

if (Math::MPFR::Rmpfr_integer_p($x) and Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
push @_, $ROUND;
goto &Math::MPFR::Rmpfr_get_ui;
return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
}

if (Math::MPFR::Rmpfr_number_p($x)) {
Expand All @@ -718,11 +717,11 @@ package Sidef::Types::Number::Number {
Math_GMPz: {

if (Math::GMPz::Rmpz_fits_slong_p($x)) {
goto &Math::GMPz::Rmpz_get_si;
return Math::GMPz::Rmpz_get_si($x);
}

if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
goto &Math::GMPz::Rmpz_get_ui;
return Math::GMPz::Rmpz_get_ui($x);
}

return;
Expand All @@ -743,13 +742,11 @@ package Sidef::Types::Number::Number {

if (Math::MPFR::Rmpfr_integer_p($x)) {
if (Math::MPFR::Rmpfr_fits_slong_p($x, $ROUND)) {
push @_, $ROUND;
goto &Math::MPFR::Rmpfr_get_si;
return Math::MPFR::Rmpfr_get_si($x, $ROUND);
}

if (Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
push @_, $ROUND;
goto &Math::MPFR::Rmpfr_get_ui;
return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
}
}

Expand Down Expand Up @@ -1063,46 +1060,44 @@ package Sidef::Types::Number::Number {
Math_GMPz: {

if (Math::GMPz::Rmpz_fits_slong_p($x)) {
goto &Math::GMPz::Rmpz_get_si;
return Math::GMPz::Rmpz_get_si($x);
}

if (Math::GMPz::Rmpz_fits_ulong_p($x)) {
goto &Math::GMPz::Rmpz_get_ui;
return Math::GMPz::Rmpz_get_ui($x);
}

goto &Math::GMPz::Rmpz_get_d;
return Math::GMPz::Rmpz_get_d($x);
}

Math_GMPq: {

if (Math::GMPq::Rmpq_integer_p($x)) {
@_ = ($x = _mpq2mpz($x));
$x = _mpq2mpz($x);
goto Math_GMPz;
}

goto &Math::GMPq::Rmpq_get_d;
return Math::GMPq::Rmpq_get_d($x);
}

Math_MPFR: {
push @_, $ROUND;

if (Math::MPFR::Rmpfr_integer_p($x)) {
if (Math::MPFR::Rmpfr_fits_slong_p($x, $ROUND)) {
goto &Math::MPFR::Rmpfr_get_si;
return Math::MPFR::Rmpfr_get_si($x, $ROUND);
}

if (Math::MPFR::Rmpfr_fits_ulong_p($x, $ROUND)) {
goto &Math::MPFR::Rmpfr_get_ui;
return Math::MPFR::Rmpfr_get_ui($x, $ROUND);
}
}

goto &Math::MPFR::Rmpfr_get_d;
return Math::MPFR::Rmpfr_get_d($x, $ROUND);
}

Math_MPC: {
my $r = Math::MPFR::Rmpfr_init2(CORE::int($PREC));
Math::MPC::RMPC_RE($r, $x);
@_ = ($x = $r);
$x = $r;
goto Math_MPFR;
}
}
Expand All @@ -1117,8 +1112,7 @@ package Sidef::Types::Number::Number {
goto(ref($x) =~ tr/:/_/rs);

Math_GMPz: {
push @_, 10;
goto &Math::GMPz::Rmpz_get_str;
return Math::GMPz::Rmpz_get_str($x, 10);
}

Math_GMPq: {
Expand Down Expand Up @@ -5345,15 +5339,15 @@ package Sidef::Types::Number::Number {
goto(ref($x) =~ tr/:/_/rs);

Math_MPFR: {
goto &Math::MPFR::Rmpfr_sgn;
return Math::MPFR::Rmpfr_sgn($x);
}

Math_GMPq: {
goto &Math::GMPq::Rmpq_sgn;
return Math::GMPq::Rmpq_sgn($x);
}

Math_GMPz: {
goto &Math::GMPz::Rmpz_sgn;
return Math::GMPz::Rmpz_sgn($x);
}

Math_MPC: {
Expand Down

0 comments on commit d12ab96

Please sign in to comment.