-
-
Couldn't load subscription status.
- Fork 680
Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc #40734
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
base: develop
Are you sure you want to change the base?
Conversation
|
Documentation preview for this PR (built with commit da6ef59; changes) is ready! 🎉 |
|
This is probably not new, but the Ctrl-C test fails reliably for me: |
|
The rest looks reasonable. There are some opportunities for simultaneous init/set (https://gmplib.org/manual/Simultaneous-Integer-Init-_0026-Assign) I think. |
|
init/set would be strictly worse in this case, since too bad the init/swap/delete empty is (I suspect) not inlined, if it were inlined it would be almost O(0). (that's wrong use of big-O notation, I know. |
|
Not at the end... I meant in situations like - cdef Integer n = Integer(self) # need a copy as it is modified below
-
+ cdef mpz_t mm, n
+ mpz_init(mm)
+ mpz_init(n)
+ mpz_set(n, self.value)
+ mpz_set(mm, (<Integer>m).value)Surely |
|
okay, you're right. |
|
Probably a topic for wider discussion, but it always seemed weird to me that we cache the first 100 integers to be deallocated. Seems like it would be a lot simpler to cache 0-10, the first few primes, the first few powers of those primes, etc. There are a lot of commonly used integers and 100 isn't very many. It would eliminate a lot of voodoo if we just picked 100 that everyone likes. |
no, the point is if you do then you don't incur the cost of allocation/deallocation every loop iteration. |
seems to work on my machine (though I'm still on 10.8.beta5), so not much I can do. I guess if it makes the test work better you can set it to 0.5s or something… 0.5s is still acceptable to human observer right. |
|
okay I can reproduce it once in roughly 50 tries. worth investigating, I suppose. |
I know what it's trying to do, but 100 is not very many. If that loop is the first thing you do in a sage session, you're probably OK; but if you mess around for a while first, the pool is going to be filled with integers you don't care about. Then when you write, the extremely common powers of two will still have to be allocated and deallocated each time because the pool is filled with random numbers. |
|
after adding the latest round of
|
sorry, I don't understand. See the comments below |
|
by the way, we do have a separate pool for small integers, but in order to create such an integer you need to explicitly invoke doing a |
|
Ok, I concede, I will have to instrument integer.pyx with some helper functions and then come back if I still think there's room for improvement. |
|
The tests are better now, I'm at 15,000 iterations and counting. This probably needs a rebase onto develop ( |
|
Huh, I didn't notice there's merge conflict.
|
|
Just a guess since I deleted the patch file, but I think |
sagemathgh-40734: Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc Follow-up to sagemath#40556. Now the `sig_occurred()` check in `fast_tp_dealloc` is removed. ------ This is the effect on the last commit, changing `divisors()` to use `sig_check()`. Before: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 245 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 616 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 302 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` After: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.6 ms ± 424 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 254 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.7 ms ± 206 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40734 Reported by: user202729 Reviewer(s):
sagemathgh-40734: Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc Follow-up to sagemath#40556. Now the `sig_occurred()` check in `fast_tp_dealloc` is removed. ------ This is the effect on the last commit, changing `divisors()` to use `sig_check()`. Before: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 245 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 616 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 302 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` After: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.6 ms ± 424 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 254 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.7 ms ± 206 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40734 Reported by: user202729 Reviewer(s):
sagemathgh-40734: Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc Follow-up to sagemath#40556. Now the `sig_occurred()` check in `fast_tp_dealloc` is removed. ------ This is the effect on the last commit, changing `divisors()` to use `sig_check()`. Before: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 245 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 616 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 302 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` After: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.6 ms ± 424 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 254 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.7 ms ± 206 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40734 Reported by: user202729 Reviewer(s):
sagemathgh-40734: Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc Follow-up to sagemath#40556. Now the `sig_occurred()` check in `fast_tp_dealloc` is removed. ------ This is the effect on the last commit, changing `divisors()` to use `sig_check()`. Before: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 245 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 616 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 302 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` After: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.6 ms ± 424 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 254 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.7 ms ± 206 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40734 Reported by: user202729 Reviewer(s):
sagemathgh-40734: Avoid mutate Integer.value at a few more places, remove sig_occurred() check in Integer fast_tp_dealloc Follow-up to sagemath#40556. Now the `sig_occurred()` check in `fast_tp_dealloc` is removed. ------ This is the effect on the last commit, changing `divisors()` to use `sig_check()`. Before: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 245 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 616 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20 ms ± 302 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` After: ``` sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.6 ms ± 424 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.5 ms ± 254 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) sage: n = prod(primes_first_n(17)) ....: %timeit ignore = n.divisors() 20.7 ms ± 206 μs per loop (mean ± std. dev. of 7 runs, 10 loops each) ``` ### 📝 Checklist <!-- Put an `x` in all the boxes that apply. --> - [ ] The title is concise and informative. - [ ] The description explains in detail what this PR is about. - [ ] I have linked a relevant issue or discussion. - [ ] I have created tests covering the changes. - [ ] I have updated the documentation and checked the documentation preview. ### ⌛ Dependencies <!-- List all open PRs that this PR logically depends on. For example, --> <!-- - sagemath#12345: short description why this is a dependency --> <!-- - sagemath#34567: ... --> URL: sagemath#40734 Reported by: user202729 Reviewer(s):
Follow-up to #40556. Now the
sig_occurred()check infast_tp_deallocis removed.This is the effect on the last commit, changing
divisors()to usesig_check().Before:
After:
📝 Checklist
⌛ Dependencies