[ENet-discuss] ENet performance (packet reuse/allocation, channel count...)

Lee Salzman lsalzman1 at cox.net
Mon May 1 21:25:50 PDT 2006


Just because I felt inspired I will eventually add something similar to 
the following code to the main ENet branch. If you really want, just 
manually apply these diffs for now, and when I get around to the next 
release it will be in there. You can consider the interface to it 
(ENET_PACKET_FLAG_NO_ALLOCATE) final, so don't worry about using it. It 
will just use the pointer passed to enet_packet_create() verbatim rather 
than copying from it.

Index: packet.c
===================================================================
RCS file: /cvsroot/sauerbraten/sauerbraten/src/enet/packet.c,v
retrieving revision 1.2
diff -u -r1.2 packet.c
--- packet.c    24 Mar 2006 21:14:07 -0000      1.2
+++ packet.c    2 May 2006 04:13:31 -0000
@@ -21,10 +21,15 @@
  {
      ENetPacket * packet = (ENetPacket *) enet_malloc (sizeof 
(ENetPacket));

-    packet -> data = (enet_uint8 *) enet_malloc (dataLength);
+    if(flags & ENET_PACKET_FLAG_NO_ALLOCATE)
+      packet -> data = (enet_uint8 *) data;
+    else
+    {
+       packet -> data = (enet_uint8 *) enet_malloc (dataLength);

-    if (data != NULL)
-      memcpy (packet -> data, data, dataLength);
+       if (data != NULL)
+         memcpy (packet -> data, data, dataLength);
+    };

      packet -> referenceCount = 0;
      packet -> flags = flags;
@@ -39,7 +44,8 @@
  void
  enet_packet_destroy (ENetPacket * packet)
  {
-    enet_free (packet -> data);
+    if((packet -> flags & ENET_PACKET_FLAG_NO_ALLOCATE) == 0)
+      enet_free (packet -> data);
      enet_free (packet);
  }

@@ -54,7 +60,7 @@
  {
      enet_uint8 * newData;

-    if (dataLength <= packet -> dataLength)
+    if (dataLength <= packet -> dataLength || (packet -> flags & 
ENET_PACKET_FLAG_NO_ALLOCATE))
      {
         packet -> dataLength = dataLength;

Index: include/enet/enet.h
===================================================================
RCS file: /cvsroot/sauerbraten/sauerbraten/src/enet/include/enet/enet.h,v
retrieving revision 1.8
diff -u -r1.8 enet.h
--- include/enet/enet.h 1 May 2006 19:24:17 -0000       1.8
+++ include/enet/enet.h 2 May 2006 04:13:31 -0000
@@ -80,7 +80,9 @@
     /** packet will not be sequenced with other packets
       * not supported for reliable packets
       */
-   ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1)
+   ENET_PACKET_FLAG_UNSEQUENCED = (1 << 1),
+   /** packet will not allocate data, and user must supply it instead */
+   ENET_PACKET_FLAG_NO_ALLOCATE = (1 << 2)
  } ENetPacketFlag;

  /**

Kevin Gadd wrote:
> Perfect. Being able to send the same packets multiple times will be a 
> godsend - glad that's possible. Probably wouldn't hurt to document that 
> somewhere. :)
> 
> I noticed the callbacks stuff in the new version of the library but 
> didn't notice that it was for allocation. Makes sense. Is there any way 
> to turn the callbacks on and off dynamically so that I can only use them 
> when I'm creating a packet from an existing memory buffer?
> 
> Thanks!
> -kg
> 


More information about the ENet-discuss mailing list