# HG changeset patch # User Gregory Szorc # Date 1489089265 28800 # Node ID 08ecec297521c46822bd96042ef8678f017b4c27 # Parent b6bbfbaa205a097b3eca4955f35e912911060140 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). diff -r b6bbfbaa205a -r 08ecec297521 mercurial/bdiff_module.c --- a/mercurial/bdiff_module.c Thu Mar 16 11:17:55 2017 -0700 +++ b/mercurial/bdiff_module.c Thu Mar 09 11:54:25 2017 -0800 @@ -158,7 +158,7 @@ r = PyBytes_AsString(s); rlen = PyBytes_Size(s); - w = (char *)malloc(rlen ? rlen : 1); + w = (char *)PyMem_Malloc(rlen ? rlen : 1); if (!w) goto nomem; @@ -178,7 +178,7 @@ result = PyBytes_FromStringAndSize(w, wlen); nomem: - free(w); + PyMem_Free(w); return result ? result : PyErr_NoMemory(); }