-
-
Notifications
You must be signed in to change notification settings - Fork 5.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
merge 2 versions (for GMP 5 & 6) of rand(::UnitRange{BigInt}) #13815
merge 2 versions (for GMP 5 & 6) of rand(::UnitRange{BigInt}) #13815
Conversation
base/gmp.jl
Outdated
ccall((:__gmpz_init,:libgmp), Void, (Ptr{BigInt},), &b) | ||
finalizer(b, _gmp_clear_func) | ||
return b | ||
function BigInt(::Val{:allocbits}, nbits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
allocbits should be a keyword argument
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I feared that a keyword would slow things down... the only point of this feature is to improve efficiency by avoiding allocating unnecessarily one Limb
(the default of mpz_init
) and then re-allocating what is needed from within mpz_*
functions. For the same reason, I think sizehint!
would not work. Also this is really not meant to be a user API (where a BigInt
is immutable), meant only for the wrapping layer of mpz
, so the ugliness was kind of "on purpose". Would you accept a solution like BigInt(::Type{Bool}, nbits)
, or maybe using an ad-hoc type tag BigInt(::Type{SizeHint}, nbits)
? Otherwise, I will update to use a keyword argument.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If it's just going to be internal, we can use a name like BigInt_withsize
and switch to a keyword arg later when they are fast.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Ok i'll update using BigInt_withsize
. Other methods in gmp.jl may benefit from this constructor.
Instead of |
So I finally changed |
52e3144
to
04d780f
Compare
Is |
base/gmp.jl
Outdated
function BigInt(::_withsize, nbits) | ||
z = new(Cint(0), Cint(0), C_NULL) | ||
# WARNING: nbits == 0 is unsafe, some mpz_* functions expect an abs size >= 1 | ||
nbits > 0 && ccall((:__gmpz_init2,:libgmp), Void, (Ptr{BigInt}, Culong), &z, nbits) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
should there be an error when nbits <= 0
instead of just doing nothing?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I left this as an option for the programmer to manually initialize the BigIng
object at a later stage. Note that as long as the object (equal to big(0)
) is not used in a mutative GMP function, this should not be unsafe.
Isn't edit: whoops, hadn't read the collapsed conversation edit2: So rereading it, I think Jeff's suggestion is actually to use a different function name for the |
Whoops, sorry @tkelman I have missed your last comment. I don't understand how to use a different function name, as there has to be an inner constructor which allows to tune the allocation; otherwise, |
Is there currently a meaning to calling BigInt with 3 arguments? Could use the default inner constructor as an unchecked shortcut, make the existing 0 arg method an outer constructor, then use different names for preallocated versions? |
04d780f
to
28286fb
Compare
Ah I get it now, thanks. It's not my preference to introduce a new name for what is actually a constructor but I updated accordingly. The 3-argument constructor is left unchecked (in particular with no finalizer attached). Another option: using a dummy |
Would it help pushing this forward if I entirely remove the |
28286fb
to
47aee88
Compare
I rebased because of merge conflicts. I decided to remove the |
47aee88
to
f8223cd
Compare
f8223cd
to
765febf
Compare
Rebased. I would like to merge soon, if no-one objects... |
Good to go? |
765febf
to
2ca7166
Compare
you've tested this against older libgmp? |
@nanosoldier |
I didn't think to test against older gmp versions, good idea, I'm running the whole tests locally now with gmp-5.1.3 (you must have seen I had rebased, I was about to merge!) |
cc @nalimilan as well who I believe maintains some configurations of his copr against older libgmp, main user of the older branch code |
There is only one failure with gmp-5.1.3, on numbers, which seems unrelated: |
Your benchmark job has completed - possible performance regressions were detected. A full report can be found here. cc @jrevels |
I think we can drop gmp-5 support, if it simplifies things. |
This PR doesn't change the compatibility with gmp-5, it bypasses the different APIs by working directly with the low-level representation. On the other hand, the error with |
As a consequence, using dynamically a version of GMP which does not match the compile time version is no longer an error (this patch makes this a warming). The code is shorter, but we break the MPZ API boundary; this also allows to put allocating pointer_to_array out of the loop, and get slightly better performances.
2ca7166
to
141a8f4
Compare
I saw the error caused by incompatible GMP versions (run time vs compile time) reported few times on the lists, but technically the versions need to agree only for
rand(::UnitRange{BigInt})
, so this PR proposes to remove this constraint.That said I'm not sure if different versions of GMP could reveal other installation problems, so I kept a warning, please comment on this point.
The second commit optimizes slightly the pre-allocation of the random BigInt in some cases (the speed gains are very modest). This is not related to GMP versions, and I could as well make another PR for that.