Rev 4752: start trying to expose everything just from cython. in http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

John Arbash Meinel john at arbash-meinel.com
Wed Oct 7 00:00:11 BST 2009


At http://bazaar.launchpad.net/~jameinel/bzr/2.1-static-tuple

------------------------------------------------------------
revno: 4752
revision-id: john at arbash-meinel.com-20091006225959-fogffimckh1m7ww0
parent: john at arbash-meinel.com-20091006213529-k02tyvu39jnxr03k
committer: John Arbash Meinel <john at arbash-meinel.com>
branch nick: 2.1-static-tuple
timestamp: Tue 2009-10-06 17:59:59 -0500
message:
  start trying to expose everything just from cython.
  
  But I found out the argument is moot. You can't override tp_new (__new__)
  to have a custom allocator/StaticTuple() be a singleton.
  So I'm going to go back to the C api, and just make the rest
  pyrex compatible.
-------------- next part --------------
=== modified file 'bzrlib/_btree_serializer_pyx.pyx'
--- a/bzrlib/_btree_serializer_pyx.pyx	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_btree_serializer_pyx.pyx	2009-10-06 22:59:59 +0000
@@ -59,9 +59,9 @@
 
 # It seems we need to import the definitions so that the pyrex compiler has
 # local names to access them.
-from _static_tuple_pyx cimport StaticTuple, \
-    STATIC_TUPLE_ALL_STRING, StaticTuple_New, \
-    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
+# from _static_tuple_pyx cimport StaticTuple, \
+#     StaticTuple_New, \
+#     StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
 
 # TODO: Find some way to import this from _dirstate_helpers

=== modified file 'bzrlib/_chk_map_pyx.pyx'
--- a/bzrlib/_chk_map_pyx.pyx	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_chk_map_pyx.pyx	2009-10-06 22:59:59 +0000
@@ -61,8 +61,8 @@
 
 # It seems we need to import the definitions so that the pyrex compiler has
 # local names to access them.
-from _static_tuple_pyx cimport StaticTuple, StaticTuple_New, \
-    StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
+# from _static_tuple_pyx cimport StaticTuple, StaticTuple_New, \
+#     StaticTuple_Intern, StaticTuple_SET_ITEM, StaticTuple_CheckExact
 
 
 cdef object _LeafNode

=== modified file 'bzrlib/_static_tuple_pyx.pxd'
--- a/bzrlib/_static_tuple_pyx.pxd	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_static_tuple_pyx.pxd	2009-10-06 22:59:59 +0000
@@ -21,8 +21,25 @@
         pass
 
 
-from bzrlib._static_tuple_type_c cimport StaticTuple, StaticTuple_SET_ITEM, \
-    StaticTuple_GET_ITEM, STATIC_TUPLE_INTERNED_FLAG, STATIC_TUPLE_ALL_STRING
+cdef extern from "_static_tuple_pyx_macros.h":
+    # Steals a reference and Val must be a PyStringObject, no checking is done
+    void StaticTuple_SET_ITEM(object key, Py_ssize_t offset, object val)
+    object StaticTuple_GET_ITEM(object key, Py_ssize_t offset)
+    int STATIC_TUPLE_INTERNED_FLAG
+    int STATIC_TUPLE_ALL_STRING
+
+
+cdef public api class StaticTuple [object StaticTuple, type StaticTuple_Type]:
+    cdef unsigned char size
+    cdef unsigned char flags
+    cdef unsigned char _unused0
+    cdef unsigned char _unused1
+    cdef PyObject *items[0]
+
+cdef api StaticTuple StaticTuple_New(Py_ssize_t)
+cdef api StaticTuple StaticTuple_Intern(StaticTuple)
+cdef api int StaticTuple_CheckExact(object)
+
 
 cdef public api class StaticTupleInterner [object StaticTupleInternerObject,
                                            type StaticTupleInterner_type]:
@@ -39,11 +56,8 @@
     cpdef int discard(self, key) except -1
     cdef int _insert_clean(self, PyObject *key) except -1
     cpdef Py_ssize_t _resize(self, Py_ssize_t min_unused) except -1
-# TODO: might want to export the C api here, though it is all available from
-#       the class object...
+
+# TODO: might want to export more of the C api here, though it is all available
+#       from the class object...
 cdef api object StaticTupleInterner_Add(object self, object key)
 
-cdef api StaticTuple StaticTuple_New(Py_ssize_t)
-cdef api StaticTuple StaticTuple_Intern(StaticTuple)
-cdef api int StaticTuple_CheckExact(object)
-

=== modified file 'bzrlib/_static_tuple_pyx.pyx'
--- a/bzrlib/_static_tuple_pyx.pyx	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_static_tuple_pyx.pyx	2009-10-06 22:59:59 +0000
@@ -31,6 +31,7 @@
         hashfunc tp_hash
         richcmpfunc tp_richcompare
 
+    int PyString_CheckExact(object)
     PyTypeObject *Py_TYPE(PyObject *)
     PyVarObject * _PyObject_NewVar(PyTypeObject *, Py_ssize_t) except NULL
         
@@ -70,6 +71,36 @@
     return 0
 
 
+cdef public api class StaticTuple [object StaticTuple, type StaticTuple_Type]:
+    """A tuple-like object that is only allowed to reference constat data."""
+
+    def __new__(cls, *args, **kwargs):
+        cdef StaticTuple mynew
+        cdef Py_ssize_t i, size
+        cdef PyObject *tmp
+
+        print cls, args, kwargs
+        size = len(args)
+        mynew = StaticTuple_New(size)
+        for i from 0 <= i < size:
+            obj = args[i]
+            if (not PyString_CheckExact(obj)):
+                if (not StaticTuple_CheckExact(obj)):
+                    raise TypeError("StaticTuple.__init__(...) requires"
+                        " that all key bits are strings or StaticTuple.")
+            tmp = <PyObject *>obj
+            Py_INCREF(tmp)
+            mynew.items[i] = tmp
+            # StaticTuple_SET_ITEM(mynew, i, tmp)
+        # return mynew
+
+    def __init__(self):
+        pass
+
+    def intern(self):
+        pass
+
+
 cdef public api class StaticTupleInterner [object StaticTupleInternerObject,
                                            type StaticTupleInterner_type]:
     """This class tracks the canonical forms for StaticTuples.
@@ -565,6 +596,8 @@
 
     if size < 0:
         raise ValueError('size must be > 0')
+    if size > 255:
+        raise ValueError('size must be <= 255')
 
     if (size == 0 and _empty_tuple is not None):
         return _empty_tuple

=== added file 'bzrlib/_static_tuple_pyx_macros.h'
--- a/bzrlib/_static_tuple_pyx_macros.h	1970-01-01 00:00:00 +0000
+++ b/bzrlib/_static_tuple_pyx_macros.h	2009-10-06 22:59:59 +0000
@@ -0,0 +1,27 @@
+/* Copyright (C) 2009 Canonical Ltd
+ * 
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ * GNU General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#ifndef _STATIC_TUPLE_PYX_MACROS_H_
+#define _STATIC_TUPLE_PYX_MACROS_H_
+
+#define STATIC_TUPLE_INTERNED_FLAG 0x01
+#define STATIC_TUPLE_ALL_STRING    0x02
+#define StaticTuple_SET_ITEM(key, offset, val) \
+    ((((StaticTuple*)(key))->items[(offset)]) = ((PyObject *)(val)))
+#define StaticTuple_GET_ITEM(key, offset) (((StaticTuple*)key)->items[offset])
+
+#endif // _STATIC_TUPLE_PYX_MACROS_H_

=== removed file 'bzrlib/_static_tuple_type_c.h'
--- a/bzrlib/_static_tuple_type_c.h	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_static_tuple_type_c.h	1970-01-01 00:00:00 +0000
@@ -1,67 +0,0 @@
-/* Copyright (C) 2009 Canonical Ltd
- * 
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 of the License, or
- * (at your option) any later version.
- *
- * This program is distributed in the hope that it will be useful,
- * but WITHOUT ANY WARRANTY; without even the implied warranty of
- * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
- * GNU General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#ifndef _STATIC_TUPLE_H_
-#define _STATIC_TUPLE_H_
-
-#define STATIC_TUPLE_HAS_HASH 0
-/* Caching the hash adds memory, but allows us to save a little time during
- * lookups. TIMEIT hash(key) shows it as
- *  0.108usec w/ hash
- *  0.160usec w/o hash
- * Note that the entries themselves are strings, which already cache their
- * hashes. So while there is a 1.5:1 difference in the time for hash(), it is
- * already a function which is quite fast. Probably the only reason we might
- * want to do so, is if we implement a KeyIntern dict that assumes it is
- * available, and can then drop the 'hash' value from the item pointers. Then
- * again, if Key_hash() is fast enough, we may not even care about that.
- */
-
-/* This defines a single variable-width key.
- * It is basically the same as a tuple, but
- * 1) Lighter weight in memory
- * 2) Only supports strings.
- * It is mostly used as a helper. Note that Keys() is a similar structure for
- * lists of Key objects. Its main advantage, though, is that it inlines all of
- * the Key objects so that you have 1 python object overhead for N Keys, rather
- * than N objects.
- */
-
-#define STATIC_TUPLE_INTERNED_FLAG 0x01
-#define STATIC_TUPLE_ALL_STRING    0x02
-#define STATIC_TUPLE_DID_HASH      0x04
-typedef struct {
-    PyObject_HEAD
-    unsigned char size;
-    unsigned char flags;
-    unsigned char _unused0;
-    unsigned char _unused1;
-    // Note that on 64-bit, we actually have 4-more unused bytes
-    // because items will always be aligned to a 64-bit boundary
-#if STATIC_TUPLE_HAS_HASH
-    long hash;
-#endif
-    PyObject *items[0];
-} StaticTuple;
-extern PyTypeObject StaticTuple_Type;
-
-#define _StaticTuple_CheckExact(obj) (Py_TYPE(obj) == &StaticTuple_Type)
-#define StaticTuple_SET_ITEM(key, offset, val) \
-    ((((StaticTuple*)(key))->items[(offset)]) = ((PyObject *)(val)))
-#define StaticTuple_GET_ITEM(key, offset) (((StaticTuple*)key)->items[offset])
-
-#endif // !_STATIC_TUPLE_H_

=== removed file 'bzrlib/_static_tuple_type_c.pxd'
--- a/bzrlib/_static_tuple_type_c.pxd	2009-10-06 21:35:29 +0000
+++ b/bzrlib/_static_tuple_type_c.pxd	1970-01-01 00:00:00 +0000
@@ -1,36 +0,0 @@
-# Copyright (C) 2009 Canonical Ltd
-#
-# This program is free software; you can redistribute it and/or modify
-# it under the terms of the GNU General Public License as published by
-# the Free Software Foundation; either version 2 of the License, or
-# (at your option) any later version.
-#
-# This program is distributed in the hope that it will be useful,
-# but WITHOUT ANY WARRANTY; without even the implied warranty of
-# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-# GNU General Public License for more details.
-#
-# You should have received a copy of the GNU General Public License
-# along with this program; if not, write to the Free Software
-# Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
-
-cdef extern from "Python.h":
-    ctypedef struct PyObject:
-        pass
-
-cdef extern from "_static_tuple_type_c.h":
-    ctypedef class bzrlib._static_tuple_type_c.StaticTuple [object StaticTuple]:
-        cdef unsigned char size
-        cdef unsigned char flags
-        cdef unsigned char _unused0
-        cdef unsigned char _unused1
-        cdef PyObject *items[0]
-    int STATIC_TUPLE_ALL_STRING
-    int STATIC_TUPLE_INTERNED_FLAG
-
-    # Steals a reference and Val must be a PyStringObject, no checking is done
-    void StaticTuple_SET_ITEM(StaticTuple key, Py_ssize_t offset, object val)
-    object StaticTuple_GET_ITEM(StaticTuple key, Py_ssize_t offset)
-
-
-

=== modified file 'setup.py'
--- a/setup.py	2009-10-06 21:35:29 +0000
+++ b/setup.py	2009-10-06 22:59:59 +0000
@@ -291,13 +291,13 @@
         add_pyrex_extension('bzrlib._dirstate_helpers_pyx')
     add_pyrex_extension('bzrlib._readdir_pyx')
     z_lib = 'z'
-add_pyrex_extension('bzrlib._chk_map_pyx', libraries=[z_lib])
 ext_modules.append(Extension('bzrlib._patiencediff_c',
                              ['bzrlib/_patiencediff_c.c']))
-ext_modules.append(Extension('bzrlib._static_tuple_type_c',
-                             ['bzrlib/_static_tuple_type_c.c']))
+# ext_modules.append(Extension('bzrlib._static_tuple_type_c',
+#                              ['bzrlib/_static_tuple_type_c.c']))
 add_pyrex_extension('bzrlib._static_tuple_pyx')
-add_pyrex_extension('bzrlib._btree_serializer_pyx')
+# add_pyrex_extension('bzrlib._btree_serializer_pyx')
+# add_pyrex_extension('bzrlib._chk_map_pyx', libraries=[z_lib])
 
 
 if unavailable_files:



More information about the bazaar-commits mailing list