Optimize ByteTagList adjustment
authorAlexander Krotov <ilabdsf@yandex.ru>
Sat, 08 Aug 2015 23:13:40 +0300
changeset 11565 f75bca10b317
parent 11564 bc5002f15ce8
child 11566 85fc8963ab62
Optimize ByteTagList adjustment
src/network/model/byte-tag-list.cc
src/network/model/byte-tag-list.h
--- a/src/network/model/byte-tag-list.cc	Sat Aug 08 18:21:45 2015 +0300
+++ b/src/network/model/byte-tag-list.cc	Sat Aug 08 23:13:40 2015 +0300
@@ -106,8 +106,8 @@
       TagBuffer buf = TagBuffer (m_current, m_end);
       m_nextTid = buf.ReadU32 ();
       m_nextSize = buf.ReadU32 ();
-      m_nextStart = buf.ReadU32 ();
-      m_nextEnd = buf.ReadU32 ();
+      m_nextStart = buf.ReadU32 () + m_adjustment;
+      m_nextEnd = buf.ReadU32 () + m_adjustment;
       if (m_nextStart >= m_offsetEnd || m_nextEnd <= m_offsetStart)
         {
           m_current += 4 + 4 + 4 + 4 + m_nextSize;
@@ -118,13 +118,14 @@
         }
     }
 }
-ByteTagList::Iterator::Iterator (uint8_t *start, uint8_t *end, int32_t offsetStart, int32_t offsetEnd)
+ByteTagList::Iterator::Iterator (uint8_t *start, uint8_t *end, int32_t offsetStart, int32_t offsetEnd, int32_t adjustment)
   : m_current (start),
     m_end (end),
     m_offsetStart (offsetStart),
-    m_offsetEnd (offsetEnd)
+    m_offsetEnd (offsetEnd),
+    m_adjustment (adjustment)
 {
-  NS_LOG_FUNCTION (this << &start << &end << offsetStart << offsetEnd);
+  NS_LOG_FUNCTION (this << &start << &end << offsetStart << offsetEnd << adjustment);
   PrepareForNext ();
 }
 
@@ -139,6 +140,7 @@
 ByteTagList::ByteTagList ()
   : m_minStart (INT32_MAX),
     m_maxEnd (INT32_MIN),
+    m_adjustment (0),
     m_used (0),
     m_data (0)
 {
@@ -147,6 +149,7 @@
 ByteTagList::ByteTagList (const ByteTagList &o)
   : m_minStart (o.m_minStart),
     m_maxEnd (o.m_maxEnd),
+    m_adjustment (o.m_adjustment),
     m_used (o.m_used),
     m_data (o.m_data)
 {
@@ -167,6 +170,7 @@
   Deallocate (m_data);
   m_minStart = o.m_minStart;
   m_maxEnd = o.m_maxEnd;
+  m_adjustment = o.m_adjustment;
   m_data = o.m_data;
   m_used = o.m_used;
   if (m_data != 0)
@@ -206,15 +210,15 @@
                              &m_data->data[spaceNeeded]);
   tag.WriteU32 (tid.GetUid ());
   tag.WriteU32 (bufferSize);
-  tag.WriteU32 (start);
-  tag.WriteU32 (end);
-  if (start < m_minStart)
+  tag.WriteU32 (start - m_adjustment);
+  tag.WriteU32 (end - m_adjustment);
+  if (start - m_adjustment < m_minStart)
     {
-      m_minStart = start;
+      m_minStart = start - m_adjustment;
     }
-  if (end > m_maxEnd)
+  if (end - m_adjustment > m_maxEnd)
     {
-      m_maxEnd = end;
+      m_maxEnd = end - m_adjustment;
     }
   m_used = spaceNeeded;
   m_data->dirty = m_used;
@@ -241,6 +245,7 @@
   Deallocate (m_data);
   m_minStart = INT32_MAX;
   m_maxEnd = INT32_MIN;
+  m_adjustment = 0;
   m_data = 0;
   m_used = 0;
 }
@@ -260,11 +265,11 @@
   NS_LOG_FUNCTION (this << offsetStart << offsetEnd);
   if (m_data == 0)
     {
-      return Iterator (0, 0, offsetStart, offsetEnd);
+      return Iterator (0, 0, offsetStart, offsetEnd, 0);
     }
   else
     {
-      return Iterator (m_data->data, &m_data->data[m_used], offsetStart, offsetEnd);
+      return Iterator (m_data->data, &m_data->data[m_used], offsetStart, offsetEnd, m_adjustment);
     }
 }
 
@@ -272,7 +277,8 @@
 ByteTagList::AddAtEnd (int32_t adjustment, int32_t appendOffset)
 {
   NS_LOG_FUNCTION (this << adjustment << appendOffset);
-  if (adjustment == 0 && m_maxEnd <= appendOffset)
+  m_adjustment += adjustment;
+  if (m_maxEnd <= appendOffset - m_adjustment)
     {
       return;
     }
@@ -281,8 +287,6 @@
   while (i.HasNext ())
     {
       ByteTagList::Iterator::Item item = i.Next ();
-      item.start += adjustment;
-      item.end += adjustment;
 
       if (item.start >= appendOffset)
         {
@@ -306,7 +310,8 @@
 ByteTagList::AddAtStart (int32_t adjustment, int32_t prependOffset)
 {
   NS_LOG_FUNCTION (this << adjustment << prependOffset);
-  if (adjustment == 0 && m_minStart >= prependOffset)
+  m_adjustment += adjustment;
+  if (m_minStart >= prependOffset - m_adjustment)
     {
       return;
     }
@@ -316,8 +321,6 @@
   while (i.HasNext ())
     {
       ByteTagList::Iterator::Item item = i.Next ();
-      item.start += adjustment;
-      item.end += adjustment;
 
       if (item.end <= prependOffset)
         {
--- a/src/network/model/byte-tag-list.h	Sat Aug 08 18:21:45 2015 +0300
+++ b/src/network/model/byte-tag-list.h	Sat Aug 08 23:13:40 2015 +0300
@@ -126,8 +126,9 @@
      * \param end End tag
      * \param offsetStart offset to the start of the tag from the virtual byte buffer
      * \param offsetEnd offset to the end of the tag from the virtual byte buffer
+     * \param adjustment adjustment to byte tag offsets
      */
-    Iterator (uint8_t *start, uint8_t *end, int32_t offsetStart, int32_t offsetEnd);
+    Iterator (uint8_t *start, uint8_t *end, int32_t offsetStart, int32_t offsetEnd, int32_t m_adjustment);
 
     /**
      * \brief Prepare the iterator for the next tag
@@ -137,6 +138,7 @@
     uint8_t *m_end;         //!< End tag
     int32_t m_offsetStart;  //!< Offset to the start of the tag from the virtual byte buffer
     int32_t m_offsetEnd;    //!< Offset to the end of the tag from the virtual byte buffer
+    int32_t m_adjustment;   //!< Adjustment to byte tag offsets
     uint32_t m_nextTid;     //!< TypeId of the next tag
     uint32_t m_nextSize;    //!< Size of the next tag
     int32_t m_nextStart;    //!< Start of the next tag
@@ -249,6 +251,7 @@
 
   int32_t m_minStart; // !< minimal start offset
   int32_t m_maxEnd; // !< maximal end offset
+  int32_t m_adjustment; // !< adjustment to byte tag offsets
   uint16_t m_used; //!< the number of used bytes in the buffer
   struct ByteTagListData *m_data; //!< the ByteTagListData structure
 };