On Thu, Jul 08, 2010 at 11:02:50AM -0700, Shawn Walker wrote:
> On 07/ 8/10 10:30 AM, [email protected] wrote:
> >api.py:
> >
> >   - line 1032 (and others):  I've seen a bunch of instances of this:
> >
> >     while tgt is not None:
> >
> >     can this be re-written as?
> >
> >     while tgt:
> >
> >     Is it ever possible for this code 'ren_stems.get(stem, None)' to
> >     return a stem that's valid but evaluates to False?  If not, it's
> >     simpler to omit the 'is' part of the check.
> 
> In Python, testing for identity is much faster than testing for
> "truthness" or "equality" because it simply compares memory
> addresses. Since this code is a hot path, I've opted to take the
> Python developer's recommendations to test for identity.

It may be faster, but it comes at the expense of less readable code.
Our vacationing language nurse has also been pretty stern about the
format of comparisons.  If this is the hot path, can you quantify how
much faster this path runs with the 'is' comparison than the standard
method?

> >manifest.py:
> >
> >   - line 296:  It would simplify this code if you initialized this
> >     to an empty list instead.  That would mean that you could remove
> >     lines 316-319.  (How much overhead is there for allocating an empty
> >     list?  I would expect the yielding of results to dominate the time
> >     spent in this function).  This also means that you could change line
> >     324 to 'if errors' since it will only return True if there are items
> >     in the list.
> 
> Since the error case is not common, using errors = None avoids a
> significant number of pointless memory allocations for a list that
> will never be populated.

This seems like another case of premature optimization.  The whole point
of a language like Python is that it hides memory allocation from the
programmer.  Unless there's a performance problem with this code that
warrants additional optimization, it seems silly to complicate the code
further.

This also relies on an assumption that list allocation occurs when we
assign the empty list.  In C Python 2, the interpreter keeps a freelist
of list objects, which are available for reuse without a call to
malloc/free.  I think it's also hypothetically feasible for an
interpreter to keep a single empty list object, and only peform
allocations when the caller actually adds items to the empty COW
list.  I would prefer that we only make assumptions about the underlying
behavior of the runtime when it's absolutely necessary.

-j
_______________________________________________
pkg-discuss mailing list
[email protected]
http://mail.opensolaris.org/mailman/listinfo/pkg-discuss

Reply via email to