bdiff: use Python memory allocator in fixws
authorGregory Szorc <gregory.szorc@gmail.com>
Thu, 09 Mar 2017 11:54:25 -0800
changeset 31467 08ecec297521
parent 31466 b6bbfbaa205a
child 31468 b9dd03ed564f
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).
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();
 }