[ZCM] [ZC] 1633/ 5 Comment "Lurking CPU eater in Transience"
Collector: Zope Bugs, Features,
and Patches ...
zope-coders-admin at zope.org
Mon Dec 20 14:27:40 EST 2004
Issue #1633 Update (Comment) "Lurking CPU eater in Transience"
Status Accepted, Zope/bug+solution medium
To followup, visit:
http://collector.zope.org/Zope/1633
==============================================================
= Comment - Entry #5 by evan on Dec 20, 2004 2:27 pm
Absolutely. My "bad idea" comment refers to the likelihood of writing code with surprising (and therefore incorrect) results.
Your one-liner correctly captures the intent of this function, and should be used.
________________________________________
= Comment - Entry #4 by tim_one on Dec 20, 2004 2:04 pm
Everything about mutating Python lists while iterating over them is defined. It can be *surprising* if the list ever changes size while that's going on, but it's still well-defined even then. There's nothing surprising about what happens if the list doesn't change size.
That said, variables named "l" should be outlawed. In Courier New, there's only one pixel to distinguish the letter from the digit, and I couldn't understand at first why the patched code returned the digit 1.
Suggest something like
result = [x * period + begin for x in xrange(n)]
result.reverse()
return result
instead. Then it's readable, fast, and nobody can get confused about mutation-while-iterating.
OTOH, if ``begin`` and ``period`` are integers, and period>0, it's really just doing this one-liner:
return range(begin + period*(n-1), begin-1, -period)
________________________________________
= Comment - Entry #3 by evan on Dec 20, 2004 1:54 pm
Inserting or deleting elements or replacing a slice that spans the current element in a for-loop is generally a bad idea. Changing the value of a list element is fine.
________________________________________
= Comment - Entry #2 by mcdonc on Dec 20, 2004 1:10 pm
> This was due to it inserting tens of thousands of elements into the head
> of an empty list, one at a time. While I learned my lesson, and bumped
> the resolution up to match the timeout, I also decided to fix
> getTimeslices. A patch is attached.
Cool, thanks! I'm wondering, though, I've always heard it was bad to mutate a list while iterating over it. Does that only matter if you change the size of the list while doing so?
________________________________________
= Request - Entry #1 by evan on Dec 20, 2004 12:02 pm
Status: Pending => Accepted
Supporters added: chrism
Uploaded: "p"
- http://collector.zope.org/Zope/1633/p/view
I foolishly set the timeout of a Transient Object Container to a very high value while leaving the timeout resolution at 20. Subsequent profiling revealed that Transience.py's getTimeslices was using 100% CPU for more than a second every time it was called, which was frequently. This was due to it inserting tens of thousands of elements into the head of an empty list, one at a time. While I learned my lesson, and bumped the resolution up to match the timeout, I also decided to fix getTimeslices. A patch is attached.
==============================================================
More information about the Zope-Collector-Monitor
mailing list