-
-
Notifications
You must be signed in to change notification settings - Fork 5.1k
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
BUG: make C-implemented root finders work with functools.lru_cache #10891
Conversation
Previously a single tuple was created and the first element updated on each call to the underlying function. This broke the immutability condition of tuples, and caused problems e.g. when passing functions wrapped in functools.lru_cache. Closes scipy#10846.
Doc building failures don't seem to have anything to do with this PR. |
I think the bulk of the code you’ve added can probably be replaced with a call to PySequence_Concat. |
@ewmoore the last commit uses Running the above benchmark with this new commit added gives: |
Well never mind then.
…On Tue, Oct 1, 2019 at 12:52 PM Joseph Weston ***@***.***> wrote:
@ewmore the last commit uses PySequence_Concat, however the timings have
deteriorated.
Running the above benchmark with this new commit added gives:
7.98 µs ± 106 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
which is a much larger slowdown.
—
You are receiving this because you commented.
Reply to this email directly, view it on GitHub
<#10891?email_source=notifications&email_token=AARVDELGZI24A3KXPY63VIDQMN53ZA5CNFSM4I4KIRXKYY3PNVWWK3TUL52HS4DFVREXG43VMVBW63LNMVXHJKTDN5WW2ZLOORPWSZGOEAB6MII#issuecomment-537126433>,
or mute the thread
<https://github.com/notifications/unsubscribe-auth/AARVDEL76A3AL6ZUQK7TROLQMN53ZANCNFSM4I4KIRXA>
.
|
f0884f8
to
92b0e93
Compare
I just removed the commit that used |
@pv I think this fix should also be backported to previous scipy versions, but I don't know scipy's policy on providing bugfixes for older versions. |
Reference issue
Closes #10846.
What does this implement/fix?
C-implemented root finders in
scipy.optimize
raiseSystemError
when passed a function wrapped withfunctools.lru_cache
. This is because when calling C-implemented root finders we create the tuple of arguments to pass to the underlying function once, and modify only the first entry on subsequent calls. This breaks when the argument tuple is stored internally byfunctools.lru_cache
.My fix is to instead construct a new argument tuple each time the underlying function is called.
As this change has the potential to slow down the root finders I ran the following benchmark:
on
master
this gives:4.78 µs ± 277 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
on this branch this gives:
5.18 µs ± 204 ns per loop (mean ± std. dev. of 7 runs, 100000 loops each)
which represents an 8% slowdown. I think that this is pretty much an upper bound on the slowdown, as the function we're finding the root of is trivial, and this is using the most naive root-finder (i.e. it does the least amount of work per function call).
Additional information
This bug is present in older versions of Scipy, but I have applied the fix on top of
master
. I could not find any information on Scipy's policy regarding how far back bugfixes should be applied. Let me know if I should apply this fix to one of themaintenance/*
branches instead.