Skip to content

Commit e24b433

Browse files
committed
String compare optimization
1 parent c36716e commit e24b433

File tree

1 file changed

+10
-2
lines changed

1 file changed

+10
-2
lines changed

stdlib/public/runtime/MetadataLookup.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@
3939
#include <vector>
4040
#include <list>
4141
#include <new>
42+
#include <cstring>
4243

4344
using namespace swift;
4445
using namespace Demangle;
@@ -472,6 +473,13 @@ static bool sameObjCTypeManglings(Demangle::NodePointer node1,
472473
}
473474
#endif
474475

476+
/// Optimization for the case where we need to compare a StringRef and a null terminated C string
477+
/// Not converting s2 to a StringRef avoids the need to call both strlen and memcmp
478+
static bool stringRefEqualsCString(StringRef s1, const char *s2) {
479+
size_t length = s1.size();
480+
return strncmp(s1.data(), s2, length) == 0 && s2[length] == '\0';
481+
}
482+
475483
bool
476484
swift::_contextDescriptorMatchesMangling(const ContextDescriptor *context,
477485
Demangle::NodePointer node) {
@@ -496,7 +504,7 @@ swift::_contextDescriptorMatchesMangling(const ContextDescriptor *context,
496504
// Match to a mangled module name.
497505
if (node->getKind() != Demangle::Node::Kind::Module)
498506
return false;
499-
if (!node->getText().equals(module->Name.get()))
507+
if (!stringRefEqualsCString(node->getText(), module->Name.get()))
500508
return false;
501509

502510
node = nullptr;
@@ -568,7 +576,7 @@ swift::_contextDescriptorMatchesMangling(const ContextDescriptor *context,
568576
auto nameNode = node->getChild(1);
569577
if (nameNode->getKind() != Demangle::Node::Kind::Identifier)
570578
return false;
571-
if (nameNode->getText() == proto->Name.get()) {
579+
if (stringRefEqualsCString(nameNode->getText(), proto->Name.get())) {
572580
node = node->getChild(0);
573581
break;
574582
}

0 commit comments

Comments
 (0)