Why GIL? (was Re: what's the point of rpython?)

Carl Banks pavlovevidence at gmail.com
Fri Jan 23 14:45:50 EST 2009


On Jan 23, 7:33 am, s... at pobox.com wrote:
>     >> You mean every time you access a list or dictionary or class
>     >> instance, you have to acquire a mutex?  That sounds like a horrible
>     >> slowdown.
>
>     Steve> Indeed it would, but hey, let's not let that stop us repeating
>     Steve> the thinking that's gone into CPython over the last fifteen
>     Steve> years. "Those who cannot remember the past are condemned to
>     Steve> repeat it".
>
> Also, every object is mutable at some level.  Tuples, ints and floats are
> definitely mutable at creation time.  You need to hold a mutex then, so
> Carl's notion of three types of objects breaks down then.

immutable_type objects wouldn't exist at all until their
PyWhatever_New or their tp_new member is called.  After that, the
reference exists only on the local stack, which is accessible only to
one thread.  As long as you finish initializing the object while it's
still only on the stack, there is no possibility of a conflict.

What about tp_init, then, you ask?  Well it's simple: immutable_type
doesn't call it.  In fact, it requires that tp_init, tp_setattro,
tp_mapping->mp_setitem, etc., are all null.

immutable_obejcts have no instance dict, so if you want to create
attributes in Python you have to use slots.  immutable_object.__new__
accepts keyword arguments and initializes the slots with the value.

class Record(immutable_object,slots=['name','number']):
    def __new__(cls,name):
        number = db.lookup_number(name)
        immutable_object.__new__(cls,name=name,number=number)


Carl Banks



More information about the Python-list mailing list