~bzr-pqm/bzr/bzr.dev

« back to all changes in this revision

Viewing changes to bzrlib/_readdir_pyx.pyx

Streamline _walkdirs_utf8 for utf8 file systems, reducing time to traverse a mozilla tree from 1s to .6 seconds. (Robert Collins)

Show diffs side-by-side

added added

removed removed

Lines of Context:
29
29
    int errno
30
30
    char *strerror(int errno)
31
31
 
 
32
cdef extern from 'unistd.h':
 
33
    int chdir(char *path)
 
34
    char *getcwd(char *, int size)
 
35
 
 
36
cdef extern from 'stdlib.h':
 
37
    void *malloc(int)
 
38
    void free(void *)
 
39
 
 
40
cdef extern from 'sys/stat.h':
 
41
    cdef struct stat:
 
42
        int st_mode
 
43
        int st_size
 
44
        int st_dev
 
45
        int st_ino
 
46
        int st_mtime
 
47
        int st_ctime
 
48
    int lstat(char *path, stat *buf)
 
49
    int S_ISDIR(int mode)
 
50
    int S_ISCHR(int mode)
 
51
    int S_ISBLK(int mode)
 
52
    int S_ISREG(int mode)
 
53
    int S_ISFIFO(int mode)
 
54
    int S_ISLNK(int mode)
 
55
    int S_ISSOCK(int mode)
 
56
 
 
57
 
32
58
cdef extern from 'sys/types.h':
33
59
    ctypedef long ssize_t
34
60
    ctypedef unsigned long size_t
 
61
    ctypedef long time_t
 
62
 
 
63
 
 
64
cdef extern from 'Python.h':
 
65
    char * PyString_AS_STRING(object)
 
66
    int PyOS_snprintf(char *str, size_t size, char *format, ...)
 
67
    ctypedef int Py_ssize_t # Required for older pyrex versions
 
68
    Py_ssize_t PyString_Size(object s)
 
69
    object PyList_GetItem(object lst, Py_ssize_t index)
 
70
    void *PyList_GetItem_object_void "PyList_GET_ITEM" (object lst, int index)
 
71
    int PyList_Append(object lst, object item) except -1
 
72
    void *PyTuple_GetItem_void_void "PyTuple_GET_ITEM" (void* tpl, int index)
 
73
    int PyTuple_SetItem(void *, Py_ssize_t pos, object item) except -1
 
74
    void Py_INCREF(object o)
 
75
    void Py_DECREF(object o)
 
76
 
35
77
 
36
78
cdef extern from 'dirent.h':
37
79
    int DT_UNKNOWN
61
103
_symlink = 'symlink'
62
104
_socket = 'socket'
63
105
_unknown = 'unknown'
 
106
_missing = 'missing'
64
107
 
65
108
dot = ord('.')
66
109
 
68
111
cdef extern from 'readdir.h':
69
112
    pass
70
113
 
71
 
def read_dir(path):
 
114
 
 
115
cdef class _Stat:
 
116
    """Represent a 'stat' result."""
 
117
 
 
118
    cdef readonly int st_mode
 
119
    # nanosecond time definitions use MACROS, fucking up te ability to have a
 
120
    # variable called st_mtime. Yay Ulrich, thanks a lot.
 
121
    cdef readonly time_t _ctime
 
122
    cdef readonly time_t _mtime
 
123
    # cdef readonly double st_atime
 
124
    cdef readonly int st_size
 
125
 
 
126
    cdef readonly int st_dev
 
127
    cdef readonly int st_ino
 
128
 
 
129
    property st_mtime:
 
130
        def __get__(self):
 
131
            return self._mtime
 
132
 
 
133
    property st_ctime:
 
134
        def __get__(self):
 
135
            return self._ctime
 
136
 
 
137
    def __repr__(self):
 
138
        """Repr is the same as a Stat object.
 
139
 
 
140
        (mode, ino, dev, nlink, uid, gid, size, atime, mtime, ctime)
 
141
        """
 
142
        #return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, self.st_atime,
 
143
        #             self.st_mtime, self.st_ctime))
 
144
        return repr((self.st_mode, 0, 0, 0, 0, 0, self.st_size, None,
 
145
                     self._mtime, self._ctime))
 
146
 
 
147
 
 
148
from bzrlib import osutils
 
149
 
 
150
 
 
151
cdef class UTF8DirReader:
 
152
    """A dir reader for utf8 file systems."""
 
153
 
 
154
    cdef readonly object _safe_utf8
 
155
    cdef _directory, _chardev, _block, _file, _fifo, _symlink
 
156
    cdef _socket, _unknown
 
157
 
 
158
    def __init__(self):
 
159
        self._safe_utf8 = osutils.safe_utf8
 
160
        self._directory = _directory
 
161
        self._chardev = _chardev
 
162
        self._block = _block
 
163
        self._file = _file
 
164
        self._fifo = _fifo
 
165
        self._symlink = _symlink
 
166
        self._socket = _socket
 
167
        self._unknown = _unknown
 
168
 
 
169
    def kind_from_mode(self, int mode):
 
170
        """Get the kind of a path from a mode status."""
 
171
        return self._kind_from_mode(mode)
 
172
 
 
173
    cdef _kind_from_mode(self, int mode):
 
174
        # in order of frequency:
 
175
        if S_ISREG(mode):
 
176
            return self._file
 
177
        if S_ISDIR(mode):
 
178
            return self._directory
 
179
        if S_ISCHR(mode):
 
180
            return self._chardev
 
181
        if S_ISBLK(mode):
 
182
            return self._block
 
183
        if S_ISLNK(mode):
 
184
            return self._symlink
 
185
        if S_ISFIFO(mode):
 
186
            return self._fifo
 
187
        if S_ISSOCK(mode):
 
188
            return self._socket
 
189
        return self._unknown
 
190
 
 
191
    def top_prefix_to_starting_dir(self, top, prefix=""):
 
192
        """See DirReader.top_prefix_to_starting_dir."""
 
193
        return (self._safe_utf8(prefix), None, None, None,
 
194
            self._safe_utf8(top))
 
195
 
 
196
    def read_dir(self, prefix, top):
 
197
        """Read a single directory from a utf8 file system.
 
198
 
 
199
        All paths in and out are utf8.
 
200
 
 
201
        This sub-function is called when we know the filesystem is already in utf8
 
202
        encoding. So we don't need to transcode filenames.
 
203
 
 
204
        See DirReader.read_dir for details.
 
205
        """
 
206
        #cdef char *_prefix = prefix
 
207
        #cdef char *_top = top
 
208
        # Use C accelerated directory listing.
 
209
        cdef object newval
 
210
        cdef int index
 
211
        cdef int length
 
212
        cdef void * atuple
 
213
        cdef object name
 
214
 
 
215
 
 
216
        if PyString_Size(prefix):
 
217
            relprefix = prefix + '/'
 
218
        else:
 
219
            relprefix = ''
 
220
        top_slash = top + '/'
 
221
 
 
222
        # read_dir supplies in should-stat order.
 
223
        # for _, name in sorted(_listdir(top)):
 
224
        result = _read_dir(top)
 
225
        length = len(result)
 
226
        # result.sort()
 
227
        for index from 0 <= index < length:
 
228
            atuple = PyList_GetItem_object_void(result, index)
 
229
            name = <object>PyTuple_GetItem_void_void(atuple, 1)
 
230
            # We have inode, name, None, statvalue, None
 
231
            # inode -> path_from_top
 
232
            newval = relprefix + name
 
233
            Py_INCREF(newval)
 
234
            PyTuple_SetItem(atuple, 0, newval)
 
235
            # None -> kind
 
236
            newval = self._kind_from_mode(
 
237
                (<_Stat>PyTuple_GetItem_void_void(atuple, 3)).st_mode)
 
238
            Py_INCREF(newval)
 
239
            PyTuple_SetItem(atuple, 2, newval)
 
240
            # none -> abspath # perhaps only do if its a dir?
 
241
            newval = top_slash + name
 
242
            Py_INCREF(newval)
 
243
            PyTuple_SetItem(atuple, 4, newval)
 
244
        return result
 
245
 
 
246
 
 
247
cdef _read_dir(path):
72
248
    """Like os.listdir, this reads the contents of a directory.
73
249
 
74
250
    :param path: the directory to list.
80
256
    cdef dirent * entry
81
257
    cdef dirent sentinel
82
258
    cdef char *name
83
 
    the_dir = opendir(path)
 
259
    cdef int stat_result
 
260
    cdef stat st
 
261
    cdef _Stat statvalue
 
262
    cdef char * abspath
 
263
    cdef char * pathstart
 
264
    cdef int pathlen
 
265
    cdef int snprintf_result
 
266
    cdef char *cwd
 
267
 
 
268
    cwd = getcwd(NULL, 0)
 
269
    if -1 == chdir(path):
 
270
        raise OSError(errno, strerror(errno))
 
271
    the_dir = opendir(".")
84
272
    if NULL == the_dir:
85
273
        raise OSError(errno, strerror(errno))
 
274
    pathlen = len(path)
 
275
    # allow 1024 bytes of name. (+ '/' + '\x00' to terminate).
 
276
    abspath = <char *>malloc(pathlen + 1026)
 
277
    snprintf_result = PyOS_snprintf(abspath, pathlen + 2, "%s/", PyString_AS_STRING(path))
 
278
    if snprintf_result < 0:
 
279
        raise OSError(errno, strerror(errno))
 
280
    pathstart = abspath + pathlen + 1
86
281
    result = []
87
282
    try:
88
283
        entry = &sentinel
107
302
                (name[1] == 0) or 
108
303
                (name[1] == dot and name[2] == 0))
109
304
                ):
110
 
                result.append((entry.d_ino, entry.d_name))
 
305
                snprintf_result = PyOS_snprintf(pathstart, 1025, "%s", name)
 
306
                if snprintf_result < 0:
 
307
                    raise OSError(errno, strerror(errno))
 
308
                stat_result = lstat(entry.d_name, &st)
 
309
                # stat_result = lstat(abspath, &st)
 
310
                if stat_result != 0:
 
311
                    if errno != ENOENT:
 
312
                        raise OSError(errno, strerror(errno))
 
313
                    else:
 
314
                        kind = _missing
 
315
                        statvalue = None
 
316
                else:
 
317
                    statvalue = _Stat()
 
318
                    statvalue.st_mode = st.st_mode
 
319
                    statvalue._ctime = st.st_ctime
 
320
                    statvalue._mtime = st.st_mtime
 
321
                    # statvalue.st_atime = st.st_atime
 
322
                    statvalue.st_size = st.st_size
 
323
                    statvalue.st_ino = st.st_ino
 
324
                    statvalue.st_dev = st.st_dev
 
325
                # We append a 5-tuple that can be modified in-place by the C
 
326
                # api:
 
327
                # inode to sort on and replace with top_path
 
328
                # name to keep
 
329
                # kind 
 
330
                # statvalue
 
331
                # abspath
 
332
                PyList_Append(result, (entry.d_ino, entry.d_name, None,
 
333
                    statvalue, None))
111
334
    finally:
 
335
        free(abspath)
 
336
        if -1 == chdir(cwd):
 
337
            free(cwd)
 
338
            raise OSError(errno, strerror(errno))
 
339
        free(cwd)
112
340
        if -1 == closedir(the_dir):
113
341
            raise OSError(errno, strerror(errno))
114
342
    return result