More optimization for Message2. It now uses a more lightweight BSimpleMallocIO instead of the full blown BMallocIO. This wastes less memory and reduces unnecessary overhead when unflattening.

git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@13861 a95241bf-73f2-0310-859d-f6bbb57e9c96
This commit is contained in:
Michael Lotz
2005-07-31 11:48:38 +00:00
parent 4835f2d292
commit 43abf8a345
7 changed files with 197 additions and 126 deletions
+4 -2
View File
@@ -43,7 +43,7 @@ public:
status_t Flatten(BDataIO *stream) const;
status_t Unflatten(BDataIO *stream);
status_t AddData(const char *name, BMallocIO *buffer,
status_t AddData(const char *name, BSimpleMallocIO *buffer,
type_code type);
status_t RemoveData(const char *name, int32 index = 0);
@@ -52,7 +52,7 @@ public:
int32 index, const void **data,
ssize_t *numBytes) const;
status_t ReplaceData(const char *name, int32 index,
BMallocIO *buffer, type_code type);
BSimpleMallocIO *buffer, type_code type);
status_t Rename(const char *oldName, const char *newName);
status_t RemoveName(const char *name);
@@ -77,6 +77,8 @@ private:
BList fFieldList;
BMessageField **fFieldTable;
int32 fFieldTableSize;
mutable ssize_t fFlattenedSize;
};
} // namespace BPrivate
+5 -4
View File
@@ -25,6 +25,8 @@
namespace BPrivate {
class BSimpleMallocIO;
class BMessageField {
public:
BMessageField();
@@ -41,10 +43,9 @@ public:
uint8 NameLength() const { return fName.Length(); };
type_code Type() const { return fType; };
void AddItem(BMallocIO *item);
void ReplaceItem(int32 index, BMallocIO *item,
bool deleteOld = true);
void RemoveItem(int32 index, bool deleteIt = true);
void AddItem(BSimpleMallocIO *item);
void ReplaceItem(int32 index, BSimpleMallocIO *item);
void RemoveItem(int32 index);
int32 CountItems() const { return fItems.CountItems(); };
size_t SizeAt(int32 index) const;
const void *BufferAt(int32 index) const;
+2 -3
View File
@@ -116,15 +116,14 @@ public:
if (fError < B_OK)
throw fError;
if (IsSwapping()) {
if (IsSwapping())
byte_swap(data);
}
}
template<class T>
inline void operator()(T data, size_t len)
{
fError = fStream->Read((void*)data, len);
fError = fStream->Read((void *)data, len);
if (fError < B_OK)
throw fError;
}
+88
View File
@@ -0,0 +1,88 @@
/*
* Copyright 2005, Haiku.
* Distributed under the terms of the MIT License.
*
* Authors:
* Michael Lotz <[email protected]>
*/
/* A BMallocIO similar structure but with less overhead */
#ifndef _SIMPLE_MALLOC_IO_H_
#define _SIMPLE_MALLOC_IO_H_
#include <malloc.h>
namespace BPrivate {
class BSimpleMallocIO {
public:
BSimpleMallocIO(size_t size)
: fSize(size)
{
fBuffer = (char *)malloc(size);
}
~BSimpleMallocIO()
{
free(fBuffer);
}
void Read(void *buffer)
{
memcpy(buffer, fBuffer, fSize);
}
void Read(void *buffer, size_t size)
{
memcpy(buffer, fBuffer, size);
}
void ReadAt(off_t pos, void *buffer, size_t size)
{
memcpy(buffer, fBuffer + pos, size);
}
void Write(const void *buffer)
{
memcpy(fBuffer, buffer, fSize);
}
void Write(const void *buffer, size_t size)
{
memcpy(fBuffer, buffer, size);
}
void WriteAt(off_t pos, const void *buffer, size_t size)
{
memcpy(fBuffer + pos, buffer, size);
}
status_t SetSize(off_t size)
{
fBuffer = (char *)realloc(fBuffer, size);
if (!fBuffer)
return B_NO_MEMORY;
fSize = size;
return B_OK;
}
char *Buffer()
{
return fBuffer;
}
size_t BufferLength()
{
return fSize;
}
private:
char *fBuffer;
size_t fSize;
};
} // namespace BPivate
#endif // _SIMPLE_MALLOC_IO_H_