From e0f8f5bdff2e83094f9e3da26d0d1ad6fc46bf82 Mon Sep 17 00:00:00 2001 From: Ben Maurer Date: Thu, 9 May 2013 15:09:32 -0700 Subject: [PATCH] Use equals() in equals(StringData, StringData) Equals is faster in a few ways. (1) no call to memcmp unless string sizes are the same (2) does a fast check for either of the strings being static before any calls to is_numeric_string --- hphp/runtime/base/comparisons.h | 2 +- hphp/runtime/base/string_data.cpp | 17 +++++++++++++++++ hphp/runtime/base/string_data.h | 16 +--------------- 3 files changed, 19 insertions(+), 16 deletions(-) diff --git a/hphp/runtime/base/comparisons.h b/hphp/runtime/base/comparisons.h index 7e6615452..3fe127d6e 100644 --- a/hphp/runtime/base/comparisons.h +++ b/hphp/runtime/base/comparisons.h @@ -390,7 +390,7 @@ inline bool equal(const StringData *v1, const StringData *v2) { if (v1 == v2) return true; if (v1 == nullptr) return v2->empty(); if (v2 == nullptr) return v1->empty(); - return v1->compare(v2) == 0; + return v1->equal(v2); } inline bool equal(const StringData *v1, CStrRef v2) { return equal(v1, v2.get()); diff --git a/hphp/runtime/base/string_data.cpp b/hphp/runtime/base/string_data.cpp index 295b9b7e2..c48f94e46 100644 --- a/hphp/runtime/base/string_data.cpp +++ b/hphp/runtime/base/string_data.cpp @@ -855,6 +855,23 @@ DataType StringData::toNumeric(int64_t &lval, double &dval) const { /////////////////////////////////////////////////////////////////////////////// // comparisons +HOT_FUNC +bool StringData::equal(const StringData *s) const { + assert(s); + if (s == this) return true; + int ret; + + if (!(m_hash < 0 || s->m_hash < 0)) { + ret = numericCompare(s); + if (ret >= -1) { + return ret == 0; + } + } + if (m_len != s->m_len) return false; + ret = memcmp(rawdata(), s->rawdata(), m_len); + return ret == 0; +} + HOT_FUNC int StringData::numericCompare(const StringData *v2) const { assert(v2); diff --git a/hphp/runtime/base/string_data.h b/hphp/runtime/base/string_data.h index 94b1d592d..f3ea36c2d 100644 --- a/hphp/runtime/base/string_data.h +++ b/hphp/runtime/base/string_data.h @@ -322,21 +322,7 @@ public: /** * Comparisons. */ - bool equal(const StringData *s) const { - assert(s); - if (s == this) return true; - int ret; - - if (!(m_hash < 0 || s->m_hash < 0)) { - ret = numericCompare(s); - if (ret >= -1) { - return ret == 0; - } - } - if (m_len != s->m_len) return false; - ret = memcmp(rawdata(), s->rawdata(), m_len); - return ret == 0; - } + bool equal(const StringData *s) const; bool same(const StringData *s) const { assert(s);