[Pyrex] Pyrex enhancement request: 'intern' constant strings

Joseph Koshy jkoshy at FreeBSD.ORG
Wed May 7 07:41:21 CEST 2003


The following optimization could help speed up comparisions of 
string objects.

Consider PYREX code like the following:
+-+-+
cdef class Foo:
  def __getattr__(self,a):
    if a == "foo":
      ..
    elif a == "bar":
      ..
+-+-+

This generates code that uses (a) lots of static C strings and
(b) uses PyString_FromString() on these EACH time the __getattr__
routine is used.  Like so:

+-+-+
static char (__pyx_k98[]) = "foo";
static char (__pyx_k99[]) = "bar";

... snip ...
... inside the __getattr__ function : ...

__pyx_3 = PyString_FromString(__pyx_k99); if (!__pyx_3) { .. error .. }
if (PyObject_Cmp(__pyx_v_a, __pyx_3, &__pyx_1) < 0) { ... error ... }

+-+-+

Would it be possible for the PYREX compiler to generate code to 
'intern' constant C strings at module init time (say, using
`PyString_InternFromString()' ) and have the rest of the generated code 
use the resulting Python string objects directly, instead of creating
single-use objects with PyString_FromString() each time round?

Regards,
Koshy
<jkoshy at freebsd.org>




More information about the Pyrex mailing list