Mercurial > hg > cc > cirrus_work
changeset 264:7886d7de5eed default tip
use cdb library directly,
sequestration of cdb handle complete and working,
nndb counts two loops now, one with and one without counting successes
author | Henry S. Thompson <ht@inf.ed.ac.uk> |
---|---|
date | Fri, 31 Jan 2025 13:31:02 +0000 |
parents | f457d7eb8cbe |
children | |
files | lib/python/cc/lmh/db.pxd lib/python/cc/lmh/db.pyx lib/python/cc/lmh/nndb.pyx |
diffstat | 3 files changed, 30 insertions(+), 18 deletions(-) [+] |
line wrap: on
line diff
--- a/lib/python/cc/lmh/db.pxd Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/db.pxd Fri Jan 31 13:31:02 2025 +0000 @@ -1,5 +1,8 @@ cimport cdb cdef class CCdb: cdef cdb.Cdb* _c_cdb + cdef void findstart(self) cdef int find(self, bytes p) cdef char[::1] init(self, int fno) + cdef cdb.uint32 pos(self) + cdef int len(self)
--- a/lib/python/cc/lmh/db.pyx Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/db.pyx Fri Jan 31 13:31:02 2025 +0000 @@ -1,6 +1,5 @@ -# distutils: sources = [cdb_min/cdb.c, cdb_min/error.c, cdb_min/open_read.c, cdb_min/seek_cur.c, cdb_min/open_trunc.c, cdb_min/seek_set.c, cdb_min/byte_copy.c, cdb_min/byte_diff.c, cdb_min/error_str.c, cdb_min/uint32_unpack.c, cdb_min/cdb_hash.c] -# extra_objects = cdb_min/libcdb.a -# distutils: include_dirs = cdb_min +# distutils: extra_objects = cdb/libcdb.a +# distutils: include_dirs = cdb cimport cdb @@ -8,6 +7,9 @@ def __cinit__(self): self._c_cdb = cdb.cdb_new() + cdef void findstart(self): + cdb.cdb_findstart(self._c_cdb) + cdef int find(self, bytes p): return cdb.cdb_find(self._c_cdb, p, len(p)) @@ -15,3 +17,9 @@ cdb.cdb_init(self._c_cdb,fno) cdef char[::1] _mview = <char[:cdb.cdb_msize():1]>(cdb.cdb_mmap()) return _mview + + cdef cdb.uint32 pos(self): + return cdb.cdb_pos() + + cdef int len(self): + return cdb.cdb_len()
--- a/lib/python/cc/lmh/nndb.pyx Fri Jan 31 13:28:09 2025 +0000 +++ b/lib/python/cc/lmh/nndb.pyx Fri Jan 31 13:31:02 2025 +0000 @@ -1,8 +1,6 @@ -# distutils: extra_objects = cdb_min/libcdb.a -# sources = [cdb_min/cdb.c, cdb_min/error.c, cdb_min/open_read.c, cdb_min/seek_cur.c, cdb_min/open_trunc.c, cdb_min/seek_set.c, cdb_min/byte_copy.c, cdb_min/byte_diff.c, cdb_min/error_str.c, cdb_min/uint32_unpack.c, cdb_min/cdb_hash.c] -# distutils: include_dirs = cdb_min +# distutils: extra_objects = cdb/libcdb.a +# distutils: include_dirs = cdb -cimport cdb cimport db # Usage: ... cdb-path probe nreps @@ -16,30 +14,33 @@ cdef db.CCdb testMe(bytes probe): global CC res: int = -1 - cdef cdb.Cdb* c_cdb print('testing... %s %s x %s'%(sys.argv[1],probe,int(sys.argv[3]))) with open(sys.argv[1],'rb') as dbf: mv = CC.init(dbf.fileno()) - c_cdb = CC._c_cdb - print("mv %s cdb %s"%(len(mv),<long>(c_cdb))) - cdb.cdb_findstart(c_cdb) + CC.findstart() res=CC.find(probe) if res == 1: - print(res,o:=cdb.cdb_pos(), l:=cdb.cdb_len()) + print(res,o:=CC.pos(), l:=CC.len()) sys.stdout.buffer.write(mv[o:o+l]) sys.stdout.buffer.write(b'\n') else: print('losing %s'%res) print('tested') - return CC + +testMe(sys.argv[2].encode('utf8')) + +probe = sys.argv[2].encode("utf8") def cfind(p: bytes) -> int: global CC return CC.find(p) -testMe(sys.argv[2].encode('utf8')) +print(timeit.timeit('cfind(probe)', + number=int(sys.argv[3]), globals = globals())) -probe = sys.argv[2].encode("utf8") - -print(timeit.timeit('cfind(probe)', - number=int(sys.argv[3]), globals=globals())) +X = 0 +print(timeit.timeit('(X:=X+1) if cfind(probe)==1 else None', + number=int(sys.argv[3]), + setup = 'global X', + globals = globals()), + X)