# 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
|
|
23 | 23 | cdef class CachedMethod(object): |
24 | 24 | cdef str _cache_name |
25 | 25 | cdef CachedFunction _cachedfunc |
| 26 | cdef int nargs |
26 | 27 | cpdef dict _get_instance_cache(self, inst) |
27 | 28 | |
diff --git a/sage/misc/cachefunc.pyx b/sage/misc/cachefunc.pyx
a
|
b
|
|
346 | 346 | ######################################################################## |
347 | 347 | from function_mangling import ArgumentFixer |
348 | 348 | import os |
349 | | from sage.misc.sageinspect import sage_getfile, sage_getsourcelines |
| 349 | from sage.misc.sageinspect import sage_getfile, sage_getsourcelines, sage_getargspec |
350 | 350 | |
351 | 351 | def _cached_function_unpickle(module,name): |
352 | 352 | """ |
… |
… |
|
611 | 611 | False)) |
612 | 612 | |
613 | 613 | """ |
614 | | from sage.misc.sageinspect import sage_getargspec |
615 | 614 | return sage_getargspec(self.f) |
616 | 615 | |
617 | 616 | def __call__(self, *args, **kwds): |
… |
… |
|
1866 | 1865 | # Since we have an optimized version for functions that do not accept arguments, |
1867 | 1866 | # we need to analyse the argspec |
1868 | 1867 | 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: |
1872 | 1879 | Caller = CachedMethodCallerNoArgs(inst, f, name=name) |
1873 | 1880 | else: |
1874 | 1881 | Caller = CachedMethodCaller(self, inst, |
1875 | | cache=self._get_instance_cache(inst), name=name) |
| 1882 | cache=self._get_instance_cache(inst), |
| 1883 | name=name) |
1876 | 1884 | try: |
1877 | 1885 | setattr(inst,name, Caller) |
1878 | 1886 | return Caller |