Skip to content

Commit 5e0125b

Browse files
authored
Merge pull request #69334 from mikeash/metadata-cache-key-equality-memcmp
[Runtime] Have MetadataCacheKey::operator== try memcmp before deeper comparison.
2 parents 0974cf3 + fc6cfbb commit 5e0125b

File tree

1 file changed

+18
-2
lines changed

1 file changed

+18
-2
lines changed

stdlib/public/runtime/MetadataCache.h

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -606,12 +606,28 @@ class MetadataCacheKey {
606606
// Compare the hashes.
607607
if (hash() != rhs.hash()) return false;
608608

609+
// Fast path the case where they're bytewise identical. That's nearly always
610+
// the case if the hashes are the same, and we can skip the slower deep
611+
// comparison.
612+
auto *adata = begin();
613+
auto *bdata = rhs.begin();
614+
615+
auto asize = (uintptr_t)end() - (uintptr_t)adata;
616+
auto bsize = (uintptr_t)rhs.end() - (uintptr_t)bdata;
617+
618+
// If sizes don't match, they can never be equal.
619+
if (asize != bsize)
620+
return false;
621+
622+
// If sizes match, see if the bytes match. If they do, then the contents
623+
// must necessarily match. Otherwise do a deep comparison.
624+
if (memcmp(adata, bdata, asize) == 0)
625+
return true;
626+
609627
// Compare the layouts.
610628
if (Layout != rhs.Layout) return false;
611629

612630
// Compare the content.
613-
auto *adata = begin();
614-
auto *bdata = rhs.begin();
615631
const uintptr_t *packCounts = reinterpret_cast<const uintptr_t *>(adata);
616632

617633
unsigned argIdx = 0;

0 commit comments

Comments
 (0)