bpo-27535: Fix memory leak with warnings ignore#4489
Conversation
serhiy-storchaka
left a comment
There was a problem hiding this comment.
I want to play with the code more.
Sorry, something went wrong.
|
Ooops, previously, I pushed optimization commits in this PR by mistake. It's now fixed. Again, this PR is only the change to leave the registry unchanged for the "ignore" action. |
Sorry, something went wrong.
The warnings module doesn't leak memory anymore in the hidden warnings registry for the "ignore" action of warnings filters. The warn_explicit() function doesn't add the warning key to the registry anymore for the "ignore" action.
|
I reformatted the if with multiline condition and rebased my PR. |
Sorry, something went wrong.
|
Latest reliable (using isolated CPUs) benchmark: So this PR adds a slowdown of +133 ns on warning.warn() when the warning is ignored. This slowdown can be removed using a "C cache" of warnings.filters: the optimization implemented in PR #4502. The problem is that I'm not sure that it's ok to cache warnings.filters and/or make warnings.filters "immutable" (convert it to a tuple). |
Sorry, something went wrong.
|
The overhead depends on the number and kind of filters. For warnings emitted in a C code the relative slowdown will be larger. |
Sorry, something went wrong.
|
@serhiy-storchaka: Are you ok to merge this PR even with the "1.2x slower" (+19%)? Or do you prefer to wait until a decision can be made on a "C cache" for warnings.filters? |
Sorry, something went wrong.
|
Could you please make a benchmark for warnings emitted in a tight loop in the C code. It would be nice to know what is the worst case. If it is less than 2x slower I will prefer this simpler PR. |
Sorry, something went wrong.
It's not 2x slower, it's between 1.03x slower (+21 ns) and 1.09x slower (+79 ns): |
Sorry, something went wrong.
|
Thanks @vstinner for the PR 🌮🎉.. I'm working now to backport this PR to: 3.6. |
Sorry, something went wrong.
The warnings module doesn't leak memory anymore in the hidden warnings registry for the "ignore" action of warnings filters. The warn_explicit() function doesn't add the warning key to the registry anymore for the "ignore" action. (cherry picked from commit c975878)
|
I backported manually the change to Python 2.7: PR #4588. |
Sorry, something went wrong.
The warnings module doesn't leak memory anymore in the hidden
warnings registry for the "ignore" action of warnings filters.
The warn_explicit() function doesn't add the warning key to the
registry anymore for the "ignore" action.
https://bugs.python.org/issue27535