[pypy-dev] How to call the "same pypy function" from C for thousands of times?
Yicong Huang
hengha.mao at gmail.com
Thu May 14 05:02:42 CEST 2015
Great thanks!
The method slove our problems.
On Wed, May 13, 2015 at 3:58 PM, Amaury Forgeot d'Arc <amauryfa at gmail.com>
wrote:
> Hi,
>
> 2015-05-13 4:42 GMT+02:00 Yicong Huang <hengha.mao at gmail.com>:
>
>> However, the code is quite slow for reasons:
>> 1. Pypy treats the same function call for a new function every time. It
>> took time to do parsing, analysing and such a lot of work.
>> 2. JIT could not give any benifits for the single function call.
>>
>
> As said in the docs, the trick is to call pypy_execute_source once, and
> make it return a set of C function pointers.
>
> Here is how I would do it:
>
> - in a file "interface.py"
>
> import cffi
>
> ffi = cffi.FFI()
> ffi.cdef('''
> struct API {
> double (*add_numbers)(double x, double y);
> };
> ''')
>
> # Better define callbacks at module scope, it's important to keep this
> object alive.
> @ffi.callback("double (double, double)")
> def add_numbers(x, y):
> return x + y
>
> def fill_api(ptr):
> api = ffi.cast("struct API*", ptr)
> api.add_numbers = add_numbers
>
>
> - From C code:
>
> struct API {
> double (*add_numbers)(double x, double y);
> };
>
> API api;
> const char source[] = "import interface;
> interface.fill_api(c_argument)";
> res = pypy_execute_source_ptr(source, &api);
>
> // Now call this in a loop:
> api.add_numbers(12, 13);
>
>
> --
> Amaury Forgeot d'Arc
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/pypy-dev/attachments/20150514/efc7ffbb/attachment.html>
More information about the pypy-dev
mailing list