From 92c39cca42c9635090cd31d226d98b941b379b92 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Mon, 14 Mar 2022 15:26:19 +0100 Subject: [PATCH 1/8] fix implicit cast warnings in a header --- src/common/Serialize.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/common/Serialize.h b/src/common/Serialize.h index 6e71639e76..958be46ff3 100644 --- a/src/common/Serialize.h +++ b/src/common/Serialize.h @@ -80,7 +80,7 @@ namespace Util { { if (size > std::numeric_limits::max()) Sys::Drop("IPC: Size out of range in message"); - Write(size); + Write(static_cast( size )); } template void Write(Arg&& value) { @@ -247,7 +247,7 @@ namespace Util { struct SerializeTraits { static void Write(Writer& stream, bool value) { - stream.Write(+value); + stream.Write(static_cast(value)); } static bool Read(Reader& stream) { From 015c9bd9ff9dc375311c0437835e273676a02919 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sat, 9 Nov 2024 23:49:54 +0100 Subject: [PATCH 2/8] improve SendMsg performances by avoiding malloc calls Measurements done by instrumenting the code shows a ~17% performance improvement on that function. time calls time/calls patched 209635243 12242 17124,264 not patched 167909532 8198 20481,768 --- src/common/IPC/Primitives.cpp | 8 ++++++-- 1 file changed, 6 insertions(+), 2 deletions(-) diff --git a/src/common/IPC/Primitives.cpp b/src/common/IPC/Primitives.cpp index 14bdd8c339..79a16b46dd 100644 --- a/src/common/IPC/Primitives.cpp +++ b/src/common/IPC/Primitives.cpp @@ -234,7 +234,8 @@ static void InternalSendMsg(Sys::OSHandle handle, bool more, const FileDesc* han } #else size_t descBytes = 0; - std::unique_ptr descBuffer; + static std::unique_ptr descBuffer; + static size_t descBufferSize = 0; if (numHandles != 0) { for (size_t i = 0; i < numHandles; i++) { // tag: 1 byte @@ -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; From 31434fee1dc697cb1274e038e20348d215a22711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sun, 10 Nov 2024 01:40:06 +0100 Subject: [PATCH 3/8] InternalRecvMsg reserves before pushing --- src/common/IPC/Primitives.cpp | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/common/IPC/Primitives.cpp b/src/common/IPC/Primitives.cpp index 79a16b46dd..67f4a264ea 100644 --- a/src/common/IPC/Primitives.cpp +++ b/src/common/IPC/Primitives.cpp @@ -365,6 +365,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(); From 0c9487e4f8881f8ddaf5769129a68a526b7f4354 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sun, 10 Nov 2024 02:13:31 +0100 Subject: [PATCH 4/8] recvmsg: use memset instead of for-loop --- src/common/IPC/Primitives.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/src/common/IPC/Primitives.cpp b/src/common/IPC/Primitives.cpp index 67f4a264ea..51580f4361 100644 --- a/src/common/IPC/Primitives.cpp +++ b/src/common/IPC/Primitives.cpp @@ -343,8 +343,7 @@ bool InternalRecvMsg(Sys::OSHandle handle, Util::Reader& reader) recvBuffer.reset(new char[NACL_ABI_IMC_BYTES_MAX]); } - for (size_t i = 0; i < NACL_ABI_IMC_DESC_MAX; i++) - h[i] = NACL_INVALID_HANDLE; + memset( h, 0xff, sizeof(h) ); #ifdef __native_client__ hdr.iov = iov; From ebefb2571e52ffbf09a83e111dd88dcc0f6c1f22 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sun, 10 Nov 2024 02:13:57 +0100 Subject: [PATCH 5/8] recvmsg: use std::fill instead of memset (seems faster?) --- src/common/IPC/Primitives.cpp | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/common/IPC/Primitives.cpp b/src/common/IPC/Primitives.cpp index 51580f4361..a137078814 100644 --- a/src/common/IPC/Primitives.cpp +++ b/src/common/IPC/Primitives.cpp @@ -343,7 +343,7 @@ bool InternalRecvMsg(Sys::OSHandle handle, Util::Reader& reader) recvBuffer.reset(new char[NACL_ABI_IMC_BYTES_MAX]); } - memset( h, 0xff, sizeof(h) ); + std::fill(std::begin(h), std::end(h),NACL_INVALID_HANDLE) #ifdef __native_client__ hdr.iov = iov; From 33c2e436fa7b64643c463eebeb7368f91fd0e422 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sun, 10 Nov 2024 02:14:23 +0100 Subject: [PATCH 6/8] recvmsg: use malloc instead of new, reduces a tiny bit time spent there --- src/common/IPC/Primitives.cpp | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/common/IPC/Primitives.cpp b/src/common/IPC/Primitives.cpp index a137078814..bdc74e3193 100644 --- a/src/common/IPC/Primitives.cpp +++ b/src/common/IPC/Primitives.cpp @@ -340,7 +340,12 @@ 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( malloc( NACL_ABI_IMC_BYTES_MAX ) ) ); } std::fill(std::begin(h), std::end(h),NACL_INVALID_HANDLE) From e5d390db1bcd7b0ae625bc1685b1ce6ccd513280 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Mon, 13 Jan 2025 17:21:50 +0100 Subject: [PATCH 7/8] minor perf patch for IPC Reader --- src/common/Serialize.h | 10 ++++++++-- 1 file changed, 8 insertions(+), 2 deletions(-) diff --git a/src/common/Serialize.h b/src/common/Serialize.h index 958be46ff3..3ef6f0389c 100644 --- a/src/common/Serialize.h +++ b/src/common/Serialize.h @@ -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); @@ -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) From 65ab193b9a464a96e486b5d1c7225e50c36b68d2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Morel=20B=C3=A9renger?= Date: Sun, 24 Nov 2024 20:36:42 +0100 Subject: [PATCH 8/8] use final keyword in some places --- src/engine/renderer/tr_local.h | 40 +++++++++++++++++----------------- 1 file changed, 20 insertions(+), 20 deletions(-) diff --git a/src/engine/renderer/tr_local.h b/src/engine/renderer/tr_local.h index eab633f98f..cb9619f3e3 100644 --- a/src/engine/renderer/tr_local.h +++ b/src/engine/renderer/tr_local.h @@ -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 { @@ -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; @@ -3899,7 +3899,7 @@ 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; @@ -3907,22 +3907,22 @@ void GLimp_LogComment_( std::string comment ); 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; @@ -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; @@ -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; @@ -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; };