[CalendarServer-changes] [1024] CalendarServer/trunk/lib-patches/Twisted/twisted.web2.dav.element. parser.patch

source_changes at macosforge.org source_changes at macosforge.org
Thu Jan 11 12:51:00 PST 2007


Revision: 1024
          http://trac.macosforge.org/projects/calendarserver/changeset/1024
Author:   wsanchez at apple.com
Date:     2007-01-11 12:51:00 -0800 (Thu, 11 Jan 2007)

Log Message:
-----------
Tweak the XML parser to cache the subclasses it creates, thereby
potentially significantly reducing the number of classes it creates.
Important because python leaks a weakref for every class instantiated,
up to the maximum number of classes-in-flight at any given time.

Fixes #101

Added Paths:
-----------
    CalendarServer/trunk/lib-patches/Twisted/twisted.web2.dav.element.parser.patch

Added: CalendarServer/trunk/lib-patches/Twisted/twisted.web2.dav.element.parser.patch
===================================================================
--- CalendarServer/trunk/lib-patches/Twisted/twisted.web2.dav.element.parser.patch	                        (rev 0)
+++ CalendarServer/trunk/lib-patches/Twisted/twisted.web2.dav.element.parser.patch	2007-01-11 20:51:00 UTC (rev 1024)
@@ -0,0 +1,61 @@
+Index: twisted/web2/dav/element/parser.py
+===================================================================
+--- twisted/web2/dav/element/parser.py	(revision 18545)
++++ twisted/web2/dav/element/parser.py	(working copy)
+@@ -106,6 +106,26 @@
+             "children"   : [],
+         }]
+ 
++        # Keep a cache of the subclasses we create for unknown XML
++        # elements, so that we don't create multiple classes for the
++        # same element; it's fairly typical for elements to appear
++        # multiple times in a document.
++        self.unknownElementClasses = {}
++
++        # New-style classes keep weak references to all subclasses.
++        # As a result, the subclasses we create continue to effect the
++        # footprint of WebDAVUnknownElement even after they are
++        # deallocated, up to the maximum number of subclasses that exist
++        # simultaneously.  The number of such weak references doesn't appear
++        # to decrease.  In order to avoid growing it unneccessarily, create a
++        # subclass for use by this document, subclass the new class instead,
++        # and then delete it when we're done, thereby adding only one weak
++        # reference to WebDAVUnknownElement's list.
++        # http://trac.macosforge.org/projects/calendarserver/ticket/101
++        class UnknownElement (WebDAVUnknownElement):
++            pass
++        self.unknownElementClass = UnknownElement
++
+     def endDocument(self):
+         top = self.stack[-1]
+ 
+@@ -115,6 +135,8 @@
+         assert len(top["children"]) is 1, "Must have exactly one root element, got %d" % len(top["children"])
+ 
+         self.dom = WebDAVDocument(top["children"][0])
++        del(self.unknownElementClasses)
++        del(self.unknownElementClass)
+ 
+     def startElementNS(self, name, qname, attributes):
+         attributes_dict = {}
+@@ -125,13 +147,16 @@
+ 
+         tag_namespace, tag_name = name
+ 
+-        if (name not in elements_by_tag_name):
+-            class UnknownElement (WebDAVUnknownElement):
++        if name in elements_by_tag_name:
++            element_class = elements_by_tag_name[name]
++        elif name in self.unknownElementClasses:
++            element_class = self.unknownElementClasses[name]
++        else:
++            class UnknownElement (self.unknownElementClass):
+                 namespace = tag_namespace
+                 name      = tag_name
+             element_class = UnknownElement
+-        else:
+-            element_class = elements_by_tag_name[name]
++            self.unknownElementClasses[name] = UnknownElement
+ 
+         self.stack.append({
+             "name"       : name,

-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://lists.macosforge.org/pipermail/calendarserver-changes/attachments/20070111/765c83be/attachment.html


More information about the calendarserver-changes mailing list