Skip to content

Commit 466b007

Browse files
committed
opt(log): 优化log::AsyncSink,使用Buffer替代vector,使之更高效好用
1 parent 793334e commit 466b007

File tree

2 files changed

+9
-17
lines changed

2 files changed

+9
-17
lines changed

modules/log/async_sink.cpp

Lines changed: 7 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -59,30 +59,22 @@ void AsyncSink::onLogFrontEnd(const LogContent *content)
5959

6060
void AsyncSink::onLogBackEndReadPipe(const void *data_ptr, size_t data_size)
6161
{
62-
constexpr auto LogContentSize = sizeof(LogContent);
63-
const char *p = reinterpret_cast<const char*>(data_ptr);
64-
65-
buffer_.reserve(buffer_.size() + data_size);
66-
std::back_insert_iterator<std::vector<char>> back_insert_iter(buffer_);
67-
std::copy(p, p + data_size, back_insert_iter);
62+
buffer_.append(data_ptr, data_size);
6863

6964
bool is_need_flush = false;
70-
while (buffer_.size() >= LogContentSize) {
71-
auto content = reinterpret_cast<LogContent*>(buffer_.data());
72-
auto frame_size = LogContentSize + content->text_len;
73-
if (frame_size > buffer_.size()) //! 总结长度不够
65+
while (buffer_.readableSize() >= sizeof(LogContent)) {
66+
auto content = reinterpret_cast<LogContent*>(buffer_.readableBegin());
67+
auto frame_size = sizeof(LogContent) + content->text_len;
68+
if (frame_size > buffer_.readableSize()) //! 总结长度不够
7469
break;
7570
content->text_ptr = reinterpret_cast<const char *>(content + 1);
7671
onLogBackEnd(content);
7772
is_need_flush = true;
78-
buffer_.erase(buffer_.begin(), (buffer_.begin() + frame_size));
73+
buffer_.hasRead(frame_size);
7974
}
8075

81-
if (is_need_flush) {
76+
if (is_need_flush)
8277
flushLog();
83-
if (buffer_.capacity() > 1024)
84-
buffer_.shrink_to_fit();
85-
}
8678
}
8779

8880
void AsyncSink::onLogBackEnd(const LogContent *content)

modules/log/async_sink.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,8 @@
2222

2323
#include "sink.h"
2424

25-
#include <vector>
2625
#include <tbox/util/async_pipe.h>
26+
#include <tbox/util/buffer.h>
2727

2828
namespace tbox {
2929
namespace log {
@@ -50,7 +50,7 @@ class AsyncSink : public Sink {
5050
util::AsyncPipe async_pipe_;
5151
bool is_pipe_inited_ = false;
5252

53-
std::vector<char> buffer_;
53+
util::Buffer buffer_;
5454
};
5555

5656
}

0 commit comments

Comments
 (0)