From d12ab96fc7bb0d157c1bac8011b462d01049cc7d Mon Sep 17 00:00:00 2001 From: trizen Date: Tue, 19 Jan 2021 10:09:21 +0200 Subject: [PATCH] - Changed all the `goto &Math::*` calls to regular function calls. 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. --- lib/Sidef/Types/Number/Number.pm | 44 ++++++++++++++------------------ 1 file changed, 19 insertions(+), 25 deletions(-) diff --git a/lib/Sidef/Types/Number/Number.pm b/lib/Sidef/Types/Number/Number.pm index 58cf5ae0e..742174326 100644 --- a/lib/Sidef/Types/Number/Number.pm +++ b/lib/Sidef/Types/Number/Number.pm @@ -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); @@ -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)) { @@ -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; @@ -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); } } @@ -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; } } @@ -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: { @@ -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: {