Ticket #198: type_map_cache.diff

File type_map_cache.diff, 1.1 KB (added by akaihola, 11 months ago)

performance patch: caches encoder functions keyed by element class, all tests pass

  • pyamf/__init__.py

     
    537537            raise TypeError, "context must be of type %s.%s" % ( 
    538538                self.context_class.__module__, self.context_class.__name__) 
    539539 
    540     def _writeElementFunc(self, data): 
     540        self._write_elem_func_cache = {} 
     541 
     542    def _getWriteElementFunc(self, data): 
    541543        """ 
    542544        Gets a function used to encode the data. 
    543545 
     
    565567 
    566568        return None 
    567569 
     570    def _writeElementFunc(self, data): 
     571        """ 
     572        Gets a function used to encode the data. 
     573 
     574        @type   data: C{mixed} 
     575        @param  data: Python data. 
     576        @rtype: callable or C{None}. 
     577        @return: The function used to encode data to the stream. 
     578        """ 
     579        key = data.__class__ 
     580        if key not in self._write_elem_func_cache: 
     581            self._write_elem_func_cache[key] =  self._getWriteElementFunc(data) 
     582        return self._write_elem_func_cache[key] 
     583 
    568584    def writeElement(self, data): 
    569585        """ 
    570586        Writes the data.