Skip to content

Commit 2146189

Browse files
committed
Reduce logging overhead when the output is unchanged by the character encoding
1 parent 78d8e82 commit 2146189

File tree

3 files changed

+40
-11
lines changed

3 files changed

+40
-11
lines changed

src/main/cpp/charsetencoder.cpp

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -644,3 +644,18 @@ void CharsetEncoder::encode(CharsetEncoderPtr& enc,
644644
dst.put(Transcoder::LOSSCHAR);
645645
}
646646
}
647+
648+
bool CharsetEncoder::isTriviallyCopyable(const LogString& src, CharsetEncoderPtr& enc)
649+
{
650+
bool result = false;
651+
if (dynamic_cast<LocaleCharsetEncoder*>(enc.get()))
652+
{
653+
result = src.end() == std::find_if(src.begin(), src.end()
654+
, [](const logchar& ch) -> bool { return 0x80 <= (unsigned int)ch; });
655+
}
656+
#if LOG4CXX_LOGCHAR_IS_UTF8
657+
else
658+
result = !!dynamic_cast<TrivialCharsetEncoder*>(enc.get());
659+
#endif
660+
return result;
661+
}

src/main/cpp/outputstreamwriter.cpp

Lines changed: 19 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -78,18 +78,29 @@ void OutputStreamWriter::flush(Pool& p)
7878

7979
void OutputStreamWriter::write(const LogString& str, Pool& p)
8080
{
81-
if (str.length() > 0)
81+
if (str.empty())
82+
return;
83+
if (CharsetEncoder::isTriviallyCopyable(str, m_priv->enc))
8284
{
85+
ByteBuffer buf((char*)str.data(), str.size() * sizeof (logchar));
86+
m_priv->out->write(buf, p);
87+
}
88+
else
89+
{
90+
enum { BUFSIZE = 1024 };
91+
char stackData[BUFSIZE];
92+
char* rawbuf = stackData;
93+
size_t bufSize = BUFSIZE;
8394
#ifdef LOG4CXX_MULTI_PROCESS
95+
std::vector<char> heapData;
8496
// Ensure the logging event is a single write system call to keep events from each process separate
85-
size_t bufSize = str.length() * 2;
86-
char* rawbuf = new char[bufSize];
87-
ByteBuffer buf(rawbuf, (size_t) bufSize);
88-
#else
89-
enum { BUFSIZE = 1024 };
90-
char rawbuf[BUFSIZE];
91-
ByteBuffer buf(rawbuf, (size_t) BUFSIZE);
97+
if (bufSize < str.length() * 2)
98+
{
99+
heapData.resize(bufSize = str.length() * 2)
100+
rawbuf = heapData.data();
101+
}
92102
#endif
103+
ByteBuffer buf(rawbuf, bufSize);
93104
m_priv->enc->reset();
94105
LogString::const_iterator iter = str.begin();
95106

@@ -105,9 +116,6 @@ void OutputStreamWriter::write(const LogString& str, Pool& p)
105116
m_priv->enc->flush(buf);
106117
buf.flip();
107118
m_priv->out->write(buf, p);
108-
#ifdef LOG4CXX_MULTI_PROCESS
109-
delete []rawbuf;
110-
#endif
111119
}
112120
}
113121

src/main/include/log4cxx/helpers/charsetencoder.h

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,12 @@ class LOG4CXX_EXPORT CharsetEncoder : public Object
116116
return (stat != 0);
117117
}
118118

119+
/**
120+
* Is the data of \c src unchanged by \c enc.
121+
*
122+
*/
123+
static bool isTriviallyCopyable(const LogString& src, CharsetEncoderPtr& enc);
124+
119125

120126
private:
121127
/**

0 commit comments

Comments
 (0)