You are viewing a plain text version of this content. The canonical link for it is here.
Posted to dev@lucy.apache.org by Marvin Humphrey <ma...@rectangular.com> on 2016/02/04 22:53:50 UTC
[lucy-dev] String optimizations in Python host
Lucifers,
Under the Python bindings, Clownfish objects *are* Python objects. Each
Clownfish object struct def starts with the Python object struct def:
typedef struct cfish_Obj {
Py_ssize_t ob_refcnt; // Python, shared by Clownfish
PyTypeObject *ob_type; // Used by Python exclusively
struct cfish_Class *klass; // Used by Clownfish exclusively
} cfish_Obj;
This means that we don't need host wrappers when sending Clownfish objects
into Python space -- they can be used directly by the host, unlike Clownfish
objects under our bindings for Perl and Go.
It also means that under Python, Clownfish objects must conform to Python's
refcounting conventions.
Unfortunately, the "stack string" optimization we use under Perl requires
overriding refcounting -- Clownfish stack strings are "copy on incref". This
won't work under Python because Python may incref a stack string without
knowing that it needs to make a copy instead.
If a Clownfish stack string ever winds up in Python space, and Python increfs
it without making a copy, the result will likely be a pointer to stack memory
in a (Python) heap data structure. Once the stack frame where the stack
string was allocated goes away, we'll be left with a dangling pointer and an
eventual memory error.
Therefore, the stack string optimization is not feasible under Python -- or
any other host where we can't implement copy-on-incref Strings.
It may still be possible to optimize Clownfish-Python string conversion,
though. We'll have to allocate all Clownfish strings on the heap, but they
can still wrap string content from a Python string.
Python strings are immutable, so as long as the Clownfish String holds a
refcount open on a Python string, it can wrap the UTF-8 string buffer obtained
via `PyUnicode_AsUTF8AndSize`:
https://docs.python.org/3/c-api/unicode.html?highlight=pyunicode_asutf8#c.PyUnicode_AsUTF8AndSize
This caches the UTF-8 representation of the string in the Unicode object,
and subsequent calls will return a pointer to the same buffer. The caller
is not responsible for deallocating the buffer.
Implementing this optimization would require changes to String's internals.
For the time being, I intend to just copy all string content and leave this
optimization as a TODO.
Marvin Humphrey
Re: [lucy-dev] String optimizations in Python host
Posted by Nick Wellnhofer <we...@aevum.de>.
On 04/02/2016 22:53, Marvin Humphrey wrote:
> It may still be possible to optimize Clownfish-Python string conversion,
> though. We'll have to allocate all Clownfish strings on the heap, but they
> can still wrap string content from a Python string.
>
> Python strings are immutable, so as long as the Clownfish String holds a
> refcount open on a Python string, it can wrap the UTF-8 string buffer obtained
> via `PyUnicode_AsUTF8AndSize`:
Or we add support for additional encodings to Clownfish::String. Then we
should be able to use Python string buffers directly without conversion to
UTF-8. Support for multiple encodings shouldn't be too hard to implement, now
that our String API is mostly based on iterators.
> Implementing this optimization would require changes to String's internals.
> For the time being, I intend to just copy all string content and leave this
> optimization as a TODO.
I agree that this optimization isn't a priority.
Nick
Re: [lucy-dev] String optimizations in Python host
Posted by Logan Bell <lo...@apache.org>.
+1
On Fri, Feb 5, 2016 at 7:03 AM, Peter Karman <pe...@peknet.com> wrote:
> Marvin Humphrey wrote on 2/4/16, 3:53 PM:
>
>> Implementing this optimization would require changes to String's
>> internals.
>> For the time being, I intend to just copy all string content and leave
>> this
>> optimization as a TODO.
>
>
> +1
>
> --
> Peter Karman . http://peknet.com/ . peter@peknet.com
Re: [lucy-dev] String optimizations in Python host
Posted by Peter Karman <pe...@peknet.com>.
Marvin Humphrey wrote on 2/4/16, 3:53 PM:
> Implementing this optimization would require changes to String's internals.
> For the time being, I intend to just copy all string content and leave this
> optimization as a TODO.
+1
--
Peter Karman . http://peknet.com/ . peter@peknet.com