-
-
Notifications
You must be signed in to change notification settings - Fork 30k
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
Building a list of tuples has non-linear performance #48324
Comments
The attached script simply loops building a list of tuples. It has % python tuple_gc_hell.py ~ The time it takes to execute grows linearly with the number of tuples Turning on gc.debug(gc.DEBUG_STATS) shows why as does running python % cumulative self self total It appears the cyclic gc is rechecking all of these tuples repeatedly. |
This is a known problem; see the GC discussions in June for an example, e.g. http://mail.python.org/pipermail/python-dev/2008-June/080579.html |
Someone should try implementing Martin's suggestion one day :) |
Here is a simple patch implementing Martin's proposal with a few basic Using Greg's script, we get: -> without patch: 1000000 2.64134001732 -> with patch: 1000000 2.692289114 -> with GC disabled: 1000000 1.6810901165 |
I didn't test it, but the patch looks okay to me. |
This new patch adds another improvement where tuples can be "optimized". |
This new patch also adds a function named gc.is_tracked() which returns >>> import gc
>>> gc.is_tracked(1)
False
>>> gc.is_tracked([])
True
>>> gc.is_tracked(())
True
>>> gc.is_tracked((0,1))
False
>>> gc.is_tracked((0,"a"))
False
>>> gc.is_tracked((0,[]))
True
>>> gc.is_tracked((0,(1,2)))
False
>>> gc.is_tracked((0,(1,0.55)))
False
>>> gc.is_tracked((0,(1,{})))
True
>>> gc.is_tracked((None, True, False, "a", (1,2,u"z",("another",
"nested", u"tuple")), int))
False
>>> gc.is_tracked(gc)
True (as you see the empty tuple is considered tracked, this is not important |
Here is a new patch adding tests for gc.is_tracked(). |
FWIW, with the tuple optimization, the number of objects in |
New version of Greg's script, with different choices (list of tuples, |
Now an additional patch which applies over the basic gctrigger4.patch. Since dicts are often used as mappings of simple objects (int, str, |
Cleanup: this patch only has the algorithmic change. Tuple and dict opts |
The optimization issue for tuples and dicts is bpo-4688. |
Looking at gctrigger5.patch, my only thought is that it would be Otherwise, LGTM. |
Patch with updated comments. |
LGTM. Go ahead and commit this. |
Ok, committed in trunk and py3k. Thanks! |
Thanks! |
Note: these values reflect the state of the issue at the time it was migrated and might not reflect the current state.
Show more details
GitHub fields:
bugs.python.org fields:
The text was updated successfully, but these errors were encountered: