From a6a253d10d12d3f39eb3aff87bbf8df8f1cc3f7d Mon Sep 17 00:00:00 2001
From: Kipp Cannon <kipp.cannon@ligo.org>
Date: Sat, 24 Nov 2018 11:50:18 -0800
Subject: [PATCH] snglinspiraltable: more performance work

- move more performance-critical methods into the C layer, this time the
  rich comparison methods
---
 gstlal-inspiral/python/snglinspiraltable.c  | 72 +++++++++++++++++++++
 gstlal-inspiral/python/snglinspiraltable.py | 25 -------
 2 files changed, 72 insertions(+), 25 deletions(-)

diff --git a/gstlal-inspiral/python/snglinspiraltable.c b/gstlal-inspiral/python/snglinspiraltable.c
index 96c4b512b6..7d9ce45da0 100644
--- a/gstlal-inspiral/python/snglinspiraltable.c
+++ b/gstlal-inspiral/python/snglinspiraltable.c
@@ -410,6 +410,77 @@ static struct PyMethodDef methods[] = {
 };
 
 
+/*
+ * comparison is defined specifically for the coincidence code, allowing a
+ * bisection search of a sorted trigger list to be used to identify the
+ * subset of triggers that fall within a time interval
+ */
+
+
+static PyObject *richcompare(PyObject *self, PyObject *other, int op_id)
+{
+	PyObject *converted = PyObject_CallFunctionObjArgs(LIGOTimeGPSType, other, NULL);
+	PyObject *attr;
+	PyObject *result;
+	LIGOTimeGPS t_other;
+	int cmp;
+
+	if(!converted)
+		return NULL;
+
+	attr = PyObject_GetAttrString(converted, "gpsSeconds");
+	if(!attr) {
+		Py_DECREF(converted);
+		return NULL;
+	}
+	t_other.gpsSeconds = PyInt_AsLong(attr);
+	Py_DECREF(attr);
+	attr = PyObject_GetAttrString(converted, "gpsNanoSeconds");
+	if(!attr) {
+		Py_DECREF(converted);
+		return NULL;
+	}
+	t_other.gpsNanoSeconds = PyInt_AsLong(attr);
+	Py_DECREF(attr);
+	Py_DECREF(converted);
+
+	cmp = XLALGPSCmp(&((gstlal_GSTLALSnglInspiral *) self)->row.end, &t_other);
+
+	switch(op_id) {
+	case Py_LT:
+		result = (cmp < 0) ? Py_True : Py_False;
+		break;
+
+	case Py_LE:
+		result = (cmp <= 0) ? Py_True : Py_False;
+		break;
+
+	case Py_EQ:
+		result = (cmp == 0) ? Py_True : Py_False;
+		break;
+
+	case Py_NE:
+		result = (cmp != 0) ? Py_True : Py_False;
+		break;
+
+	case Py_GE:
+		result = (cmp >= 0) ? Py_True : Py_False;
+		break;
+
+	case Py_GT:
+		result = (cmp > 0) ? Py_True : Py_False;
+		break;
+
+	default:
+		PyErr_BadInternalCall();
+		return NULL;
+	}
+
+	Py_INCREF(result);
+	return result;
+}
+
+
 /*
  * Type
  */
@@ -426,6 +497,7 @@ static PyTypeObject gstlal_GSTLALSnglInspiral_Type = {
 	.tp_name = MODULE_NAME ".GSTLALSnglInspiral",
 	.tp_new = __new__,
 	.tp_dealloc = __del__,
+	.tp_richcompare = richcompare,
 };
 
 
diff --git a/gstlal-inspiral/python/snglinspiraltable.py b/gstlal-inspiral/python/snglinspiraltable.py
index 065438fca5..0ba6151ab6 100644
--- a/gstlal-inspiral/python/snglinspiraltable.py
+++ b/gstlal-inspiral/python/snglinspiraltable.py
@@ -33,31 +33,6 @@ class GSTLALSnglInspiral(_snglinspiraltable.GSTLALSnglInspiral):
 	spin1 = lsctables.SnglInspiral.spin1
 	spin2 = lsctables.SnglInspiral.spin2
 
-	#
-	# comparison is defined specifically for the coincidence code,
-	# allowing a bisection search of a sorted trigger list to be used
-	# to identify the subset of triggers that fall within a time
-	# interval
-	#
-
-	def __eq__(self, other):
-		return self.end == other
-
-	def __ne__(self, other):
-		return self.end != other
-
-	def __lt__(self, other):
-		return self.end < other
-
-	def __le__(self, other):
-		return self.end <= other
-
-	def __gt__(self, other):
-		return self.end > other
-
-	def __ge__(self, other):
-		return self.end >= other
-
 	@property
 	def process_id(self):
 		return self.process_id_type(self._process_id)
-- 
GitLab