[pypy-dev] Declaring a function that returns a string in CFFI

Armin Rigo arigo at tunes.org
Thu Sep 25 14:10:38 CEST 2014


Hi,

On 25 September 2014 09:06, Elefterios Stamatogiannakis
<estama at gmail.com> wrote:
> Unfortunately, the C library that i use (libsqlite3) does not provide a
> function like that :( . It has a function that returns the size of the
> string, but in my tests the overhead of doing another CFFI call (to find the
> size) is greater than doing the 2nd copy (depending on the average string
> size).

In general, if performance is an issue, particularly if you're running
CPython (as opposed to PyPy), you can try to write small helpers in C
that regroup a few operations.  This can reduce the overhead of doing
two calls instead of one.  In this case, you can write this in the
ffi.verify() part:

size_t myGetString(xxx, char **presult)
{
    *presult = getString(xxx);
    return strlen(*presult);
}

and then in Python you'd declare the function 'myGetString', and use
it like that:

p = ffi.new("char *[1]")        # you can put this before some loop
...
size = lib.myGetString(xxx, p)
..ffi.buffer(p[0], size)..


A bientôt,

Armin.


More information about the pypy-dev mailing list