Ticket #13159: trac13159_cache_nargs_of_cached_methods.patch

File trac13159_cache_nargs_of_cached_methods.patch, 2.6 KB (added by SimonKing, 10 years ago)
  • sage/misc/cachefunc.pxd

    # HG changeset patch
    # User Simon King <simon.king@uni-jena.de>
    # Date 1340616012 -7200
    # Node ID 2789887d2eb2950e672b3404440cb2bdbc1b2681
    # Parent  382c7a1cdac02dbd9ba51daabf15bf44f224ab90
    #13159: Cache the number of arguments of a cached method, to avoid repeated source inspection.
    
    diff --git a/sage/misc/cachefunc.pxd b/sage/misc/cachefunc.pxd
    a b  
    2323cdef class CachedMethod(object):
    2424    cdef str _cache_name
    2525    cdef CachedFunction _cachedfunc
     26    cdef int nargs
    2627    cpdef dict _get_instance_cache(self, inst)
    2728
  • sage/misc/cachefunc.pyx

    diff --git a/sage/misc/cachefunc.pyx b/sage/misc/cachefunc.pyx
    a b  
    346346########################################################################
    347347from function_mangling import ArgumentFixer
    348348import os
    349 from sage.misc.sageinspect import sage_getfile, sage_getsourcelines
     349from sage.misc.sageinspect import sage_getfile, sage_getsourcelines, sage_getargspec
    350350
    351351def _cached_function_unpickle(module,name):
    352352    """
     
    611611            False))
    612612
    613613        """
    614         from sage.misc.sageinspect import sage_getargspec
    615614        return sage_getargspec(self.f)
    616615
    617616    def __call__(self, *args, **kwds):
     
    18661865        # Since we have an optimized version for functions that do not accept arguments,
    18671866        # we need to analyse the argspec
    18681867        f = (<CachedFunction>self._cachedfunc).f
    1869         from sage.misc.sageinspect import sage_getargspec
    1870         args, varargs, keywords, defaults = sage_getargspec(f)
    1871         if varargs is None and keywords is None and len(args)<=1:
     1868        if self.nargs==0:
     1869            args, varargs, keywords, defaults = sage_getargspec(f)
     1870            if varargs is None and keywords is None and len(args)<=1:
     1871                self.nargs = 1
     1872                Caller = CachedMethodCallerNoArgs(inst, f, name=name)
     1873            else:
     1874                self.nargs = 2 # don't need the exact number
     1875                Caller = CachedMethodCaller(self, inst,
     1876                                            cache=self._get_instance_cache(inst),
     1877                                            name=name)
     1878        elif self.nargs==1:
    18721879            Caller = CachedMethodCallerNoArgs(inst, f, name=name)
    18731880        else:
    18741881            Caller = CachedMethodCaller(self, inst,
    1875             cache=self._get_instance_cache(inst), name=name)
     1882                                        cache=self._get_instance_cache(inst),
     1883                                        name=name)
    18761884        try:
    18771885            setattr(inst,name, Caller)
    18781886            return Caller