[XML-SIG] Bug in exception handling?

Rob Hooft r.hooft@euromail.net
Thu, 24 Jun 1999 16:16:43 +0200 (MZT)


>>>>> "LMG" == Lars Marius Garshol <larsga@ifi.uio.no> writes:

 LMG> Feedback on speed differences between these three drivers (original,
 LMG> the one on the web and the one in this post) would be interesting.

devel[445]cubic%% ls -l final.y
-rw-r--r--   1 hooft    hooft     3963562 Jun 24 13:04 final.y

My sax-less version:

   Reading reflection file... 43.05 seconds

The original:

   Reading reflection file... 74.37 seconds

The Web version (without activating the lazy code):

   Reading reflection file... 348.88 seconds

Oops. something else changed?

I made up a lazy version myself, using the old 0.10 version of the file, and
a lazy map that is a bit less lazy than the one you made up.

   Reading reflection file... 71.87 seconds

Conclusion: this is not the real problem, making up the dictionary is 
not so expensive in comparison with the rest of the SAX layer.
For completeness, here is my added code:

class LazyExpatDriver(SAX_expat):

    def startElement(self,name,attrs):
        self.doc_handler.startElement(name,LazyAttributeMap(attrs))    
            
# --- A lazy attribute map

# This avoids the costly conversion from a list to a hash table if the attribute
# list is not needed anywhere.

class LazyAttributeMap:
    """An implementation of AttributeList that takes an (attr,val) hash
    and uses it to implement the AttributeList interface."""    

    def __init__(self, list):
        self.lst=list
        self.map=None

    def _mkmap(self):
        self.map={}
        for i in range(0,len(self.lst),2):
            self.map[self.lst[i]]=self.lst[i+1]
    
    def getLength(self):
        return len(self.list()/2)
        
    def getName(self, i):
        if self.map is None:
            self._mkmap()
        try:
            return self.map.keys()[i]
        except IndexError,e:
            return None

    def getType(self, i):
        return "CDATA"

    def getValue(self, i):
        if self.map is None:
            self._mkmap()
        try:
            if type(i)==types.IntType:
                return self.map[self.getName(i)]
            else:
                return self.map[i]
        except KeyError,e:
            return None

    def __len__(self):
        return len(self.lst()/2)

    def __getitem__(self, key):
        if self.map is None:
            self._mkmap()
        if type(key)==types.IntType:
            return self.map.keys()[key]
        else:
            return self.map[key]

    def items(self):
        if self.map is None:
            self._mkmap()
        return self.map.items()
        
    def keys(self):
        if self.map is None:
            self._mkmap()
        return self.map.keys()

    def has_key(self,key):
        if self.map is None:
            self._mkmap()
        return self.map.has_key(key)

    def get(self, key, alternative):
        """Return the value associated with attribute name; if it is not
        available, then return the alternative."""
        if self.map is None:
            self._mkmap()
        return self.map.get(key, alternative)

# ---
        
def create_parser():
    #return SAX_expat() 
    return LazyExpatDriver()