Skip to content

IPC perf improvements by freem #1632

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 15 additions & 5 deletions src/common/IPC/Primitives.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,8 @@ static void InternalSendMsg(Sys::OSHandle handle, bool more, const FileDesc* han
}
#else
size_t descBytes = 0;
std::unique_ptr<unsigned char[]> descBuffer;
static std::unique_ptr<unsigned char[]> descBuffer;
static size_t descBufferSize = 0;
if (numHandles != 0) {
for (size_t i = 0; i < numHandles; i++) {
// tag: 1 byte
Expand All @@ -250,7 +251,10 @@ static void InternalSendMsg(Sys::OSHandle handle, bool more, const FileDesc* han
// Add 1 byte end tag and round to 16 bytes
descBytes = (descBytes + 1 + 0xf) & ~0xf;

descBuffer.reset(new unsigned char[descBytes]);
if (descBufferSize < descBytes) {
descBufferSize = descBytes;
descBuffer.reset(new unsigned char[descBytes]);
}
unsigned char* descBuffer_ptr = &descBuffer[0];
for (size_t i = 0; i < numHandles; i++) {
*descBuffer_ptr++ = handles[i].type;
Expand Down Expand Up @@ -336,11 +340,15 @@ bool InternalRecvMsg(Sys::OSHandle handle, Util::Reader& reader)
NaClIOVec iov[2];
NaClHandle h[NACL_ABI_IMC_DESC_MAX];
if (!recvBuffer) {
recvBuffer.reset(new char[NACL_ABI_IMC_BYTES_MAX]);
//while using malloc in combination with delete[] or delete is
//a bad idea in theory, in this case, we are freeing an array of
//bytes, there is NOTHING special to clean in a string of bytes.
//This does, however, avoid new[] or new to initilialise data,
//those functions being actually closer to calloc than malloc.
recvBuffer.reset( static_cast<char*>( malloc( NACL_ABI_IMC_BYTES_MAX ) ) );
}

for (size_t i = 0; i < NACL_ABI_IMC_DESC_MAX; i++)
h[i] = NACL_INVALID_HANDLE;
std::fill(std::begin(h), std::end(h),NACL_INVALID_HANDLE)

#ifdef __native_client__
hdr.iov = iov;
Expand All @@ -361,6 +369,8 @@ bool InternalRecvMsg(Sys::OSHandle handle, Util::Reader& reader)
if (hdr.flags & (NACL_MESSAGE_TRUNCATED | NACL_HANDLES_TRUNCATED))
Sys::Drop("IPC: Recieved truncated message");

// NACL_ABI_IMC_DESC_MAX == 8 anyway, so let's avoid useless loops and reallocs
reader.GetHandles().reserve(reader.GetHandles().size() + NACL_ABI_IMC_DESC_MAX);
for (size_t i = 0; i < NACL_ABI_IMC_DESC_MAX; i++) {
if (h[i] != NACL_INVALID_HANDLE) {
reader.GetHandles().emplace_back();
Expand Down
14 changes: 10 additions & 4 deletions src/common/Serialize.h
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ namespace Util {
{
if (size > std::numeric_limits<uint32_t>::max())
Sys::Drop("IPC: Size out of range in message");
Write<uint32_t>(size);
Write<uint32_t>(static_cast<uint32_t>( size ));
}
template<typename T, typename Arg> void Write(Arg&& value)
{
Expand Down Expand Up @@ -130,10 +130,16 @@ namespace Util {
public:
Reader()
: pos(0), handles_pos(0) {}
Reader(Reader const& other) = delete;
Reader& operator=(Reader const& other) = delete;
Reader(Reader&& other) NOEXCEPT
: data(std::move(other.data)), handles(std::move(other.handles)), pos(other.pos), handles_pos(other.handles_pos) {}
Reader& operator=(Reader&& other) NOEXCEPT
{
if (this == &other)
{
return *this;
}
std::swap(data, other.data);
std::swap(handles, other.handles);
std::swap(pos, other.pos);
Expand All @@ -143,8 +149,8 @@ namespace Util {
~Reader()
{
// Close any handles that weren't read
for (size_t i = handles_pos; i < handles.size(); i++)
handles[i].Close();
for (auto it = handles.begin() + handles_pos; it != handles.end(); ++it)
it->Close();
}

void ReadData(void* p, size_t len)
Expand Down Expand Up @@ -247,7 +253,7 @@ namespace Util {
struct SerializeTraits<bool> {
static void Write(Writer& stream, bool value)
{
stream.Write<uint8_t>(+value);
stream.Write<uint8_t>(static_cast<uint8_t>(value));
}
static bool Read(Reader& stream)
{
Expand Down
40 changes: 20 additions & 20 deletions src/engine/renderer/tr_local.h
Original file line number Diff line number Diff line change
Expand Up @@ -3843,23 +3843,23 @@ void GLimp_LogComment_( std::string comment );
virtual const RenderCommand *ExecuteSelf() const = 0;
};

struct SetColorCommand : public RenderCommand {
struct SetColorCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

Color::Color color;
};
struct SetColorGradingCommand : public RenderCommand {
struct SetColorGradingCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

image_t *image;
int slot;
};
struct DrawBufferCommand : public RenderCommand {
struct DrawBufferCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

int buffer;
};
struct SwapBuffersCommand : public RenderCommand {
struct SwapBuffersCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;
};
struct StretchPicCommand : public RenderCommand {
Expand All @@ -3871,25 +3871,25 @@ void GLimp_LogComment_( std::string comment );
float s1, t1;
float s2, t2;
};
struct RotatedPicCommand : public StretchPicCommand {
struct RotatedPicCommand final : public StretchPicCommand {
const RenderCommand *ExecuteSelf() const override;

float angle;
};
struct GradientPicCommand : public StretchPicCommand {
struct GradientPicCommand final : public StretchPicCommand {
const RenderCommand *ExecuteSelf() const override;

Color::Color32Bit gradientColor; // color values 0-255
int gradientType;
};
struct Poly2dCommand : public RenderCommand {
struct Poly2dCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

polyVert_t *verts;
int numverts;
shader_t *shader;
};
struct Poly2dIndexedCommand : public RenderCommand {
struct Poly2dIndexedCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

polyVert_t *verts;
Expand All @@ -3899,30 +3899,30 @@ void GLimp_LogComment_( std::string comment );
shader_t *shader;
int translation[2];
};
struct ScissorSetCommand : public RenderCommand {
struct ScissorSetCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

int x;
int y;
int w;
int h;
};
struct SetMatrixTransformCommand : public RenderCommand {
struct SetMatrixTransformCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

matrix_t matrix;
};
struct ResetMatrixTransformCommand : public RenderCommand {
struct ResetMatrixTransformCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;
};
struct DrawViewCommand : public RenderCommand {
struct DrawViewCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
viewParms_t viewParms;
bool depthPass;
};
struct SetupLightsCommand : public RenderCommand {
struct SetupLightsCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
Expand All @@ -3933,7 +3933,7 @@ void GLimp_LogComment_( std::string comment );
SSF_JPEG,
SSF_PNG
};
struct ScreenshotCommand : public RenderCommand {
struct ScreenshotCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

int x;
Expand All @@ -3943,7 +3943,7 @@ void GLimp_LogComment_( std::string comment );
char fileName[MAX_OSPATH];
ssFormat_t format;
};
struct VideoFrameCommand : public RenderCommand {
struct VideoFrameCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

int width;
Expand All @@ -3952,33 +3952,33 @@ void GLimp_LogComment_( std::string comment );
byte *encodeBuffer;
bool motionJpeg;
};
struct RenderPostProcessCommand : public RenderCommand {
struct RenderPostProcessCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
viewParms_t viewParms;
};
struct ClearBufferCommand : public RenderCommand {
struct ClearBufferCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
viewParms_t viewParms;
};
struct PreparePortalCommand : public RenderCommand {
struct PreparePortalCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
viewParms_t viewParms;
drawSurf_t *surface;
};
struct FinalisePortalCommand : public RenderCommand {
struct FinalisePortalCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;

trRefdef_t refdef;
viewParms_t viewParms;
drawSurf_t *surface;
};
struct EndOfListCommand : public RenderCommand {
struct EndOfListCommand final : public RenderCommand {
const RenderCommand *ExecuteSelf() const override;
};

Expand Down
Loading