From 47e589072c44350b0305c05066c224d1cbda992d Mon Sep 17 00:00:00 2001 From: Maxim Sharabayko Date: Tue, 13 Aug 2019 12:35:27 +0200 Subject: [PATCH 1/2] [core] Fixed sender list to reallocate on insert if required --- srtcore/queue.cpp | 79 +++++++++++++++++++++++++---------------------- srtcore/queue.h | 23 ++++++++++---- 2 files changed, 59 insertions(+), 43 deletions(-) diff --git a/srtcore/queue.cpp b/srtcore/queue.cpp index c5bfcf705..0201d496d 100644 --- a/srtcore/queue.cpp +++ b/srtcore/queue.cpp @@ -273,32 +273,6 @@ CSndUList::~CSndUList() pthread_mutex_destroy(&m_ListLock); } -void CSndUList::insert(int64_t ts, const CUDT* u) -{ - CGuard listguard(m_ListLock); - - // increase the heap array size if necessary - if (m_iLastEntry == m_iArrayLength - 1) - { - CSNode** temp = NULL; - - try - { - temp = new CSNode*[m_iArrayLength * 2]; - } - catch(...) - { - return; - } - - memcpy(temp, m_pHeap, sizeof(CSNode*) * m_iArrayLength); - m_iArrayLength *= 2; - delete [] m_pHeap; - m_pHeap = temp; - } - - insert_(ts, u); -} void CSndUList::update(const CUDT* u, EReschedule reschedule) { @@ -319,6 +293,8 @@ void CSndUList::update(const CUDT* u, EReschedule reschedule) } remove_(u); + insert_norealloc(1, u); + return; } insert_(1, u); @@ -366,7 +342,7 @@ int CSndUList::pop(sockaddr*& addr, CPacket& pkt) // insert a new entry, ts is the next processing time if (ts > 0) - insert_(ts, u); + insert_norealloc(ts, u); return 1; } @@ -388,7 +364,38 @@ uint64_t CSndUList::getNextProcTime() return m_pHeap[0]->m_llTimeStamp_tk; } + +void CSndUList::realloc_() +{ + CSNode** temp = NULL; + + try + { + temp = new CSNode * [m_iArrayLength * 2]; + } + catch (...) + { + return; + } + + memcpy(temp, m_pHeap, sizeof(CSNode*) * m_iArrayLength); + m_iArrayLength *= 2; + delete[] m_pHeap; + m_pHeap = temp; +} + + void CSndUList::insert_(int64_t ts, const CUDT* u) +{ + // increase the heap array size if necessary + if (m_iLastEntry == m_iArrayLength - 1) + realloc_(); + + insert_norealloc(ts, u); +} + + +void CSndUList::insert_norealloc(int64_t ts, const CUDT* u) { CSNode* n = u->m_pSNode; @@ -396,6 +403,8 @@ void CSndUList::insert_(int64_t ts, const CUDT* u) if (n->m_iHeapLoc >= 0) return; + SRT_ASSERT(m_iLastEntry < m_iArrayLength); + m_iLastEntry ++; m_pHeap[m_iLastEntry] = n; n->m_llTimeStamp_tk = ts; @@ -405,16 +414,12 @@ void CSndUList::insert_(int64_t ts, const CUDT* u) while (p != 0) { p = (q - 1) >> 1; - if (m_pHeap[p]->m_llTimeStamp_tk > m_pHeap[q]->m_llTimeStamp_tk) - { - CSNode* t = m_pHeap[p]; - m_pHeap[p] = m_pHeap[q]; - m_pHeap[q] = t; - t->m_iHeapLoc = q; - q = p; - } - else - break; + if (m_pHeap[p]->m_llTimeStamp_tk <= m_pHeap[q]->m_llTimeStamp_tk) + break; + + swap(m_pHeap[p], m_pHeap[q]); + m_pHeap[q]->m_iHeapLoc = q; + q = p; } n->m_iHeapLoc = q; diff --git a/srtcore/queue.h b/srtcore/queue.h index 74dd2e36c..60f297e28 100644 --- a/srtcore/queue.h +++ b/srtcore/queue.h @@ -166,12 +166,6 @@ friend class CSndQueue; static EReschedule rescheduleIf(bool cond) { return cond ? DO_RESCHEDULE : DONT_RESCHEDULE; } - /// Insert a new UDT instance into the list. - /// @param [in] ts time stamp: next processing time - /// @param [in] u pointer to the UDT instance - - void insert(int64_t ts, const CUDT* u); - /// Update the timestamp of the UDT instance on the list. /// @param [in] u pointer to the UDT instance /// @param [in] resechedule if the timestampe shoudl be rescheduled @@ -196,7 +190,24 @@ friend class CSndQueue; uint64_t getNextProcTime(); private: + + /// Doubles the size of the list. + /// + void realloc_(); + + /// Insert a new UDT instance into the list with realloc if required. + /// + /// @param [in] ts time stamp: next processing time + /// @param [in] u pointer to the UDT instance void insert_(int64_t ts, const CUDT* u); + + /// Insert a new UDT instance into the list without realloc. + /// Should be called if there is a gauranteed space for the element. + /// + /// @param [in] ts time stamp: next processing time + /// @param [in] u pointer to the UDT instance + void insert_norealloc(int64_t ts, const CUDT* u); + void remove_(const CUDT* u); private: From 64875fa98ff6afd2f21a9727970fb4333efe95de Mon Sep 17 00:00:00 2001 From: Maxim Sharabayko Date: Tue, 13 Aug 2019 12:36:20 +0200 Subject: [PATCH 2/2] [core] CSndUList initial size is reduced to 512 elements --- srtcore/queue.cpp | 18 ++++++++---------- srtcore/queue.h | 2 +- 2 files changed, 9 insertions(+), 11 deletions(-) diff --git a/srtcore/queue.cpp b/srtcore/queue.cpp index 0201d496d..55fe903c1 100644 --- a/srtcore/queue.cpp +++ b/srtcore/queue.cpp @@ -256,7 +256,7 @@ void CUnitQueue::makeUnitGood(CUnit * unit) CSndUList::CSndUList(): m_pHeap(NULL), - m_iArrayLength(4096), + m_iArrayLength(512), m_iLastEntry(-1), m_ListLock(), m_pWindowLock(NULL), @@ -293,7 +293,7 @@ void CSndUList::update(const CUDT* u, EReschedule reschedule) } remove_(u); - insert_norealloc(1, u); + insert_norealloc_(1, u); return; } @@ -342,7 +342,7 @@ int CSndUList::pop(sockaddr*& addr, CPacket& pkt) // insert a new entry, ts is the next processing time if (ts > 0) - insert_norealloc(ts, u); + insert_norealloc_(ts, u); return 1; } @@ -371,11 +371,11 @@ void CSndUList::realloc_() try { - temp = new CSNode * [m_iArrayLength * 2]; + temp = new CSNode *[2 * m_iArrayLength]; } catch (...) { - return; + throw CUDTException(MJ_SYSTEMRES, MN_MEMORY, 0); } memcpy(temp, m_pHeap, sizeof(CSNode*) * m_iArrayLength); @@ -391,11 +391,11 @@ void CSndUList::insert_(int64_t ts, const CUDT* u) if (m_iLastEntry == m_iArrayLength - 1) realloc_(); - insert_norealloc(ts, u); + insert_norealloc_(ts, u); } -void CSndUList::insert_norealloc(int64_t ts, const CUDT* u) +void CSndUList::insert_norealloc_(int64_t ts, const CUDT* u) { CSNode* n = u->m_pSNode; @@ -457,10 +457,8 @@ void CSndUList::remove_(const CUDT* u) if (m_pHeap[q]->m_llTimeStamp_tk > m_pHeap[p]->m_llTimeStamp_tk) { - CSNode* t = m_pHeap[p]; - m_pHeap[p] = m_pHeap[q]; + swap(m_pHeap[p], m_pHeap[q]); m_pHeap[p]->m_iHeapLoc = p; - m_pHeap[q] = t; m_pHeap[q]->m_iHeapLoc = q; q = p; diff --git a/srtcore/queue.h b/srtcore/queue.h index 60f297e28..2c8f8fa11 100644 --- a/srtcore/queue.h +++ b/srtcore/queue.h @@ -206,7 +206,7 @@ friend class CSndQueue; /// /// @param [in] ts time stamp: next processing time /// @param [in] u pointer to the UDT instance - void insert_norealloc(int64_t ts, const CUDT* u); + void insert_norealloc_(int64_t ts, const CUDT* u); void remove_(const CUDT* u);