Author: alanmc
Date: 2006-12-04 12:08:46 -0500 (Mon, 04 Dec 2006)
New Revision: 68972

Modified:
   trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
   trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
Log:
Some important optimisations to vastly reduce the CPU time needed for deciding 
when a peer is interesting and when  a peer is a seeder.

Modified: trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2006-12-04 16:59:31 UTC (rev 68971)
+++ trunk/bitsharp/src/MonoTorrent.Client/Managers/PieceManager.cs      
2006-12-04 17:08:46 UTC (rev 68972)
@@ -65,6 +65,11 @@
         #region Methods
         internal bool IsInteresting(PeerConnectionID id)
         {
+            // If the peer is a seeder and i don't have all the pieces, he's 
interesting
+            if (id.Peer.IsSeeder && (this.MyBitField.TrueCount != 
this.MyBitField.Length))
+                return true;
+
+            // Otherwise we need to do a full check
             lock (this.piecePicker)
                 return this.piecePicker.IsInteresting(id);
         }

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs       
2006-12-04 16:59:31 UTC (rev 68971)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/BitfieldMessage.cs       
2006-12-04 17:08:46 UTC (rev 68972)
@@ -102,7 +102,7 @@
         /// <param name="length">The maximum number of bytes to read from the 
buffer</param>
         internal void Decode(byte[] buffer, int offset, int length)
         {
-            bitField.FromArray(buffer, offset, length);
+            this.bitField.FromArray(buffer, offset, length);
         }
 
 #warning Copy this straight into the Peers Bitfield. Slightly more memory 
efficient :p
@@ -113,26 +113,8 @@
         internal void Handle(PeerConnectionID id)
         {
             id.Peer.Connection.BitField = this.bitField;
-            for (int i = 0; i < id.Peer.Connection.BitField.Array.Length - 1; 
i++)
-                if (id.Peer.Connection.BitField.Array[0] != ~0)        // 
Check every section except the last section
-                {
-                    id.Peer.IsSeeder = false;
-                    return;
-                }
+            id.Peer.IsSeeder = (id.Peer.Connection.BitField.TrueCount == 
id.Peer.Connection.BitField.Length);
 
-
-            // Manually check each of the remaining positions in the last 
section
-            for (int i = sizeof(int) * 8 * (id.Peer.Connection.BitField.Length 
- 1); i < id.Peer.Connection.BitField.Length; i++)
-            {
-                if (!id.Peer.Connection.BitField[i])
-                {
-                    id.Peer.IsSeeder = false;
-                    return;
-                }
-            }
-
-            id.Peer.IsSeeder = true;
-
             id.Peer.Connection.IsInterestingToMe = 
id.TorrentManager.PieceManager.IsInteresting(id);
         }
 

Modified: trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs
===================================================================
--- trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs   
2006-12-04 16:59:31 UTC (rev 68971)
+++ trunk/bitsharp/src/MonoTorrent.Client/PeerMessages/HaveMessage.cs   
2006-12-04 17:08:46 UTC (rev 68972)
@@ -104,34 +104,20 @@
             this.pieceIndex = 
IPAddress.NetworkToHostOrder(BitConverter.ToInt32(buffer, offset));
         }
 
-#warning The below is duplicate code
+
         /// <summary>
         /// Performs any necessary actions required to process the message
         /// </summary>
         /// <param name="id">The Peer who's message will be handled</param>
         internal void Handle(PeerConnectionID id)
         {
-            id.Peer.Connection.BitField[this.pieceIndex] = true;
-            id.Peer.Connection.IsInterestingToMe = 
id.TorrentManager.PieceManager.IsInteresting(id);
+            // We can do a fast check to see if the peer is interesting or not 
when we receive a Have Message.
+            // If the peer just received a piece we don't have, he's 
interesting. Otherwise his state is unchanged
+            if (!id.TorrentManager.PieceManager.MyBitField[this.pieceIndex])
+                id.Peer.Connection.IsInterestingToMe = true;
 
-            for (int i = 0; i < id.Peer.Connection.BitField.Array.Length - 1; 
i++)
-                if (id.Peer.Connection.BitField.Array[0] != ~0)
-                {
-                    id.Peer.IsSeeder = false;
-                    return;
-                }
-
-
-            for (int i = 32 * (id.Peer.Connection.BitField.Length - 1); i < 
id.Peer.Connection.BitField.Length; i++)
-            {
-                if (!id.Peer.Connection.BitField[i])
-                {
-                    id.Peer.IsSeeder = false;
-                    return;
-                }
-            }
-#warning This code is duplicated in the PieceMessage i think
-            id.Peer.IsSeeder = true;
+            id.Peer.Connection.BitField[this.pieceIndex] = true;
+            id.Peer.IsSeeder = (id.Peer.Connection.BitField.TrueCount == 
id.Peer.Connection.BitField.Length);
         }
 
 

_______________________________________________
Mono-patches maillist  -  [email protected]
http://lists.ximian.com/mailman/listinfo/mono-patches

Reply via email to