try alternative implementation of AddSmall
authorMathieu Lacage <mathieu.lacage@sophia.inria.fr>
Thu, 07 Jun 2007 10:38:48 +0200
changeset 870 e335d37541c3
parent 869 652907a5e97d
child 871 0451a0497ca3
try alternative implementation of AddSmall
src/common/packet-history.cc
src/common/packet-history.h
--- a/src/common/packet-history.cc	Thu Jun 07 09:38:51 2007 +0200
+++ b/src/common/packet-history.cc	Thu Jun 07 10:38:48 2007 +0200
@@ -299,8 +299,8 @@
 {
   std::cout << "allocs="<<g_nAllocs<<", deallocs="<<g_nDeAllocs
             <<", recycle="<<g_nRecycle<<", create="<<g_nCreate<<std::endl;
-  std::cout << "one="<<g_one<<", two="<<g_two<<", three="<<g_three<<std::endl;
-  //std::cout << "four="<<g_four<<", five="<<g_five<<std::endl;
+  //std::cout << "one="<<g_one<<", two="<<g_two<<", three="<<g_three<<std::endl;
+  std::cout << "four="<<g_four<<", five="<<g_five<<std::endl;
   g_nAllocs = 0;
   g_nDeAllocs = 0;
   g_nRecycle = 0;
@@ -347,7 +347,8 @@
 {
   NS_ASSERT (m_data != 0);
   if (m_data->m_size >= m_used + size &&
-      (m_data->m_count == 1 ||
+      (m_head == 0xffff ||
+       m_data->m_count == 1 ||
        m_data->m_dirtyEnd == m_used))
     {
       /* enough room, not dirty. */
@@ -528,6 +529,71 @@
 }
 
 void
+PacketHistory::AppendValueExtra (uint32_t value, uint8_t *buffer)
+{
+  if (value < 0x200000)
+    {
+      uint8_t byte = value & (~0x80);
+      buffer[0] = 0x80 | byte;
+      value >>= 7;
+      byte = value & (~0x80);
+      buffer[1] = 0x80 | byte;
+      value >>= 7;
+      byte = value & (~0x80);
+      buffer[2] = value;
+      return;
+    }
+  if (value < 0x10000000)
+    {
+      uint8_t byte = value & (~0x80);
+      buffer[0] = 0x80 | byte;
+      value >>= 7;
+      byte = value & (~0x80);
+      buffer[1] = 0x80 | byte;
+      value >>= 7;
+      byte = value & (~0x80);
+      buffer[2] = 0x80 | byte;
+      value >>= 7;
+      buffer[3] = value;
+      return;
+    }
+  {
+    uint8_t byte = value & (~0x80);
+    buffer[0] = 0x80 | byte;
+    value >>= 7;
+    byte = value & (~0x80);
+    buffer[1] = 0x80 | byte;
+    value >>= 7;
+    byte = value & (~0x80);
+    buffer[2] = 0x80 | byte;
+    value >>= 7;
+    byte = value & (~0x80);
+    buffer[3] = 0x80 | byte;
+    value >>= 7;
+    buffer[4] = value;
+  }
+}
+
+void
+PacketHistory::AppendValue (uint32_t value, uint8_t *buffer)
+{
+  if (value < 0x80)
+    {
+      buffer[0] = value;
+      return;
+    }
+  if (value < 0x4000)
+    {
+      uint8_t byte = value & (~0x80);
+      buffer[0] = 0x80 | byte;
+      value >>= 7;
+      buffer[1] = value;
+      return;
+    }
+  AppendValueExtra (value, buffer);
+}
+
+void
 PacketHistory::UpdateTail (uint16_t written)
 {
   if (m_head == 0xffff)
@@ -586,6 +652,7 @@
       NS_ASSERT (m_head == 0xffff && m_tail == 0xffff);
     }
   NS_ASSERT (m_data != 0);
+#if 1
  append:
   uint8_t *start = &m_data->m_data[m_used];
   uint8_t *end = &m_data->m_data[m_data->m_size];
@@ -615,8 +682,38 @@
   n += GetUleb128Size (item->size);
   n += GetUleb128Size (item->chunkUid);
   n += 2 + 2;
-  ReserveCopy (n);
+  Reserve (n);
   goto append;
+#else
+  uint32_t typeUidSize = GetUleb128Size (item->typeUid);
+  uint32_t sizeSize = GetUleb128Size (item->size);
+  uint32_t chunkUidSize = GetUleb128Size (item->chunkUid);
+  uint32_t n = typeUidSize + sizeSize + chunkUidSize + 2 + 2;
+ restart:
+  if (m_used + n <= m_data->m_size &&
+      (m_head == 0xffff ||
+       m_data->m_count == 1 ||
+       m_used == m_data->m_dirtyEnd))
+    {
+      uint8_t *buffer = &m_data->m_data[m_used];
+      Append16 (item->next, buffer);
+      buffer += 2;
+      Append16 (item->prev, buffer);
+      buffer += 2;
+      AppendValue (item->typeUid, buffer);
+      buffer += typeUidSize;
+      AppendValue (item->size, buffer);
+      buffer += sizeSize;
+      AppendValue (item->chunkUid, buffer);
+    }
+  else
+    {
+      ReserveCopy (n);
+      goto restart;
+    }
+
+  return n;
+#endif
 }
 
 uint16_t
--- a/src/common/packet-history.h	Thu Jun 07 09:38:51 2007 +0200
+++ b/src/common/packet-history.h	Thu Jun 07 10:38:48 2007 +0200
@@ -107,12 +107,14 @@
                     uint32_t available);
   inline void UpdateHead (uint16_t written);
   inline void UpdateTail (uint16_t written);
-  inline uint32_t GetUleb128Size (uint32_t value) const;
+  uint32_t GetUleb128Size (uint32_t value) const;
   uint32_t ReadUleb128 (const uint8_t **pBuffer) const;
   inline void Append16 (uint16_t value, uint8_t *buffer);
   inline bool TryToAppend (uint32_t value, uint8_t **pBuffer, uint8_t *end);
   inline bool TryToAppendFast (uint32_t value, uint8_t **pBuffer, uint8_t *end);
-  void Reserve (uint32_t n);
+  void AppendValue (uint32_t value, uint8_t *buffer);
+  void AppendValueExtra (uint32_t value, uint8_t *buffer);
+  inline void Reserve (uint32_t n);
   void ReserveCopy (uint32_t n);
   uint32_t DoPrint (struct PacketHistory::SmallItem *item, uint32_t current,
                     Buffer data, uint32_t offset, const PacketPrinter &printer,