comphelper/source/streaming/memorystream.cxx |   21 ++++++++++++++++-----
 1 file changed, 16 insertions(+), 5 deletions(-)

New commits:
commit 826b536fddfebf9e19efae9dbf3dbd86861c6d74
Author:     Noel Grandin <noel.gran...@collabora.co.uk>
AuthorDate: Fri May 20 12:52:13 2022 +0200
Commit:     Noel Grandin <noel.gran...@collabora.co.uk>
CommitDate: Fri May 20 20:55:54 2022 +0200

    don't waste time on memset when we're just going to overwrite it
    
    Change-Id: Id01b056a28e23c55757b0b4e08a5901c76821b6b
    Reviewed-on: https://gerrit.libreoffice.org/c/core/+/134677
    Tested-by: Jenkins
    Reviewed-by: Noel Grandin <noel.gran...@collabora.co.uk>

diff --git a/comphelper/source/streaming/memorystream.cxx 
b/comphelper/source/streaming/memorystream.cxx
index a7612bf67b07..2abcb6b1209f 100644
--- a/comphelper/source/streaming/memorystream.cxx
+++ b/comphelper/source/streaming/memorystream.cxx
@@ -82,7 +82,18 @@ public:
     virtual void SAL_CALL truncate() override;
 
 private:
-    std::vector< sal_Int8 > maData;
+    // prevents std::vector from wasting time doing memset on data we are 
going to overwrite anyway
+    struct NoInitInt8
+    {
+        sal_Int8 value;
+        NoInitInt8() noexcept {
+            // do nothing
+            static_assert(sizeof(NoInitInt8) == sizeof(sal_Int8), "invalid 
size");
+            static_assert(alignof(NoInitInt8) == alignof(sal_Int8), "invalid 
alignment");
+        }
+    };
+
+    std::vector< NoInitInt8 > maData;
     sal_Int32 mnCursor;
 };
 
@@ -132,8 +143,8 @@ sal_Int32 SAL_CALL UNOMemoryStream::readBytes( Sequence< 
sal_Int8 >& aData, sal_
 
     if( nBytesToRead )
     {
-        sal_Int8* pData = &(*maData.begin());
-        sal_Int8* pCursor = &(pData[mnCursor]);
+        NoInitInt8* pData = &(*maData.begin());
+        NoInitInt8* pCursor = &(pData[mnCursor]);
         memcpy( static_cast<void*>(aData.getArray()), 
static_cast<void*>(pCursor), nBytesToRead );
 
         mnCursor += nBytesToRead;
@@ -205,8 +216,8 @@ void SAL_CALL UNOMemoryStream::writeBytes( const Sequence< 
sal_Int8 >& aData )
     if( static_cast< sal_Int32 >( nNewSize ) > static_cast< sal_Int32 >( 
maData.size() ) )
         maData.resize( nNewSize );
 
-    sal_Int8* pData = &(*maData.begin());
-    sal_Int8* pCursor = &(pData[mnCursor]);
+    NoInitInt8* pData = &(*maData.begin());
+    NoInitInt8* pCursor = &(pData[mnCursor]);
     memcpy( pCursor, aData.getConstArray(), nBytesToWrite );
 
     mnCursor += nBytesToWrite;

Reply via email to