[Twisted-Python] twisted 30% slower than asyncore?

Anthony Baxter anthony at interlink.com.au
Thu May 1 04:44:31 EDT 2003


The following patch to t.i.default.py removes the creation of
the 'Failure' objects in the common case of connectionDone or
connectionLost, instead using a cache of two existing objects. The
second patch (which I've already emailed to Itamar, but will send here
as well for completeness) makes a similar change to the code in tcp.py.
The two combined make a test over the loopback interface go from around
300 request/second to about 450 requests/second when serving very small
objects (2K) from apache on the same host. There's a bunch of other
speedups possible in SelectReactor.doSelect() - making globals local,
&c.

Anthony


--- ../../../../twisted/internet/default.py     Tue Apr 22 01:32:15 2003
+++ default.py  Thu May  1 18:38:20 2003
@@ -422,7 +422,12 @@
                  reads=reads,
                  writes=writes,
                  rhk=reads.has_key,
-                 whk=writes.has_key):
+                 whk=writes.has_key,
+                 faildict={
+                  error.ConnectionDone:failure.Failure(error.ConnectionDone()),
+                  error.ConnectionLost:failure.Failure(error.ConnectionLost()),
+                 }
+                ):
         """Run one iteration of the I/O monitor loop.
 
         This will run all selectables who had input or output readiness
@@ -479,7 +484,11 @@
                     self.removeReader(selectable)
                     self.removeWriter(selectable)
                     try:
-                        selectable.connectionLost(failure.Failure(why))
+                        f = faildict.get(why.__class__)
+                        if f:
+                            selectable.connectionLost(f)
+                        else:
+                            selectable.connectionLost(failure.Failure(why))
                     except:
                         log.deferr()
                 log.logOwner.disown(selectable)


--- twisted/internet/tcp.py     22 Apr 2003 20:31:16 -0000      1.115
+++ twisted/internet/tcp.py     1 May 2003 06:55:26 -0000
@@ -468,7 +468,7 @@
         except:
             log.deferr()
 
-    def loseConnection(self):
+    def loseConnection(self, connDone=failure.Failure(main.CONNECTION_DONE)):
         """Stop accepting connections on this port.
 
         This will shut down my socket and call self.connectionLost().
@@ -476,8 +476,7 @@
         self.disconnecting = 1
         self.stopReading()
         if self.connected:
-            self.reactor.callLater(0, self.connectionLost,
-                                   failure.Failure(main.CONNECTION_DONE))
+            self.reactor.callLater(0, self.connectionLost, connDone)
 
     stopListening = loseConnection
 




More information about the Twisted-Python mailing list