parsers: fix a memleak, and add a clearcaches method to the index
authorBryan O'Sullivan <bryano@fb.com>
Fri, 06 Apr 2012 00:28:36 -0700
changeset 16370 28bb4daf070c
parent 16366 913d1fa61398
child 16371 4417eb761ba8
parsers: fix a memleak, and add a clearcaches method to the index This change also fixes a nasty memory leak: previously, self->caches was not being freed. The new clearcaches method lets us benchmark with finer granularity, as it lets us separate the cost of loading a revlog index from those of populating and accessing the cache data structures.
mercurial/parsers.c
--- a/mercurial/parsers.c	Thu Apr 05 23:52:55 2012 +0900
+++ b/mercurial/parsers.c	Fri Apr 06 00:28:36 2012 -0700
@@ -412,6 +412,30 @@
 	Py_RETURN_NONE;
 }
 
+static void _index_clearcaches(indexObject *self)
+{
+	if (self->cache) {
+		Py_ssize_t i;
+
+		for (i = 0; i < self->raw_length; i++) {
+			Py_XDECREF(self->cache[i]);
+			self->cache[i] = NULL;
+		}
+		free(self->cache);
+		self->cache = NULL;
+	}
+	if (self->offsets) {
+		free(self->offsets);
+		self->offsets = NULL;
+	}
+}
+
+static PyObject *index_clearcaches(indexObject *self)
+{
+	_index_clearcaches(self);
+	Py_RETURN_NONE;
+}
+
 static int index_assign_subscript(indexObject *self, PyObject *item,
 				  PyObject *value)
 {
@@ -546,15 +570,9 @@
 
 static void index_dealloc(indexObject *self)
 {
+	_index_clearcaches(self);
 	Py_DECREF(self->data);
-	if (self->cache) {
-		Py_ssize_t i;
-
-		for (i = 0; i < self->raw_length; i++)
-			Py_XDECREF(self->cache[i]);
-	}
 	Py_XDECREF(self->added);
-	free(self->offsets);
 	PyObject_Del(self);
 }
 
@@ -572,6 +590,8 @@
 };
 
 static PyMethodDef index_methods[] = {
+	{"clearcaches", (PyCFunction)index_clearcaches, METH_NOARGS,
+	 "clear the index caches"},
 	{"insert", (PyCFunction)index_insert, METH_VARARGS,
 	 "insert an index entry"},
 	{NULL} /* Sentinel */