You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
changed the title [-]interators broken for weak dicts[/-][+]iterators broken for weak dicts[/+]on Feb 22, 2008
vdupras commented on Feb 24, 2008
vduprasmannequin
Mannequin
More actions
I made a patch to fix the problem. The cleaning up of they weakref keys or
values will be held until all references to iterators created by the
weakdict are dead.
I also couldn't resist removing code duplication of code in items(),
keys() and values().
At first, I couldn't understand why this whole remove(), _remove() and
selfref() mechanism was in place. I had removed them and replaced them
with methods, and the tests still passed. Then I realized it was to make
sure keys and values didn't prevent the weak dicts from being freed. I
added tests for this.
Interesting patch. I think the intermediate assertEquals in
test_weak_*_dict_flushed_dead_items_when_iters_go_out are just testing
an implementation detail, only the final one should remain.
Also, it is likely the "code duplication" you are talking about was
there for performance reasons, so I'd suggest putting it back in.
vdupras commented on Feb 14, 2009
vduprasmannequin
Mannequin
More actions
About duplicated code and performance:
When I look at the duplicated code, I don't see anything that remotely
looks like a performance tweak. Just to make sure, I made a bench:
#!/usr/bin/env pythonimportsyssys.path.insert(0, 'Lib')
importtimeitimportweakrefclassFoo(object): passdefsetup():
L= [Foo() foriinrange(1000)]
globaldd=weakref.WeakValueDictionary(enumerate(L))
delL[:500] # have some dead weakrefsdefdo():
d.values()
print timeit.timeit(do, setup, number=100000)
Results without the patch:
./python.exe weakref_bench.py
0.804216861725
Results with the patch:
$ ./python.exe weakref_bench.py
0.813000202179
I think the small difference in performance is more attributable to the
extra processing the weakref dict does than the deduplication of the
code itself.
About the test_weak_*_dict_flushed_dead_items_when_iters_go_out:
If a weakref dict keeps its weak reference alive, it's not an
implementation detail, it's a bug. The whole point of using such dicts
is to not keep keys or values alive when they go out.
vdupras commented on Feb 14, 2009
vduprasmannequin
Mannequin
More actions
Oh, that's me again not correctly reading my own tests. It's the
*are_not_held* tests that test that no reference is kept.
I agree about the *flushed_dead_items* being an implementation detail.
Results without the patch:
./python.exe weakref_bench.py
0.804216861725
Results with the patch:
$ ./python.exe weakref_bench.py
0.813000202179
Thanks for the numbers, I see my worries were unfounded.
About the test_weak_*_dict_flushed_dead_items_when_iters_go_out:
If a weakref dict keeps its weak reference alive, it's not an
implementation detail, it's a bug. The whole point of using such dicts
is to not keep keys or values alive when they go out.
I was talking about doing self.assertEqual(len(d), self.COUNT) before
deleting the iterators.
I can confirm that the patch applies with minimal fuzz to the
release26-maint branches and the trunk, and that the added tests fail
without the updated implementation in both cases.
Furthermore, Jim's original demo script emits it error with my stock 2.6.5
Python, but is silent with the patched trunk / 2.6 branch.
elachuni commented on May 6, 2010
elachunimannequin
Mannequin
More actions
Probably old news, but this also affects 2.5.4.
BreamoreBoy commented on Jul 14, 2010
BreamoreBoymannequin
Mannequin
More actions
If this is to go forward the patch will need porting to 2.7, 3.1 and 3.2
vdupras commented on Jul 23, 2010
vduprasmannequin
Mannequin
More actions
It looks like this issue has been fixed in bpo-7105 already. Can we close this ticket?
It's not yet fixed in 2.7 or 2.6. Updating versions.
vdupras commented on Jul 23, 2010
vduprasmannequin
Mannequin
More actions
We might as well backport Antoine's patch rather than take this one (even if mine for 2.x already). It would be weird to have 2 wildly different patches to solve the same problem.
Maybe close this ticket and flag bpo-7105 for backporting?
Activity
dcjim commentedon Nov 10, 2003
You can't use iterators on wekref dicts because items
might be removed from the dictionaries while iterating
due to GC.
I've attached a script that illustrates the bug with
Python 2.3.2. It doesn't matter whether you use weak
key or weak value dicts.
If this can't be fixed, then the iteration methods should
either be removed or made to (lamely) create intermediate
lists to work around the problem.
[-]interators broken for weak dicts[/-][+]iterators broken for weak dicts[/+]vdupras commentedon Feb 24, 2008
I made a patch to fix the problem. The cleaning up of they weakref keys or
values will be held until all references to iterators created by the
weakdict are dead.
I also couldn't resist removing code duplication of code in items(),
keys() and values().
At first, I couldn't understand why this whole remove(), _remove() and
selfref() mechanism was in place. I had removed them and replaced them
with methods, and the tests still passed. Then I realized it was to make
sure keys and values didn't prevent the weak dicts from being freed. I
added tests for this.
devdanzin commentedon Feb 14, 2009
Patch has tests, may need updating.
pitrou commentedon Feb 14, 2009
Interesting patch. I think the intermediate assertEquals in
test_weak_*_dict_flushed_dead_items_when_iters_go_out are just testing
an implementation detail, only the final one should remain.
Also, it is likely the "code duplication" you are talking about was
there for performance reasons, so I'd suggest putting it back in.
vdupras commentedon Feb 14, 2009
About duplicated code and performance:
When I look at the duplicated code, I don't see anything that remotely
looks like a performance tweak. Just to make sure, I made a bench:
print timeit.timeit(do, setup, number=100000)
Results without the patch:
./python.exe weakref_bench.py
0.804216861725
Results with the patch:
$ ./python.exe weakref_bench.py
0.813000202179
I think the small difference in performance is more attributable to the
extra processing the weakref dict does than the deduplication of the
code itself.
About the test_weak_*_dict_flushed_dead_items_when_iters_go_out:
If a weakref dict keeps its weak reference alive, it's not an
implementation detail, it's a bug. The whole point of using such dicts
is to not keep keys or values alive when they go out.
vdupras commentedon Feb 14, 2009
Oh, that's me again not correctly reading my own tests. It's the
*are_not_held* tests that test that no reference is kept.
I agree about the *flushed_dead_items* being an implementation detail.
pitrou commentedon Feb 14, 2009
Thanks for the numbers, I see my worries were unfounded.
I was talking about doing
self.assertEqual(len(d), self.COUNT)beforedeleting the iterators.
tseaver commentedon May 3, 2010
I can confirm that the patch applies with minimal fuzz to the
release26-maint branches and the trunk, and that the added tests fail
without the updated implementation in both cases.
Furthermore, Jim's original demo script emits it error with my stock 2.6.5
Python, but is silent with the patched trunk / 2.6 branch.
elachuni commentedon May 6, 2010
Probably old news, but this also affects 2.5.4.
BreamoreBoy commentedon Jul 14, 2010
If this is to go forward the patch will need porting to 2.7, 3.1 and 3.2
vdupras commentedon Jul 23, 2010
It looks like this issue has been fixed in bpo-7105 already. Can we close this ticket?
mdickinson commentedon Jul 23, 2010
It's not yet fixed in 2.7 or 2.6. Updating versions.
vdupras commentedon Jul 23, 2010
We might as well backport Antoine's patch rather than take this one (even if mine for 2.x already). It would be weird to have 2 wildly different patches to solve the same problem.
Maybe close this ticket and flag bpo-7105 for backporting?
mdickinson commentedon Jul 23, 2010
Agreed.