[1028] in Coldmud discussion meeting

root meeting help first previous next last

[COLD] Database compression patch

daemon@ATHENA.MIT.EDU (Fri Jul 12 04:38:09 1996 )

Date: Fri, 12 Jul 1996 10:10:18 +0200 (MET DST)
From: Miroslav Silovic <silovic@public.srce.hr>
To: coldstuff@cold.org


The following patch contains the new binary db compression routine.
With this, binary of the core went from 1130 kb to 560 kb (approx).
It boots, and a trick list of longs dumped and reloaded correctly, so
I must assume that the routine works. :) (I'm being careful with my
phrasing because the old one was buggy and it took long to notice the
problem. My code is at least simple enough that you can check it by
looking at it).

	Miro

-----------------------------------------------------------------------


diff -C 8 -r Genesis-1.0p7/src/dbpack.c Genesis-1.0p7-new/src/dbpack.c
*** Genesis-1.0p7/src/dbpack.c	Mon Jul  1 03:02:22 1996
--- Genesis-1.0p7-new/src/dbpack.c	Fri Jul 12 09:53:53 1996
***************
*** 16,34 ****
--- 16,54 ----
  #include "defs.h"
  
  #include <string.h>
  #include "dbpack.h"
  #include "cdc_types.h"
  #include "data.h"
  #include "memory.h"
  
+ #define COMPRESS 1
+ 
  /* Write a four-byte number to fp in a consistent byte-order. */
  void write_long(long n, FILE *fp)
  {
+ #ifdef COMPRESS
+ 
+   int sign, i, h, buf[17]; /* for 256-bit future architecture */
+ 
+   sign = n<0 ? 1 : 0;
+   n = abs(n);
+   h = 1;
+   buf[0] = n&15;
+   n >>= 4;
+   while (n) {
+     buf[h++] = n & 255;
+     n >>= 8;
+   }
+   buf[0] += (h << 5) + (sign << 4);
+   for (i=0; i<h; i++)
+     putc(buf[i], fp);
+ 
+ #else
  #ifdef ORDER_BYTES
      /* Since first byte is special, special-case 0 as well. */
      if (!n) {
  	putc(96, fp);
  	return;
      }
  
      /* First byte depends on sign. */
***************
*** 39,59 ****
--- 59,97 ----
  	putc(32 + (n % 64), fp);
  	n /= 64;
      }
  
      putc(96, fp);
  #else
      fwrite(&n, sizeof(long), 1, fp);
  #endif
+ #endif
  }
  
  /* Read a four-byte number in a consistent byte-order. */
  long read_long(FILE *fp)
  {
+ #ifdef COMPRESS
+ 
+   int sign, i, h, n, k;
+ 
+   h = (unsigned)getc(fp) & 255;
+   sign = h & 16;
+   n = h & 15;
+   k = 4;
+   h = h >> 5;
+   for (i=0; i<h-1; i++) {
+     n += ((unsigned)getc(fp) & 255) << k;
+     k += 8;
+   }
+   if (sign) n=-n;
+   return n;
+ 
+ #else
  #ifdef ORDER_BYTES
      int c;
      long n, place;
  
      /* Check for initial terminator, meaning 0. */
      c = getc(fp);
      if (c == 96)
  	return 0;
***************
*** 71,104 ****
--- 109,157 ----
      }
  #else
      long    l;
    
      fread(&l, sizeof(long), 1, fp);
  
      return l;
  #endif
+ #endif
  }
  
  int size_long(long n)
  {
+ #ifdef COMPRESS
+ 
+   int h;
+ 
+   n = abs(n);
+   h = 1;
+   n >>= 4;
+   while (n) {
+     h++;
+     n >>= 8;
+   }
+   return h;
+ #else
  #ifdef ORDER_BYTES
      int count = 2;
  
      if (!n)
  	return 1;
      n /= 32;
      while (n) {
  	n /= 64;
  	count++;
      }
      return count;
  #else
      return sizeof(n);
  #endif
+ #endif
  }
  
  
  INTERNAL void write_ident(long id, FILE *fp)
  {
      char *s;
      int len;