Mercurial > hg
comparison mercurial/bdiff_module.c @ 31467:08ecec297521
bdiff: use Python memory allocator in fixws
Python has its own memory allocation APIs. For allocations
<= 512 bytes, it allocates memory from arenas. This means that
average small allocations don't call the system allocator, which
makes them faster. Also, arena allocations cut down on memory
fragmentation, which can matter for performance in long-running
processes.
Another advantage of using the Python memory allocator is that
allocations are tracked by Python. This is a bigger deal in
Python 3, as modern versions of Python have some decent built-in
tools for examining memory usage, leaks, etc.
This patch converts a trivial malloc() + free() in the bdiff code
to use the Python allocator APIs. Since the object being
operated on is a line, chances are it will use an arena. So,
this could have a net positive impact on performance (although
I didn't measure it).
author | Gregory Szorc <gregory.szorc@gmail.com> |
---|---|
date | Thu, 09 Mar 2017 11:54:25 -0800 |
parents | 7c0c722d568d |
children | 4195b84940e9 |
comparison
equal
deleted
inserted
replaced
31466:b6bbfbaa205a | 31467:08ecec297521 |
---|---|
156 if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) | 156 if (!PyArg_ParseTuple(args, "Sb:fixws", &s, &allws)) |
157 return NULL; | 157 return NULL; |
158 r = PyBytes_AsString(s); | 158 r = PyBytes_AsString(s); |
159 rlen = PyBytes_Size(s); | 159 rlen = PyBytes_Size(s); |
160 | 160 |
161 w = (char *)malloc(rlen ? rlen : 1); | 161 w = (char *)PyMem_Malloc(rlen ? rlen : 1); |
162 if (!w) | 162 if (!w) |
163 goto nomem; | 163 goto nomem; |
164 | 164 |
165 for (i = 0; i != rlen; i++) { | 165 for (i = 0; i != rlen; i++) { |
166 c = r[i]; | 166 c = r[i]; |
176 } | 176 } |
177 | 177 |
178 result = PyBytes_FromStringAndSize(w, wlen); | 178 result = PyBytes_FromStringAndSize(w, wlen); |
179 | 179 |
180 nomem: | 180 nomem: |
181 free(w); | 181 PyMem_Free(w); |
182 return result ? result : PyErr_NoMemory(); | 182 return result ? result : PyErr_NoMemory(); |
183 } | 183 } |
184 | 184 |
185 | 185 |
186 static char mdiff_doc[] = "Efficient binary diff."; | 186 static char mdiff_doc[] = "Efficient binary diff."; |