Complete.Org: Mailing Lists: Archives: freeciv-dev: September 2004:
[Freeciv-Dev] Re: (PR#9947) insane inefficiency in packet sending
Home

[Freeciv-Dev] Re: (PR#9947) insane inefficiency in packet sending

[Top] [All Lists]

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index] [Thread Index]
To: undisclosed-recipients: ;
Subject: [Freeciv-Dev] Re: (PR#9947) insane inefficiency in packet sending
From: "Jason Short" <jdorje@xxxxxxxxxxxxxxxxxxxxx>
Date: Sun, 5 Sep 2004 14:05:11 -0700
Reply-to: rt@xxxxxxxxxxx

<URL: http://rt.freeciv.org/Ticket/Display.html?id=9947 >

This patch should fix things, and make them simpler as well.

However I can find no noticeable improvement on my system.  I wonder if 
I am timing things wrong, or if glibc's realloc uses an algorithm 
similar to specvec's for O(1) ammortized reallocation...

jason


Index: common/connection.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.c,v
retrieving revision 1.42
diff -u -r1.42 connection.c
--- common/connection.c 29 Jul 2004 00:09:27 -0000      1.42
+++ common/connection.c 5 Sep 2004 20:57:28 -0000
@@ -539,11 +539,7 @@
 void free_compression_queue(struct connection *pc)
 {
 #ifdef USE_COMPRESSION
-  if (pc->compression.queue_size > 0) {
-    free(pc->compression.queued_packets);
-    pc->compression.queued_packets = NULL;
-  }
-  pc->compression.queue_size = 0;
+  byte_vector_free(&pc->compression.queue);
 #endif
 }
 
@@ -607,9 +603,7 @@
   init_packet_hashs(pconn);
 
 #ifdef USE_COMPRESSION
-  pconn->compression.frozen_level = 0;
-  pconn->compression.queued_packets = NULL;
-  pconn->compression.queue_size = 0;
+  byte_vector_init(&pconn->compression.queue);
 #endif
 }
 
Index: common/connection.h
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/connection.h,v
retrieving revision 1.35
diff -u -r1.35 connection.h
--- common/connection.h 3 Sep 2004 04:22:37 -0000       1.35
+++ common/connection.h 5 Sep 2004 20:57:28 -0000
@@ -95,6 +95,10 @@
   unsigned char *data;
 };
 
+#define SPECVEC_TAG byte
+#define SPECVEC_TYPE unsigned char
+#include "specvec.h"
+
 /***********************************************************
   The connection struct represents a single client or server
   at the other end of a network connection.
@@ -215,8 +219,8 @@
 #ifdef USE_COMPRESSION
   struct {
     int frozen_level;
-    void *queued_packets;
-    size_t queue_size;
+
+    struct byte_vector queue;
   } compression;
 #endif
   struct {
Index: common/packets.c
===================================================================
RCS file: /home/freeciv/CVS/freeciv/common/packets.c,v
retrieving revision 1.269
diff -u -r1.269 packets.c
--- common/packets.c    5 Sep 2004 05:10:50 -0000       1.269
+++ common/packets.c    5 Sep 2004 20:57:28 -0000
@@ -136,16 +136,16 @@
     if (packet_type == PACKET_PROCESSING_STARTED
        || packet_type == PACKET_FREEZE_HINT) {
       if (pc->compression.frozen_level == 0) {
-       free_compression_queue(pc);
+       byte_vector_reserve(&pc->compression.queue, 0);
       }
       pc->compression.frozen_level++;
     }
 
     if (pc->compression.frozen_level > 0) {
-      size_t new_size = pc->compression.queue_size + size;
-      pc->compression.queued_packets = 
fc_realloc(pc->compression.queued_packets, new_size);
-      memcpy(ADD_TO_POINTER(pc->compression.queued_packets, 
pc->compression.queue_size), data, len);
-      pc->compression.queue_size = new_size;
+      size_t old_size = pc->compression.queue.size;
+
+      byte_vector_reserve(&pc->compression.queue, old_size + len);
+      memcpy(pc->compression.queue.p + old_size, data, len);
       freelog(COMPRESS2_LOG_LEVEL, "COMPRESS: putting %s into the queue",
              get_packet_name(packet_type));
     } else {
@@ -159,23 +159,23 @@
        PACKET_PROCESSING_FINISHED || packet_type == PACKET_THAW_HINT) {
       pc->compression.frozen_level--;
       if (pc->compression.frozen_level == 0) {
-       long int compressed_size = 12 + pc->compression.queue_size * 1.001;
+       long int compressed_size = 12 + pc->compression.queue.size * 1.001;
        int error;
        char compressed[compressed_size];
 
        error =
            compress2(compressed, &compressed_size,
-                     pc->compression.queued_packets,
-                     pc->compression.queue_size, compression_level);
+                     pc->compression.queue.p, pc->compression.queue.size,
+                     compression_level);
        assert(error == Z_OK);
-       if (compressed_size + 2 < pc->compression.queue_size) {
+       if (compressed_size + 2 < pc->compression.queue.size) {
            struct data_out dout;
 
          freelog(COMPRESS_LOG_LEVEL,
                  "COMPRESS: compressed %d bytes to %ld (level %d)",
-                 pc->compression.queue_size, compressed_size,
+                 pc->compression.queue.size, compressed_size,
                  compression_level);
-         stat_size_uncompressed += pc->compression.queue_size;
+         stat_size_uncompressed += pc->compression.queue.size;
          stat_size_compressed += compressed_size;
 
          if (compressed_size <= JUMBO_BORDER) {
@@ -202,9 +202,10 @@
        } else {
          freelog(COMPRESS_LOG_LEVEL,
                  "COMPRESS: would enlarging %d bytes to %ld; sending 
uncompressed",
-                 pc->compression.queue_size, compressed_size);
-         send_connection_data(pc, pc->compression.queued_packets, 
pc->compression.queue_size);
-         stat_size_no_compression += pc->compression.queue_size;
+                 pc->compression.queue.size, compressed_size);
+         send_connection_data(pc, pc->compression.queue.p,
+                              pc->compression.queue.size);
+         stat_size_no_compression += pc->compression.queue.size;
        }
       }
     }

[Prev in Thread] Current Thread [Next in Thread]