[Twisted-Python] [patch] Performance of Conch/SSH

Mika Seppanen cooz at ee.oulu.fi
Thu Nov 10 07:27:31 EST 2005


When I was profiling my code I noticed that most of time is spent on simple counter in twisted/conch/ssh/transport.py. So I rewrote _Counter-class. My version is about 5 times faster than orginal and data transfer rate is almost doubled. Maybe someone could check that this is not breaking anything?

Mika

--- transport.py	2005-11-10 13:26:01.826437000 +0200
+++ transport.py.new	2005-11-10 13:35:57.804232000 +0200
@@ -20,6 +20,7 @@
 import sha
 import zlib
 import math # for math.log
+import array
 
 # external library imports
 from Crypto import Util
@@ -727,17 +728,22 @@
         iv=iv[:mod.block_size]
         self.count = getMP('\xff\xff\xff\xff'+iv)[0]
         self.bs = mod.block_size
+        self.count = Util.number.long_to_bytes(self.count - 1)
+        self.count = '\x00'*(self.bs-len(self.count)) + self.count
+        self.count = array.array('c', self.count) 
+        self.len = len(self.count) - 1
+
     def __call__(self):
-        ret = MP(self.count)[4:]
-        if ret[0]=='\x00':
-            ret=ret[1:]
-        if len(ret) < self.bs:
-            ret = '\x00'*(self.bs-len(ret)) + ret
-        self.count += 1
-        if self.count == 2L ** self.bs:
-            self.count = 0
-        return ret
+        i = self.len
+        while i > -1:
+            self.count[i] = n = chr((ord(self.count[i]) + 1) % 256)
+            if n == '\x00':
+                i -= 1
+            else:
+                return self.count.tostring()
 
+        self.count = array.array('c', '\x00'*self.bs)
+        return self.count.tostring()
 
 def buffer_dump(b, title = None):
     r = title or ''




More information about the Twisted-Python mailing list