Tweak MemoryBuffer to allocate the class itself, the name and possibly the

buffer in the same chunk of memory.

2 less mallocs for every uninitialized MemoryBuffer and 1 less malloc for every
MemoryBuffer pointing to a memory range translate into 20% less mallocs on
clang -cc1 -Eonly Cocoa_h.m.


git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@106839 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Benjamin Kramer
2010-06-25 11:50:40 +00:00
parent 2bf4b3be5c
commit d4d1f85aa7
2 changed files with 66 additions and 79 deletions

View File

@@ -35,13 +35,11 @@ class MemoryBuffer {
const char *BufferStart; // Start of the buffer. const char *BufferStart; // Start of the buffer.
const char *BufferEnd; // End of the buffer. const char *BufferEnd; // End of the buffer.
/// MustDeleteBuffer - True if we allocated this buffer. If so, the MemoryBuffer(const MemoryBuffer &); // DO NOT IMPLEMENT
/// destructor must know the delete[] it. MemoryBuffer &operator=(const MemoryBuffer &); // DO NOT IMPLEMENT
bool MustDeleteBuffer;
protected: protected:
MemoryBuffer() : MustDeleteBuffer(false) {} MemoryBuffer() {}
void init(const char *BufStart, const char *BufEnd); void init(const char *BufStart, const char *BufEnd);
void initCopyOf(const char *BufStart, const char *BufEnd);
public: public:
virtual ~MemoryBuffer(); virtual ~MemoryBuffer();
@@ -75,20 +73,19 @@ public:
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that EndPtr[0] must be a null byte and be accessible! /// that EndPtr[0] must be a null byte and be accessible!
static MemoryBuffer *getMemBuffer(StringRef InputData, static MemoryBuffer *getMemBuffer(StringRef InputData,
const char *BufferName = ""); StringRef BufferName = "");
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
/// copying the contents and taking ownership of it. This has no requirements /// copying the contents and taking ownership of it. This has no requirements
/// on EndPtr[0]. /// on EndPtr[0].
static MemoryBuffer *getMemBufferCopy(StringRef InputData, static MemoryBuffer *getMemBufferCopy(StringRef InputData,
const char *BufferName = ""); StringRef BufferName = "");
/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
/// is completely initialized to zeros. Note that the caller should /// is completely initialized to zeros. Note that the caller should
/// initialize the memory allocated by this method. The memory is owned by /// initialize the memory allocated by this method. The memory is owned by
/// the MemoryBuffer object. /// the MemoryBuffer object.
static MemoryBuffer *getNewMemBuffer(size_t Size, static MemoryBuffer *getNewMemBuffer(size_t Size, StringRef BufferName = "");
const char *BufferName = "");
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
/// that is not initialized. Note that the caller should initialize the /// that is not initialized. Note that the caller should initialize the

View File

@@ -14,6 +14,7 @@
#include "llvm/Support/MemoryBuffer.h" #include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/OwningPtr.h" #include "llvm/ADT/OwningPtr.h"
#include "llvm/ADT/SmallString.h" #include "llvm/ADT/SmallString.h"
#include "llvm/Support/MathExtras.h"
#include "llvm/System/Errno.h" #include "llvm/System/Errno.h"
#include "llvm/System/Path.h" #include "llvm/System/Path.h"
#include "llvm/System/Process.h" #include "llvm/System/Process.h"
@@ -37,22 +38,7 @@ using namespace llvm;
// MemoryBuffer implementation itself. // MemoryBuffer implementation itself.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
MemoryBuffer::~MemoryBuffer() { MemoryBuffer::~MemoryBuffer() { }
if (MustDeleteBuffer)
free((void*)BufferStart);
}
/// initCopyOf - Initialize this source buffer with a copy of the specified
/// memory range. We make the copy so that we can null terminate it
/// successfully.
void MemoryBuffer::initCopyOf(const char *BufStart, const char *BufEnd) {
size_t Size = BufEnd-BufStart;
BufferStart = (char *)malloc(Size+1);
BufferEnd = BufferStart+Size;
memcpy(const_cast<char*>(BufferStart), BufStart, Size);
*const_cast<char*>(BufferEnd) = 0; // Null terminate buffer.
MustDeleteBuffer = true;
}
/// init - Initialize this MemoryBuffer as a reference to externally allocated /// init - Initialize this MemoryBuffer as a reference to externally allocated
/// memory, memory that we know is already null terminated. /// memory, memory that we know is already null terminated.
@@ -60,27 +46,38 @@ void MemoryBuffer::init(const char *BufStart, const char *BufEnd) {
assert(BufEnd[0] == 0 && "Buffer is not null terminated!"); assert(BufEnd[0] == 0 && "Buffer is not null terminated!");
BufferStart = BufStart; BufferStart = BufStart;
BufferEnd = BufEnd; BufferEnd = BufEnd;
MustDeleteBuffer = false;
} }
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
// MemoryBufferMem implementation. // MemoryBufferMem implementation.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
/// CopyStringRef - Copies contents of a StringRef into a block of memory and
/// null-terminates it.
static void CopyStringRef(char *Memory, StringRef Data) {
memcpy(Memory, Data.data(), Data.size());
Memory[Data.size()] = 0; // Null terminate string.
}
/// GetNamedBuffer - Allocates a new MemoryBuffer with Name copied after it.
template <typename T>
static T* GetNamedBuffer(StringRef Buffer, StringRef Name) {
char *Mem = static_cast<char*>(operator new(sizeof(T) + Name.size() + 1));
CopyStringRef(Mem + sizeof(T), Name);
return new (Mem) T(Buffer);
}
namespace { namespace {
/// MemoryBufferMem - Named MemoryBuffer pointing to a block of memory.
class MemoryBufferMem : public MemoryBuffer { class MemoryBufferMem : public MemoryBuffer {
std::string FileID;
public: public:
MemoryBufferMem(StringRef InputData, StringRef FID, bool Copy = false) MemoryBufferMem(StringRef InputData) {
: FileID(FID) { init(InputData.begin(), InputData.end());
if (!Copy)
init(InputData.data(), InputData.data()+InputData.size());
else
initCopyOf(InputData.data(), InputData.data()+InputData.size());
} }
virtual const char *getBufferIdentifier() const { virtual const char *getBufferIdentifier() const {
return FileID.c_str(); // The name is stored after the class itself.
return reinterpret_cast<const char*>(this + 1);
} }
}; };
} }
@@ -88,42 +85,55 @@ public:
/// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note /// getMemBuffer - Open the specified memory range as a MemoryBuffer. Note
/// that EndPtr[0] must be a null byte and be accessible! /// that EndPtr[0] must be a null byte and be accessible!
MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData, MemoryBuffer *MemoryBuffer::getMemBuffer(StringRef InputData,
const char *BufferName) { StringRef BufferName) {
return new MemoryBufferMem(InputData, BufferName); return GetNamedBuffer<MemoryBufferMem>(InputData, BufferName);
} }
/// getMemBufferCopy - Open the specified memory range as a MemoryBuffer, /// getMemBufferCopy - Open the specified memory range as a MemoryBuffer,
/// copying the contents and taking ownership of it. This has no requirements /// copying the contents and taking ownership of it. This has no requirements
/// on EndPtr[0]. /// on EndPtr[0].
MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData, MemoryBuffer *MemoryBuffer::getMemBufferCopy(StringRef InputData,
const char *BufferName) { StringRef BufferName) {
return new MemoryBufferMem(InputData, BufferName, true); MemoryBuffer *Buf = getNewUninitMemBuffer(InputData.size(), BufferName);
if (!Buf) return 0;
memcpy(const_cast<char*>(Buf->getBufferStart()), InputData.data(),
InputData.size());
return Buf;
} }
/// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size /// getNewUninitMemBuffer - Allocate a new MemoryBuffer of the specified size
/// that is completely initialized to zeros. Note that the caller should /// that is not initialized. Note that the caller should initialize the
/// initialize the memory allocated by this method. The memory is owned by /// memory allocated by this method. The memory is owned by the MemoryBuffer
/// the MemoryBuffer object. /// object.
MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size, MemoryBuffer *MemoryBuffer::getNewUninitMemBuffer(size_t Size,
StringRef BufferName) { StringRef BufferName) {
char *Buf = (char *)malloc(Size+1); // Allocate space for the MemoryBuffer, the data and the name. It is important
if (!Buf) return 0; // that MemoryBuffer and data are aligned so PointerIntPair works with them.
Buf[Size] = 0; size_t AlignedStringLen =
MemoryBufferMem *SB = new MemoryBufferMem(StringRef(Buf, Size), BufferName); RoundUpToAlignment(sizeof(MemoryBufferMem) + BufferName.size() + 1,
// The memory for this buffer is owned by the MemoryBuffer. sizeof(void*)); // TODO: Is sizeof(void*) enough?
SB->MustDeleteBuffer = true; size_t RealLen = AlignedStringLen + Size + 1;
return SB; char *Mem = static_cast<char*>(operator new(RealLen, std::nothrow));
if (!Mem) return 0;
// The name is stored after the class itself.
CopyStringRef(Mem + sizeof(MemoryBufferMem), BufferName);
// The buffer begins after the name and must be aligned.
char *Buf = Mem + AlignedStringLen;
Buf[Size] = 0; // Null terminate buffer.
return new (Mem) MemoryBufferMem(StringRef(Buf, Size));
} }
/// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that /// getNewMemBuffer - Allocate a new MemoryBuffer of the specified size that
/// is completely initialized to zeros. Note that the caller should /// is completely initialized to zeros. Note that the caller should
/// initialize the memory allocated by this method. The memory is owned by /// initialize the memory allocated by this method. The memory is owned by
/// the MemoryBuffer object. /// the MemoryBuffer object.
MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, MemoryBuffer *MemoryBuffer::getNewMemBuffer(size_t Size, StringRef BufferName) {
const char *BufferName) {
MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName); MemoryBuffer *SB = getNewUninitMemBuffer(Size, BufferName);
if (!SB) return 0; if (!SB) return 0;
memset(const_cast<char*>(SB->getBufferStart()), 0, Size+1); memset(const_cast<char*>(SB->getBufferStart()), 0, Size);
return SB; return SB;
} }
@@ -158,17 +168,10 @@ namespace {
/// MemoryBufferMMapFile - This represents a file that was mapped in with the /// MemoryBufferMMapFile - This represents a file that was mapped in with the
/// sys::Path::MapInFilePages method. When destroyed, it calls the /// sys::Path::MapInFilePages method. When destroyed, it calls the
/// sys::Path::UnMapFilePages method. /// sys::Path::UnMapFilePages method.
class MemoryBufferMMapFile : public MemoryBuffer { class MemoryBufferMMapFile : public MemoryBufferMem {
std::string Filename;
public: public:
MemoryBufferMMapFile(StringRef filename, const char *Pages, uint64_t Size) MemoryBufferMMapFile(StringRef Buffer)
: Filename(filename) { : MemoryBufferMem(Buffer) { }
init(Pages, Pages+Size);
}
virtual const char *getBufferIdentifier() const {
return Filename.c_str();
}
~MemoryBufferMMapFile() { ~MemoryBufferMMapFile() {
sys::Path::UnMapFilePages(getBufferStart(), getBufferSize()); sys::Path::UnMapFilePages(getBufferStart(), getBufferSize());
@@ -227,8 +230,8 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
if (FileSize >= 4096*4 && if (FileSize >= 4096*4 &&
(FileSize & (sys::Process::GetPageSize()-1)) != 0) { (FileSize & (sys::Process::GetPageSize()-1)) != 0) {
if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) { if (const char *Pages = sys::Path::MapInFilePages(FD, FileSize)) {
// Close the file descriptor, now that the whole file is in memory. return GetNamedBuffer<MemoryBufferMMapFile>(StringRef(Pages, FileSize),
return new MemoryBufferMMapFile(Filename, Pages, FileSize); Filename);
} }
} }
@@ -268,15 +271,6 @@ MemoryBuffer *MemoryBuffer::getFile(const char *Filename, std::string *ErrStr,
// MemoryBuffer::getSTDIN implementation. // MemoryBuffer::getSTDIN implementation.
//===----------------------------------------------------------------------===// //===----------------------------------------------------------------------===//
namespace {
class STDINBufferFile : public MemoryBuffer {
public:
virtual const char *getBufferIdentifier() const {
return "<stdin>";
}
};
}
MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) { MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) {
char Buffer[4096*4]; char Buffer[4096*4];
@@ -298,9 +292,5 @@ MemoryBuffer *MemoryBuffer::getSTDIN(std::string *ErrStr) {
return 0; return 0;
} }
FileData.push_back(0); // &FileData[Size] is invalid. So is &*FileData.end(). return getMemBufferCopy(StringRef(&FileData[0], FileData.size()), "<stdin>");
size_t Size = FileData.size();
MemoryBuffer *B = new STDINBufferFile();
B->initCopyOf(&FileData[0], &FileData[Size-1]);
return B;
} }