diff options
author | serge-sans-paille <sguelton@mozilla.com> | 2022-11-23 07:45:59 +0100 |
---|---|---|
committer | serge-sans-paille <sguelton@mozilla.com> | 2022-11-29 14:01:06 +0100 |
commit | 1824432174b3166b40bce59477beb5821170748e (patch) | |
tree | a4813c65f8dd7f9909b289eaabce0446be44b85c /llvm/lib/Support/MemoryBuffer.cpp | |
parent | c64359ea4c8ee9830dc828ed26d862a763f4535d (diff) | |
download | llvm-1824432174b3166b40bce59477beb5821170748e.zip llvm-1824432174b3166b40bce59477beb5821170748e.tar.gz llvm-1824432174b3166b40bce59477beb5821170748e.tar.bz2 |
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=5279e6a7d677cdf4488883b77aacab911318100c&to=389601b0dbdf23cf25167ddfc49b3af5742ebd9a&stat=instructions:u
Differential Revision: https://reviews.llvm.org/D138555
Diffstat (limited to 'llvm/lib/Support/MemoryBuffer.cpp')
-rw-r--r-- | llvm/lib/Support/MemoryBuffer.cpp | 25 |
1 files changed, 15 insertions, 10 deletions
diff --git a/llvm/lib/Support/MemoryBuffer.cpp b/llvm/lib/Support/MemoryBuffer.cpp index 6bb046e..8f123a4 100644 --- a/llvm/lib/Support/MemoryBuffer.cpp +++ b/llvm/lib/Support/MemoryBuffer.cpp @@ -58,12 +58,10 @@ void MemoryBuffer::init(const char *BufStart, const char *BufEnd, // MemoryBufferMem implementation. //===----------------------------------------------------------------------===// -/// CopyStringRef - Copies contents of a StringRef into a block of memory and -/// null-terminates it. +/// CopyStringRef - Copies contents of a StringRef into a block of memory. static void CopyStringRef(char *Memory, StringRef Data) { if (!Data.empty()) memcpy(Memory, Data.data(), Data.size()); - Memory[Data.size()] = 0; // Null terminate string. } namespace { @@ -77,8 +75,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())); + *reinterpret_cast<size_t *>(Mem + N) = NameRef.size(); + CopyStringRef(Mem + N + sizeof(size_t), NameRef); return Mem; } @@ -98,7 +98,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 +222,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 +303,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(); size_t RealLen = StringLen + Size + 1 + BufAlign.value(); if (RealLen <= Size) // Check for rollover. return nullptr; @@ -310,13 +313,15 @@ 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(); // Null terminate buffer. + 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); - Buf[Size] = 0; // Null terminate buffer. auto *Ret = new (Mem) MemBuffer(StringRef(Buf, Size), true); + Buf[Size] = 0; // Null terminate buffer. return std::unique_ptr<WritableMemoryBuffer>(Ret); } |