try to speed up
This commit is contained in:
@ -77,6 +77,8 @@ namespace {
|
||||
|
||||
constexpr std::size_t kFrameWordCount = 4U;
|
||||
constexpr std::size_t kFrameByteCount = kFrameWordCount * sizeof(uint16_t);
|
||||
constexpr std::size_t kWriteBatchFrames = 256U;
|
||||
constexpr std::size_t kWriteBatchBytes = kWriteBatchFrames * kFrameByteCount;
|
||||
|
||||
using EncodedFrame = std::array<std::uint8_t, kFrameByteCount>;
|
||||
|
||||
@ -376,8 +378,10 @@ void TtyProtocolWriter::enqueue_frame(uint16_t word0, uint16_t word1, uint16_t w
|
||||
}
|
||||
|
||||
void TtyProtocolWriter::worker_loop() {
|
||||
std::array<std::uint8_t, kWriteBatchBytes> batch_bytes {};
|
||||
|
||||
for (;;) {
|
||||
EncodedFrame frame {};
|
||||
std::size_t batch_frames = 0;
|
||||
{
|
||||
std::unique_lock<std::mutex> lock(impl_->mutex);
|
||||
impl_->data_ready_cv.wait(lock, [this]() {
|
||||
@ -388,13 +392,18 @@ void TtyProtocolWriter::worker_loop() {
|
||||
return;
|
||||
}
|
||||
|
||||
frame = impl_->ring[impl_->head];
|
||||
impl_->head = (impl_->head + 1U) % impl_->capacity_frames;
|
||||
--impl_->size;
|
||||
batch_frames = std::min<std::size_t>(kWriteBatchFrames, impl_->size);
|
||||
for (std::size_t i = 0; i < batch_frames; ++i) {
|
||||
std::memcpy(batch_bytes.data() + (i * kFrameByteCount),
|
||||
impl_->ring[impl_->head].data(),
|
||||
kFrameByteCount);
|
||||
impl_->head = (impl_->head + 1U) % impl_->capacity_frames;
|
||||
}
|
||||
impl_->size -= batch_frames;
|
||||
}
|
||||
|
||||
const std::uint8_t* bytes = frame.data();
|
||||
std::size_t remaining = frame.size();
|
||||
const std::uint8_t* bytes = batch_bytes.data();
|
||||
std::size_t remaining = batch_frames * kFrameByteCount;
|
||||
while (remaining != 0U) {
|
||||
const ssize_t written = ::write(impl_->fd, bytes, remaining);
|
||||
if (written < 0) {
|
||||
@ -436,7 +445,7 @@ void TtyProtocolWriter::worker_loop() {
|
||||
|
||||
{
|
||||
std::lock_guard<std::mutex> lock(impl_->mutex);
|
||||
++impl_->stats.frames_written;
|
||||
impl_->stats.frames_written += static_cast<std::uint64_t>(batch_frames);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user