[Python-Dev] Accessing globals without dict lookup (original) (raw)

Jason Orendorff jason@jorendorff.com
Fri, 8 Feb 2002 22:09:59 -0600


Guido van Rossum wrote:

- Let a cell be a really simple PyObject, containing a PyObject pointer and a cell pointer. Both pointers may be NULL. (It may have to be called PyGlobalCell since I believe there's already a PyCell object.) (Maybe it doesn't even have to be an object -- it could just be a tiny struct.)

- Let a celldict be a mapping that is implemented using a dict of cells. When you use its getitem method, the PyObject * in the cell is dereferenced, and if a NULL is found, getitem raises KeyError even if the cell exists. Using setitem to add a new value creates a new cell and stores the value there; using setitem to change the value for an existing key stores the value in the existing cell for that key. There's a separate API to access the cells.

The following is totally unimportant, but I feel compelled to share:

I implemented this once, long ago, for Python 1.5-ish, I believe. I got it to the point where it was only 15% slower than ordinary Python, then abandoned it. ;) In my implementation, "cells" were real first-class objects, and "celldict" was a copy-and-hack version of dictionary. I forget how the rest worked.

Anyway, this is all very exciting to me. :)

Jason Orendorff http://www.jorendorff.com/