Re: how to: decrease the size of serialized objects?

From: assaf (assafwo_at_hotmail.com)
Date: 10/01/04

  • Next message: Justin Rogers: "Re: how to: decrease the size of serialized objects?"
    Date: Sat, 2 Oct 2004 01:10:06 +0200
    
    

    hi bob

    the following code can be run.
    u will see that an empty packet is 131 bytes.
    packets filled with minimal stuff come out to almost a kilobyte each.

    assaf

    //// start of code

    using System.IO;
    using System.Runtime.Serialization;
    using System.Runtime.Serialization.Formatters.Binary;
    using System;
    using System.Drawing;
    using System.Collections;
    using System.ComponentModel;
    using System.Windows.Forms;
    using System.Data;

    namespace Serialize
    {
     /// <summary>
     /// Summary description for Form1.
     /// </summary>
     public class Form1 : System.Windows.Forms.Form
     {
      private System.Windows.Forms.Button button1;
      /// <summary>
      /// Required designer variable.
      /// </summary>
      private System.ComponentModel.Container components = null;

      public Form1()
      {
       //
       // Required for Windows Form Designer support
       //
       InitializeComponent();

       //
       // TODO: Add any constructor code after InitializeComponent call
       //
      }

      /// <summary>
      /// Clean up any resources being used.
      /// </summary>
      protected override void Dispose( bool disposing )
      {
       if( disposing )
       {
        if (components != null)
        {
         components.Dispose();
        }
       }
       base.Dispose( disposing );
      }

      #region Windows Form Designer generated code
      /// <summary>
      /// Required method for Designer support - do not modify
      /// the contents of this method with the code editor.
      /// </summary>
      private void InitializeComponent()
      {
       this.button1 = new System.Windows.Forms.Button();
       this.SuspendLayout();
       //
       // button1
       //
       this.button1.Location = new System.Drawing.Point(8, 8);
       this.button1.Name = "button1";
       this.button1.TabIndex = 0;
       this.button1.Text = "button1";
       this.button1.Click += new System.EventHandler(this.button1_Click);
       //
       // Form1
       //
       this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
       this.ClientSize = new System.Drawing.Size(292, 273);
       this.Controls.Add(this.button1);
       this.Name = "Form1";
       this.Text = "Form1";
       this.ResumeLayout(false);

      }
      #endregion

      /// <summary>
      /// The main entry point for the application.
      /// </summary>
      [STAThread]
      static void Main()
      {
       Application.Run(new Form1());
      }

      public interface IPacket
      {
       int Id{get;}
      }

      public interface IStatusInfo
      {
       int GlobalServerId {get;}
       int GlobalId {get;}
       int LocalId {get;}
       int RoomId {get;}
       string UserName {get;}
       int SentenceId {get;}
       string RoomName {get;}
      }

      [Serializable]
       public class Packet : IPacket
      {
       #region Public Variables

       #endregion Public Variables

       #region Private Variables

       private static int _IdCounter;

       #endregion Private Variables

       public readonly int Id;
       public readonly int GlobalUserId;
       public readonly int LocalUserId;
       public readonly int ServerId;
       public readonly DateTime TimeStamp;

       #region Ctor
       public Packet(IStatusInfo isi)
       {
        this.Id = Packet._IdCounter++;
        this.GlobalUserId = 0; // isi.GlobalId;
        this.LocalUserId = 0; // isi.LocalId;
        this.ServerId = 0; // isi.GlobalServerId;
        this.TimeStamp = DateTime.Now;
       }

       #endregion Ctor

       #region IPacket Members

       int IPacket.Id
       {
        get
        {
         return this.Id;
        }
       }
       #endregion
      }

      [Serializable]
       public class Data : Packet
      {
       #region Public Variables

       public readonly int RoomId;
       public readonly int SentenceId;
       public readonly int StreamId;
       public readonly int Size;

       #endregion Public Variables

       public Data(int size, int streamId, IStatusInfo sInfo)
        : base (sInfo)
       {
        this.Size = size;
        this.RoomId = 0; // sInfo.RoomId;
        this.SentenceId = 0; // sInfo.SentenceId;
        this.StreamId = streamId;
       }
      }

      [Serializable]
       public class TextPacket : Data
      {
       #region Public Variables

       public readonly string _Text;

       #endregion Public Variables

       public TextPacket(string txt, int size, int streamId, IStatusInfo sInfo)
        : base(size, streamId, sInfo)
       {
        this._Text = txt;
       }
      }

      [Serializable]
       public class SuperPacket : Packet
      {
       #region Private Vatiables

       private readonly Hashtable _Hashtable;

       #endregion Private Vatiables

       #region Public Vatiables

       public readonly int RoomId;

       #endregion Public Vatiables

       #region Ctor

       public SuperPacket(IStatusInfo sInfo) : base (sInfo)
       {
        this._Hashtable = Hashtable.Synchronized(new Hashtable());
        this.RoomId = 0; // sInfo.RoomId;
       }

       #endregion Ctor

       public Data this[int key]
       {
        get
        {
         return (Data)this._Hashtable[key];
        }
       }

       public void Add(Data d)
       {
        this._Hashtable.Add(this._Hashtable.Count, d);
       }

       public int Count()
       {
        return this._Hashtable.Count;
       }
      }

      [Serializable]
       public class EncryptPacket : Packet
      {
       #region Public Vatiables

       public readonly int RoomId;
       public readonly bool IsEncrypted;
       public readonly SuperPacket SPacket;

       #endregion

       #region Ctor

       public EncryptPacket(IStatusInfo sInfo, SuperPacket sp) : base (sInfo)
       {
        this.SPacket = sp;
        this.RoomId = 0; // sInfo.RoomId;
       }

       #endregion Ctor
      }

      [Serializable]
       private class Packet2 : Packet
      {
       public int i;
       internal Packet2(int i)
        : base(null)
       {
        this.i = i;
       }
      }

      [Serializable]
       private class Packet3 : Packet2
      {
       public int j;
       internal Packet3(int i, int j)
        : base(i)
       {
        this.i = i;
        this.j = j;
       }
      }

      private void Serialize1()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       Packet p = new Packet(null);
       bf.Serialize(ms, p);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void Serialize2()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       Packet2 p = new Packet2(2);
       bf.Serialize(ms, p);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void Serialize3()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       Packet3 p = new Packet3(2, 5);
       bf.Serialize(ms, p);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void SerializeText()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       TextPacket t = new TextPacket("", 3, 3, null);
       bf.Serialize(ms, t);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void SerializeSuperPacket()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       SuperPacket sp = new SuperPacket(null);
       bf.Serialize(ms, sp);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void SerializeEncryptedPacket()
      {
       BinaryFormatter bf = new BinaryFormatter();
       MemoryStream ms = new MemoryStream();
       SuperPacket sp = new SuperPacket(null);
       EncryptPacket ep = new EncryptPacket(null, sp);
       bf.Serialize(ms, ep);
       byte[] buffer = ms.ToArray();
       int bufferSize = buffer.Length;
       Console.WriteLine("bufferSize: {0}", bufferSize);
       foreach(byte b in buffer)
       {
        Console.Write((char)b);
       }
      }

      private void button1_Click(object sender, System.EventArgs e)
      {
       Console.Write("\n----------------------\n");
       this.Serialize1();
       Console.Write("\n----------------------\n");
       this.Serialize2();
       Console.Write("\n----------------------\n");
       this.Serialize3();
       Console.Write("\n----------------------\n");
       this.SerializeText();
       Console.Write("\n----------------------\n");
       this.SerializeSuperPacket();
       Console.Write("\n----------------------\n");
       this.SerializeEncryptedPacket();
      }

     }
    }

    //// end of code

    assaf

    "Bob Grommes" <bob@bobgrommes.com> wrote in message
    news:u6QccvooEHA.3424@TK2MSFTNGP12.phx.gbl...
    > There's nowhere near enough information or context in your question to
    > answer it properly, but in general, use a binary formatter as opposed to
    > serializing as XML -- assuming your situation allows you to do so (for
    > example, both the serializer and deserializer are .NET applications).
    >
    > --Bob
    >
    > "assaf" <assafwo@hotmail.com> wrote in message
    > news:uZTxXqooEHA.2784@TK2MSFTNGP14.phx.gbl...
    > > hi all
    > >
    > > when i serialize objects,
    > > they seem to get 100 times the original size.
    > >
    > > how can i decrease the size of serialized objects?
    > >
    > >
    > > assaf
    >
    >


  • Next message: Justin Rogers: "Re: how to: decrease the size of serialized objects?"