Skip to content
This repository was archived by the owner on Oct 23, 2024. It is now read-only.

Commit 4f999f6

Browse files
authored
Reuse buffer for QuicTransportStream. (#866)
It reduces the cost to create and delete buffer before and after each read, also fixes a memory leak issue.
1 parent 4853fda commit 4f999f6

File tree

2 files changed

+21
-5
lines changed

2 files changed

+21
-5
lines changed

source/agent/addons/quic/QuicTransportStream.cc

Lines changed: 18 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,8 @@ QuicTransportStream::QuicTransportStream(owt::quic::QuicTransportStreamInterface
2828
, m_contentSessionId()
2929
, m_receivedContentSessionId(false)
3030
, m_isPiped(false)
31+
, m_buffer(nullptr)
32+
, m_bufferSize(0)
3133
{
3234
}
3335

@@ -40,6 +42,7 @@ QuicTransportStream::~QuicTransportStream()
4042
uv_close(reinterpret_cast<uv_handle_t*>(&m_asyncOnData), NULL);
4143
}
4244
m_stream->SetVisitor(nullptr);
45+
delete[] m_buffer;
4346
}
4447

4548
void QuicTransportStream::OnCanRead()
@@ -202,20 +205,30 @@ void QuicTransportStream::SignalOnData()
202205

203206
while (m_stream->ReadableBytes() > 0) {
204207
auto readableBytes = m_stream->ReadableBytes();
205-
uint8_t* buffer = new uint8_t[readableBytes];
206-
if (buffer == nullptr) {
207-
ELOG_ERROR("Failed to alloc buffer.");
208-
return;
208+
if (readableBytes > m_bufferSize) {
209+
ReallocateBuffer(readableBytes);
209210
}
210211
owt_base::Frame frame;
211212
frame.format = owt_base::FRAME_FORMAT_DATA;
212213
frame.length = readableBytes;
213-
frame.payload = buffer;
214+
frame.payload = m_buffer;
214215
m_stream->Read(frame.payload, readableBytes);
215216
deliverFrame(frame);
216217
}
217218
}
218219

220+
void QuicTransportStream::ReallocateBuffer(size_t size)
221+
{
222+
if (size > m_bufferSize) {
223+
delete[] m_buffer;
224+
}
225+
m_buffer = new uint8_t[size];
226+
m_bufferSize = size;
227+
if (!m_buffer) {
228+
ELOG_ERROR("Failed to allocate buffer.");
229+
}
230+
}
231+
219232
void QuicTransportStream::onFeedback(const owt_base::FeedbackMsg&)
220233
{
221234
// No feedbacks righ now.

source/agent/addons/quic/QuicTransportStream.h

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,12 +56,15 @@ class QuicTransportStream : public owt_base::FrameSource, public owt_base::Frame
5656
// Try to read content session ID from data buffered.
5757
void MaybeReadContentSessionId();
5858
void SignalOnData();
59+
void ReallocateBuffer(size_t size);
5960

6061
owt::quic::QuicTransportStreamInterface* m_stream;
6162
std::vector<uint8_t> m_contentSessionId;
6263
bool m_receivedContentSessionId;
6364
// True if it's piped to a receiver in C++ layer. In this case, data will not be sent to JavaScript code.
6465
bool m_isPiped;
66+
uint8_t* m_buffer;
67+
size_t m_bufferSize;
6568

6669
uv_async_t m_asyncOnContentSessionId;
6770
uv_async_t m_asyncOnData;

0 commit comments

Comments
 (0)