[pypy-dev] a possible leak in the object namespace...

Alex A. Naanou alex.nanou at gmail.com
Mon Nov 29 15:04:08 CET 2010


Hi All,

Decided to test the 1.4 with a couple of my usecases, got quite
surprising results...

To keep it short, I have several usecases that involve replacing the
object dictionary.

Here's a simple example:

---cut---

class D(dict):
	def __setitem__(self, key, value):
		print 'updating attr "%s" to "%s"...' % (key, value)
		super(D, self).__setitem__(key, value)

class X(object): pass

x = X()
x.__dict__ = D()

x.a = 1 # will print a nice log string...


# NOTE: this will not print anything in CPython (see P.S.)

--uncut--

With the release of version 1.4, I decided to test these usecases out
and benchmark them on PyPy and 15 minutes later I got results that
were surprising to say the least...

Expectations:
1) the normal/native namespace should have been a bit faster than the
hooked object on the first run. Both cases should have leveled to
about the same performance after the JIT finished it's job +/- a
constant.
2) all times should have been near constant.

What I got per point:
1) the object with native dict was slower by about three orders of
magnitude than the object with a hooked namespace.
2) sequential write benchmark runs on the normal object did not level
out, as they did with the hook, rather, they exhibited exponential
times (!!)


For details and actual test code see the attached file.




P.S. This specific functionality is a weak point in CPython for two reasons:
1) writing to .__dict__ demands a subclass of dict (not a real
problem, though over-restrictive in my view)
2) all interactions with the namespace are done directly via the
Python C-API, completely neglecting the high-level dict interface.


Thanks!

-- 
Alex.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: object_ns_hotwire.py
Type: application/octet-stream
Size: 2993 bytes
Desc: not available
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20101129/c2210c6b/attachment.obj>


More information about the Pypy-dev mailing list