name-component: Component::compare performance improvement

refs #3807

Change-Id: I2ccdad9806d29f641db71127d9459581d93a148f
diff --git a/src/name-component.cpp b/src/name-component.cpp
index 8259dd9..b491f2e 100644
--- a/src/name-component.cpp
+++ b/src/name-component.cpp
@@ -394,6 +394,14 @@
 int
 Component::compare(const Component& other) const
 {
+  if (this->hasWire() && other.hasWire()) {
+    // In the common case where both components have wire encoding,
+    // it's more efficient to simply compare the wire encoding.
+    // This works because lexical order of TLV encoding happens to be
+    // the same as canonical order of the value.
+    return std::memcmp(wire(), other.wire(), std::min(size(), other.size()));
+  }
+
   int cmpType = type() - other.type();
   if (cmpType != 0)
     return cmpType;