On 8/18/2010 3:12 PM, Thomas Jollans wrote:
On Wednesday 18 August 2010, it occurred to John Nagle to exclaim:
On 8/18/2010 11:24 AM, ernest wrote:
Hi,

In this code:

if set(a).union(b) == set(a): pass

Does Python compute set(a) twice?

     CPython does.  Shed Skin might optimize.  Don't know
about Iron Python.

I doubt any actual Python implementation optimizes this -- how could it? The
object "set" is clearly being called twice, and it happens to be called with
the object "a" as a sole argument twice. What if "set" has side effects? A
compiler could only exclude this possibility if it knew exactly what "set"
will be at run time, which it can't.

   That just reflects the rather lame state of Python implementations.
For some other languages, there are JIT compilers that can optimize such
things.  If you rebind a function at run time, the compiled code has to
be invalidated and recompiled with the new binding.

   The HotSpot Java JIT compiler did this.  See

http://books.google.com/books?id=GBISkhhrHh8C&pg=PA786&lpg=PA786&dq=JIT+compiler+rebinding&source=bl&ots=GhZa3XbrNu&sig=OVBOnu0vwlVN_B1QC6jc2ltHk_w&hl=en&ei=IhluTLCXOIymsQOW6vy1Cw&sa=X&oi=book_result&ct=result&resnum=9&ved=0CDsQ6AEwCA#v=onepage&q&f=false

It's really tough to get this right. There's a huge overhead in
either performance or complexity for allowing rebinding of
functions during execution.  It's also a feature very seldom
used after program startup.

I was talking to the Facebook guys doing the compiler for PHP, and they
said that it was a huge win for them that PHP doesn't allow dynamically
replacing a function.

                                John Nagle
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to