Cache memory buffer's name length

This avoids repeated calls to strlen while we already know its value.

When preprocessing sqlite3.c, this gives a surprising 2% speedup.

Full benchmark available here:

https://llvm-compile-time-tracker.com/compare.php?from=d14c2d408dccd8c6defa7d151e9a96be3cac8cc3&to=04f0641c1cbdcd0bdbd11cd910ca6091420bf52e&stat=instructions:u

Recommit 1824432174, with restored '\0' at the end of buffer name.

Differential Revision: https://reviews.llvm.org/D138555
This commit is contained in:
serge-sans-paille 2022-11-23 07:45:59 +01:00
parent 5cb68314f3
commit 37048d04d8
No known key found for this signature in database
GPG Key ID: 7B24DA8C9551659F
1 changed files with 12 additions and 6 deletions

View File

@ -77,8 +77,10 @@ void *operator new(size_t N, const NamedBufferAlloc &Alloc) {
SmallString<256> NameBuf;
StringRef NameRef = Alloc.Name.toStringRef(NameBuf);
char *Mem = static_cast<char *>(operator new(N + NameRef.size() + 1));
CopyStringRef(Mem + N, NameRef);
char *Mem = static_cast<char *>(operator new(N + sizeof(size_t) +
NameRef.size() + 1));
*reinterpret_cast<size_t *>(Mem + N) = NameRef.size();
CopyStringRef(Mem + N + sizeof(size_t), NameRef);
return Mem;
}
@ -98,7 +100,8 @@ public:
StringRef getBufferIdentifier() const override {
// The name is stored after the class itself.
return StringRef(reinterpret_cast<const char *>(this + 1));
return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
*reinterpret_cast<const size_t *>(this + 1));
}
MemoryBuffer::BufferKind getBufferKind() const override {
@ -221,7 +224,8 @@ public:
StringRef getBufferIdentifier() const override {
// The name is stored after the class itself.
return StringRef(reinterpret_cast<const char *>(this + 1));
return StringRef(reinterpret_cast<const char *>(this + 1) + sizeof(size_t),
*reinterpret_cast<const size_t *>(this + 1));
}
MemoryBuffer::BufferKind getBufferKind() const override {
@ -301,7 +305,8 @@ WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size,
// that MemoryBuffer and data are aligned so PointerIntPair works with them.
SmallString<256> NameBuf;
StringRef NameRef = BufferName.toStringRef(NameBuf);
size_t StringLen = sizeof(MemBuffer) + NameRef.size() + 1;
size_t StringLen = sizeof(MemBuffer) + sizeof(size_t) + NameRef.size() + 1;
size_t RealLen = StringLen + Size + 1 + BufAlign.value();
if (RealLen <= Size) // Check for rollover.
return nullptr;
@ -310,7 +315,8 @@ WritableMemoryBuffer::getNewUninitMemBuffer(size_t Size,
return nullptr;
// The name is stored after the class itself.
CopyStringRef(Mem + sizeof(MemBuffer), NameRef);
*reinterpret_cast<size_t *>(Mem + sizeof(MemBuffer)) = NameRef.size();
CopyStringRef(Mem + sizeof(MemBuffer) + sizeof(size_t), NameRef);
// The buffer begins after the name and must be aligned.
char *Buf = (char *)alignAddr(Mem + StringLen, BufAlign);