Hi,
2015-05-13 4:42 GMT+02:00 Yicong Huang <[email protected]>:
> 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
_______________________________________________
pypy-dev mailing list
[email protected]
https://mail.python.org/mailman/listinfo/pypy-dev