[07/11] ir: Speed up enum comparison

Message ID 20240814125649.47119-7-dodji@redhat.com
State New
Headers
Series [01/11] Use smart pointers for variables exported from the ABI corpus |

Commit Message

Dodji Seketeli Aug. 14, 2024, 12:56 p.m. UTC
  From: Dodji Seketeli <dodji@redhat.com>

When two enums are naively equal, comparing then member-wise should be
fast.  This patch does just that.  This speeds up things a lot, in the
context of self-comparison and type canonicalization (from DWARF) when
there are huge enums in the binary.

	* src/abg-ir.cc (equals): In the overload for enums, compare
	enumerators naively for the case both enums are naively equal.

Signed-off-by: Dodji Seketeli <dodji@redhat.com>
---
 src/abg-ir.cc | 29 ++++++++++++++++++++++++++++-
 1 file changed, 28 insertions(+), 1 deletion(-)
  

Patch

diff --git a/src/abg-ir.cc b/src/abg-ir.cc
index 0b9e8d48..f15d655d 100644
--- a/src/abg-ir.cc
+++ b/src/abg-ir.cc
@@ -19997,7 +19997,34 @@  equals(const enum_type_decl& l, const enum_type_decl& r, change_kind* k)
 	ABG_RETURN_FALSE;
     }
 
-  // Now compare the enumerators.  Note that the order of declaration
+  // Now compare the enumerators.
+
+  // First in a naive (but potentially fast) way in case both enums
+  // are equal in a naive manner.
+
+  if (def1->get_enumerators().size() == def2->get_enumerators().size())
+    {
+      bool equals = true;
+      for (auto e1 = def1->get_enumerators().begin(),
+	     e2 = def2->get_enumerators().begin();
+	   (e1 != def1->get_enumerators().end()
+	    &&  e2 != def2->get_enumerators().end());
+	   ++e1, ++e2)
+	{
+	  if (*e1 != *e2)
+	    {
+	      equals = false;
+	      break;
+	    }
+	}
+      if (equals)
+	ABG_RETURN(result);
+    }
+
+  // If the two enums where not naively equals, let's try a more
+  // clever (and slow) way.
+
+  // Note that the order of declaration
   // of enumerators should not matter in the comparison.
   //
   // Also if an enumerator value is redundant, that shouldn't impact