<div class="gmail_quote">2012/7/3 Stefan Behnel <span dir="ltr">&lt;<a href="mailto:stefan_ml@behnel.de" target="_blank">stefan_ml@behnel.de</a>&gt;</span><br><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">&gt; Don&#39;t forget that cpyext reference counts are quite different from CPython:<br>
&gt; PySequence_GetItem() needs to *create* a PyObject structure, and the<br>
&gt; returned object has a refcount of 1.<br>
&gt; Then Py_DECREF() will really *deallocate* the PyObject structure...<br>
<br>
</div>Sure. And using PySequence_GetItem() is still some 10x faster in PyPy than<br>
taking a borrowed reference using PyList_GET_ITEM() and then calling<br>
Py_INCREF() on it, which is what Cython does in CPython because it&#39;s way<br>
faster there. The overhead of borrowed references is seriously huge in PyPy.<br>
<br>
BTW, are PyObject structures currently cached in a free-list somewhere?<br>
That would be really helpful for the iteration performance.<br></blockquote><div><br></div><div>No optimization of any kind have been done in cpyext (it&#39;s difficult enough to get it right...)</div><div>A freelist would be a nice thing, but there would still be the cost of attaching the PyObject to the pypy w_object.</div>
<div><br></div><div>Maybe we should use a weak dictionary to cache the PyObject structure.</div><div>This already exists for objects defined and created from C...</div><div> </div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex">
<div class="im">
&gt; This is quite more expensive than the simple refcount increment/decrement<br>
&gt; done by CPython.<br>
&gt;<br>
&gt;&gt; OverflowError: array too large<br>
&gt;<br>
&gt; Looks like a ctypes bug to me. Which OS, Python, etc. are you using?<br>
<br>
</div>Ah - totally, sure. I accidentally ran the system Py2.5 on 64bit Linux.<br>
Running it with Py2.7 fixes this specific problem, thanks for the hint!<br>
Although it now names the extension module &quot;nbody.so&quot; instead of<br>
&quot;<a href="http://nbody.pypy-19.so" target="_blank">nbody.pypy-19.so</a>&quot;. Comprend qui peut ...<br>
<br>
After figuring out that I was supposed to enable cpyext manually and<br>
running strace to see what extension module name it is actually looking<br>
for, I failed to make it load the module it just built regardless of how I<br>
named it, so I tried building it within the same run as follows:<br>
<br>
  pypy/bin/py.py --withmod-cpyext -c &#39;import setup; import nbody; \<br>
                                      nbody.test_nbody(1)&#39;  build_ext -i</blockquote></div><br>Ah, but this won&#39;t work!<div>py.py runs on top of CPython, so the PyString_AsString symbol is already defined by your CPython interpreter!</div>
<div><br></div><div>There is a workaround though: compile your extension module with </div><div>   python2.7 pypy/module/cpyext/presetup.py setup.py build_ext -i</div><div><br></div><div>presetup.py will patch distutils, and create a module <span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">&quot;</span><a href="http://nbody.pypy-19i.so">nbody.pypy-19i.so</a><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">&quot; (note the i) </span><span style="background-color:rgb(255,255,255);color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px">which works on top of an *interpreted* pypy.</span></div>
<div><span style="color:rgb(34,34,34);font-family:arial,sans-serif;font-size:13px;background-color:rgb(255,255,255)">Among the hacks, all symbols are renamed: #define </span>PyString_AsString PyPyString_AsString.</div><div>
<font color="#222222" face="arial, sans-serif"><br></font></div><div><font color="#222222" face="arial, sans-serif">Then this should work:</font></div><div><font color="#222222" face="arial, sans-serif">   </font>pypy/bin/py.py --withmod-cpyext -c &quot;import nbody&quot;<font color="#222222" face="arial, sans-serif"><br clear="all">
</font><div>*very* slowly of course, but I was able to debug pygames this way!</div><div><br></div>-- <br>Amaury Forgeot d&#39;Arc<br>
</div>