summaryrefslogtreecommitdiffstats
path: root/Modules/mmapmodule.c
diff options
context:
space:
mode:
authorDennis Sweeney <36520290+sweeneyde@users.noreply.github.com>2022-03-02 04:46:30 (GMT)
committerGitHub <noreply@github.com>2022-03-02 04:46:30 (GMT)
commit6ddb09f35b922a3bbb59e408a3ca7636a6938468 (patch)
tree26b0fbc0a99455f9393e5570ef884999e792ba93 /Modules/mmapmodule.c
parent9833bb91e4d5c2606421d9ec2085f5c2dfb6f72c (diff)
downloadcpython-6ddb09f35b922a3bbb59e408a3ca7636a6938468.zip
cpython-6ddb09f35b922a3bbb59e408a3ca7636a6938468.tar.gz
cpython-6ddb09f35b922a3bbb59e408a3ca7636a6938468.tar.bz2
bpo-46848: Use stringlib/fastsearch in mmap (GH-31625)
Speed up mmap.find(). Add _PyBytes_Find() and _PyBytes_ReverseFind().
Diffstat (limited to 'Modules/mmapmodule.c')
-rw-r--r--Modules/mmapmodule.c32
1 files changed, 13 insertions, 19 deletions
diff --git a/Modules/mmapmodule.c b/Modules/mmapmodule.c
index 26cedf1..6a038e7 100644
--- a/Modules/mmapmodule.c
+++ b/Modules/mmapmodule.c
@@ -315,12 +315,8 @@ mmap_gfind(mmap_object *self,
if (!PyArg_ParseTuple(args, reverse ? "y*|nn:rfind" : "y*|nn:find",
&view, &start, &end)) {
return NULL;
- } else {
- const char *p, *start_p, *end_p;
- int sign = reverse ? -1 : 1;
- const char *needle = view.buf;
- Py_ssize_t len = view.len;
-
+ }
+ else {
if (start < 0)
start += self->size;
if (start < 0)
@@ -335,21 +331,19 @@ mmap_gfind(mmap_object *self,
else if (end > self->size)
end = self->size;
- start_p = self->data + start;
- end_p = self->data + end;
-
- for (p = (reverse ? end_p - len : start_p);
- (p >= start_p) && (p + len <= end_p); p += sign) {
- Py_ssize_t i;
- for (i = 0; i < len && needle[i] == p[i]; ++i)
- /* nothing */;
- if (i == len) {
- PyBuffer_Release(&view);
- return PyLong_FromSsize_t(p - self->data);
- }
+ Py_ssize_t res;
+ if (reverse) {
+ res = _PyBytes_ReverseFind(
+ self->data + start, end - start,
+ view.buf, view.len, start);
+ }
+ else {
+ res = _PyBytes_Find(
+ self->data + start, end - start,
+ view.buf, view.len, start);
}
PyBuffer_Release(&view);
- return PyLong_FromLong(-1);
+ return PyLong_FromSsize_t(res);
}
}