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

Eleytherios Stamatogiannakis estama at gmail.com
Thu Sep 25 16:57:54 CEST 2014


On 25/09/14 15:10, Armin Rigo wrote:
> 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:

These tests i'm writting about use PyPy only. In CPython i use a native 
C wrapper (APSW). I try to not use ffi.verify because i want the program 
to be easily deployable. Also i want to test the maximum performance of 
CFFI's API.

> 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)..
>

Wouldn't an "strbuffer" that does this scan (opportunistically) be 
faster for cases like above?

Thank you very much for your suggestions.

l.


More information about the pypy-dev mailing list