Sections
Timeline
View Tickets
New Ticket
Sub-Sections
Download
Unified Diff
Zip Archive
Metanav
Preferences
About Trac
Links
Slowchop Studios
Gerald Kaszuba
Advertisement

Changeset 58

Show
Ignore:
Timestamp:
10/12/07 07:03:15 (12 months ago)
Author:
gak
Message:

Patch from Igor V. Rafienko to increase speed by about 50% by caching getmodule

Files:
1 modified

Legend:

Unmodified
Added
Removed
  • trunk/pycallgraph.py

    r55 r58  
    380380        os.unlink(tempname) 
    381381 
     382 
     383def simple_memoize(callable_object): 
     384    """Simple memoization for functions without keyword arguments. 
     385 
     386    This is useful for mapping code objects to module in this context. 
     387    inspect.getmodule() requires a number of system calls, which may slow down 
     388    the tracing considerably. Caching the mapping from code objects (there is 
     389    *one* code object for each function, regardless of how many simultaneous 
     390    activations records there are). 
     391 
     392    In this context we can ignore keyword arguments, but a generic memoizer 
     393    ought to take care of that as well. 
     394    """ 
     395 
     396    cache = dict() 
     397    def wrapper(*rest): 
     398        if rest not in cache: 
     399            cache[rest] = callable_object(*rest) 
     400        return cache[rest] 
     401 
     402    return wrapper 
     403 
     404     
    382405settings = {} 
    383406graph_attributes = {} 
    384407reset_settings() 
    385408reset_trace() 
     409inspect.getmodule = simple_memoize(inspect.getmodule) 
    386410 
    387411# vim:set shiftwidth=4 tabstop=4 expandtab textwidth=79: