[Python-Dev] strop vs. string

Greg Stein gstein@lyra.org
Sat, 2 Jun 2001 00:40:05 -0700


On Sat, Jun 02, 2001 at 02:34:43AM -0400, Tim Peters wrote:
> [Tim]
> > If after
> >
> >     b = buffer(some_object)
> >
> > b.__getitem__ needed to refetch the info between
> >
> >     b[i]
> > and
> >     b[i+1]
> >
> > I expect it would be so slow even Greg wouldn't want it anymore.
> 
> [Greg]
> > Huh? I don't think it would be all that slow. It is just a function
> > call.  And I don't think that the getitem slot is really used all that
> > frequently (in a loop) for buffer type objects.
> 
> I expect they index into the buffer memory directly then, right?  Then for
> buffers obtained from mutable objects, any such loop is unsafe in the
> absence of the GIL, or even in its presence if the loop contains code that
> may call back into Python.

Most access is: fetch ptr/len, index into the memory.

And yes: anything within that loop which could conceivably change the target
object (especially a call into Python) could move that ptr.

I was saying that, at the Python level, using a loop and doing b[i] into a
buffer/string/unicode object would seem to be relatively rare. b[0] and
stuff is reasonably common.

> > I've been thinking that refetching the ptr/len is the right fix.
> 
> So is calling __getitem__ all the time then, unless you want to dance on the
> razor's edge.  The idea that you can safely "borrow" memory from a mutable
> object without copying it is brittle.

Stay in C code and don't call into Python. It is safe then. The buffer API
is exactly what you're saying: borrow a memory reference. The concept makes
a lot of things possible that weren't before.

The buffer object's storing of that reference was a mistake.

> > Part of the neglect is also based on Guido's ambivalence. Part is
> > that I haven't needed more from it. The day that I do, then I'll
> > code it up :-) But that doesn't help the "generic" case,
> > unfortunately.
> 
> I take that as "yes" to my "nobody cares about it enough to maintain it?".
> In that light, Guido's ambivalence is indeed surprising <wink>.

Eh? I'll maintain the thing, but you're confusing that with adding more
features into it. Different question.

Cheers,
-g

-- 
Greg Stein, http://www.lyra.org/