This is an automated email from the ASF dual-hosted git repository.
swebb2066 pushed a commit to branch reduce_decoding_overhead
in repository https://gitbox.apache.org/repos/asf/logging-log4cxx.git
The following commit(s) were added to refs/heads/reduce_decoding_overhead by
this push:
new 903356ef Avoid dynamic memory allocation in Transcoder::decode
903356ef is described below
commit 903356efb633fbb841365c1acf8e18ce0c6db9d2
Author: Stephen Webb <[email protected]>
AuthorDate: Fri Sep 4 12:55:40 2026 +1000
Avoid dynamic memory allocation in Transcoder::decode
---
src/main/cpp/bytebuffer.cpp | 2 +-
src/main/cpp/charsetdecoder.cpp | 5 ++--
src/main/cpp/transcoder.cpp | 4 ++--
src/main/include/log4cxx/helpers/bytebuffer.h | 3 ++-
src/main/include/log4cxx/helpers/charsetdecoder.h | 4 ++--
src/main/include/log4cxx/private/bytebuffer_priv.h | 27 +++++++++++-----------
6 files changed, 23 insertions(+), 22 deletions(-)
diff --git a/src/main/cpp/bytebuffer.cpp b/src/main/cpp/bytebuffer.cpp
index 40aa4f53..8890dd45 100644
--- a/src/main/cpp/bytebuffer.cpp
+++ b/src/main/cpp/bytebuffer.cpp
@@ -33,7 +33,7 @@ ByteBuffer::~ByteBuffer()
{
}
-ByteBuffer::ByteBufferPriv& ByteBuffer::impl()
+ByteBufferPriv& ByteBuffer::impl()
{
return *m_priv;
}
diff --git a/src/main/cpp/charsetdecoder.cpp b/src/main/cpp/charsetdecoder.cpp
index 76ab9550..3c2541e5 100644
--- a/src/main/cpp/charsetdecoder.cpp
+++ b/src/main/cpp/charsetdecoder.cpp
@@ -325,7 +325,7 @@ class UTF8CharsetDecoder : public CharsetDecoder
auto availableByteCount = in.remaining();
while (0 < availableByteCount)
{
- auto sv = getUTF8CodePoint(in_param);
+ auto sv = getUTF8CodePoint(in);
auto nextAvailableByteCount = in.remaining();
if (sv == 0xFFFF || nextAvailableByteCount ==
availableByteCount)
return APR_BADCH;
@@ -601,9 +601,8 @@ log4cxx_status_t CharsetDecoder::decode(const char* in,
size_t maxByteCount, Log
return decode(buf, out);
}
-unsigned int CharsetDecoder::getUTF8CodePoint(ByteBuffer& in_param)
+unsigned int CharsetDecoder::getUTF8CodePoint(ByteBufferPriv& in)
{
- auto& in = in_param.impl();
auto availableByteCount = in.remaining();
if (0 == availableByteCount)
return 0xFFFF;
diff --git a/src/main/cpp/transcoder.cpp b/src/main/cpp/transcoder.cpp
index bf71a302..e60e26e4 100644
--- a/src/main/cpp/transcoder.cpp
+++ b/src/main/cpp/transcoder.cpp
@@ -20,7 +20,7 @@
#include <log4cxx/helpers/pool.h>
#include <stdlib.h>
#include <log4cxx/helpers/exception.h>
-#include <log4cxx/helpers/bytebuffer.h>
+#include <log4cxx/private/bytebuffer_priv.h>
#include <log4cxx/helpers/charsetdecoder.h>
#include <log4cxx/helpers/charsetencoder.h>
#include <log4cxx/helpers/stringhelper.h>
@@ -191,7 +191,7 @@ unsigned int Transcoder::decode(const std::string& src,
{
auto offset = iter - src.begin();
auto remaining = src.size() - offset;
- ByteBuffer buf(const_cast<char*>(src.data() + offset), remaining);
+ ByteBufferPriv buf(const_cast<char*>(src.data() + offset), remaining);
auto result = CharsetDecoder::getUTF8CodePoint(buf);
iter += remaining - buf.remaining();
return result;
diff --git a/src/main/include/log4cxx/helpers/bytebuffer.h
b/src/main/include/log4cxx/helpers/bytebuffer.h
index e4ea054a..670c47c2 100644
--- a/src/main/include/log4cxx/helpers/bytebuffer.h
+++ b/src/main/include/log4cxx/helpers/bytebuffer.h
@@ -26,6 +26,7 @@ namespace LOG4CXX_NS
namespace helpers
{
+struct ByteBufferPriv;
/**
* An area of memory and a cursor into that memory.
@@ -40,7 +41,7 @@ namespace helpers
class LOG4CXX_EXPORT ByteBuffer
{
private:
- LOG4CXX_DECLARE_PRIVATE_MEMBER_PTR(ByteBufferPriv, m_priv)
+ LOG4CXX_DECLARE_PRIVATE_MEMBER(std::unique_ptr<ByteBufferPriv>,
m_priv);
public:
/// A \c capacity sized area of memory at \c data.
diff --git a/src/main/include/log4cxx/helpers/charsetdecoder.h
b/src/main/include/log4cxx/helpers/charsetdecoder.h
index 71938e84..5a9af190 100644
--- a/src/main/include/log4cxx/helpers/charsetdecoder.h
+++ b/src/main/include/log4cxx/helpers/charsetdecoder.h
@@ -27,7 +27,7 @@ namespace helpers
class CharsetDecoder;
LOG4CXX_PTR_DEF(CharsetDecoder);
class ByteBuffer;
-
+struct ByteBufferPriv;
/**
* An abstract engine to transform a sequences of bytes in a specific charset
@@ -109,7 +109,7 @@ class LOG4CXX_EXPORT CharsetDecoder : public Object
* @param buf the bytes to decode.
* @return the code point value, if successful, otherwise
0xFFFF. The \c buf cursor position is only incremented if successful.
*/
- static unsigned int getUTF8CodePoint(ByteBuffer& buf);
+ static unsigned int getUTF8CodePoint(ByteBufferPriv& buf);
private:
/**
diff --git a/src/main/include/log4cxx/private/bytebuffer_priv.h
b/src/main/include/log4cxx/private/bytebuffer_priv.h
index 65625417..93ec2a1e 100644
--- a/src/main/include/log4cxx/private/bytebuffer_priv.h
+++ b/src/main/include/log4cxx/private/bytebuffer_priv.h
@@ -15,11 +15,12 @@
* limitations under the License.
*/
#include <log4cxx/helpers/bytebuffer.h>
+#include <cstring> // memmove
using namespace LOG4CXX_NS;
using namespace LOG4CXX_NS::helpers;
-struct ByteBuffer::ByteBufferPriv
+struct LOG4CXX_NS::helpers::ByteBufferPriv
{
private: // Attributes
char* base;
@@ -56,13 +57,13 @@ public: // Modifiers
#endif
};
-void ByteBuffer::ByteBufferPriv::clear()
+void ByteBufferPriv::clear()
{
this->lim = this->cap;
this->pos = 0;
}
-void ByteBuffer::ByteBufferPriv::carry()
+void ByteBufferPriv::carry()
{
auto available = remaining();
memmove(this->base, current(), available);
@@ -70,13 +71,13 @@ void ByteBuffer::ByteBufferPriv::carry()
this->pos = available;
}
-void ByteBuffer::ByteBufferPriv::flip()
+void ByteBufferPriv::flip()
{
this->lim = this->pos;
this->pos = 0;
}
-bool ByteBuffer::ByteBufferPriv::put(char byte)
+bool ByteBufferPriv::put(char byte)
{
if (this->pos < this->lim)
{
@@ -87,42 +88,42 @@ bool ByteBuffer::ByteBufferPriv::put(char byte)
return false;
}
-char* ByteBuffer::ByteBufferPriv::data()
+char* ByteBufferPriv::data()
{
return this->base;
}
-const char* ByteBuffer::ByteBufferPriv::data() const
+const char* ByteBufferPriv::data() const
{
return this->base;
}
-char* ByteBuffer::ByteBufferPriv::current()
+char* ByteBufferPriv::current()
{
return this->base + this->pos;
}
-const char* ByteBuffer::ByteBufferPriv::current() const
+const char* ByteBufferPriv::current() const
{
return this->base + this->pos;
}
-size_t ByteBuffer::ByteBufferPriv::limit() const
+size_t ByteBufferPriv::limit() const
{
return this->lim;
}
-size_t ByteBuffer::ByteBufferPriv::position() const
+size_t ByteBufferPriv::position() const
{
return this->pos;
}
-size_t ByteBuffer::ByteBufferPriv::remaining() const
+size_t ByteBufferPriv::remaining() const
{
return this->lim - this->pos;
}
-size_t ByteBuffer::ByteBufferPriv::increment_position(size_t byteCount)
+size_t ByteBufferPriv::increment_position(size_t byteCount)
{
auto available = remaining();
this->pos += byteCount < available ? byteCount : available;