view setup_mpatch_cffi.py @ 30288:ceddc3d94d74

revlog: inline start() and end() for perf reasons When I implemented `hg perfrevlogchunks`, one of the things that stood out was N * _chunk() calls was ~38x slower than 1 _chunks() call. Specifically, on the mozilla-unified repo: N*_chunk: 0.528997s 1*_chunks: 0.013735s This repo has 352,097 changesets. So the average time per changeset comes out to: N*_chunk: 1.502us 1*_chunks: 0.039us If you extrapolate these numbers to a repository with 1M changesets, that comes out to 1.502s versus 0.039s, which is significant. At these latencies, Python attribute lookups and function calls matter. So, this patch inlines some code to cut down on that overhead. The impact of this patch on N*_chunk() calls is clear: ! wall 0.528997 comb 0.520000 user 0.500000 sys 0.020000 (best of 19) ! wall 0.367723 comb 0.370000 user 0.350000 sys 0.020000 (best of 27) So, we go from ~38x slower to ~27x. A nice improvement. But there's still a long way to go. It's worth noting that functionality like revsets perform changelog lookups one revision at a time. So this code path is worth optimizing.
author Gregory Szorc <gregory.szorc@gmail.com>
date Sat, 22 Oct 2016 15:41:23 -0700
parents 90af59b40d8a
children
line wrap: on
line source

from __future__ import absolute_import

import cffi
import os

ffi = cffi.FFI()
mpatch_c = os.path.join(os.path.join(os.path.dirname(__file__), 'mercurial',
                                     'mpatch.c'))
ffi.set_source("_mpatch_cffi", open(mpatch_c).read(),
               include_dirs=["mercurial"])
ffi.cdef("""

struct mpatch_frag {
       int start, end, len;
       const char *data;
};

struct mpatch_flist {
       struct mpatch_frag *base, *head, *tail;
};

extern "Python" struct mpatch_flist* cffi_get_next_item(void*, ssize_t);

int mpatch_decode(const char *bin, ssize_t len, struct mpatch_flist** res);
ssize_t mpatch_calcsize(size_t len, struct mpatch_flist *l);
void mpatch_lfree(struct mpatch_flist *a);
static int mpatch_apply(char *buf, const char *orig, size_t len,
                        struct mpatch_flist *l);
struct mpatch_flist *mpatch_fold(void *bins,
                       struct mpatch_flist* (*get_next_item)(void*, ssize_t),
                       ssize_t start, ssize_t end);
""")

if __name__ == '__main__':
    ffi.compile()