| Server IP : 216.92.14.13 / Your IP : 216.73.216.171 Web Server : Apache System : Linux vps4089.pairvps.com 5.15.0-190-generic #200-Ubuntu SMP Fri Aug 7 15:06:04 UTC 2026 x86_64 User : rmlac2fmr ( 1040637) PHP Version : 8.2.32 Disable Function : NONE MySQL : OFF | cURL : ON | WGET : ON | Perl : ON | Python : ON | Sudo : ON | Pkexec : ON Directory : /lib/scons/SCons/ |
Upload File : |
�
�b3Qc @ s� d Z d Z d d l Z d a g Z d e f d � � YZ d e f d � � YZ d e f d � � YZ d
e f d � � YZ
d d � Z d
e f d � � YZ
d � Z d S( s6 src/engine/SCons/Memoize.py 2013/03/03 09:48:35 garyos� Memoizer
A metaclass implementation to count hits and misses of the computed
values that various methods cache in memory.
Use of this modules assumes that wrapped methods be coded to cache their
values in a consistent way. Here is an example of wrapping a method
that returns a computed value, with no input parameters:
memoizer_counters = [] # Memoization
memoizer_counters.append(SCons.Memoize.CountValue('foo')) # Memoization
def foo(self):
try: # Memoization
return self._memo['foo'] # Memoization
except KeyError: # Memoization
pass # Memoization
result = self.compute_foo_value()
self._memo['foo'] = result # Memoization
return result
Here is an example of wrapping a method that will return different values
based on one or more input arguments:
def _bar_key(self, argument): # Memoization
return argument # Memoization
memoizer_counters.append(SCons.Memoize.CountDict('bar', _bar_key)) # Memoization
def bar(self, argument):
memo_key = argument # Memoization
try: # Memoization
memo_dict = self._memo['bar'] # Memoization
except KeyError: # Memoization
memo_dict = {} # Memoization
self._memo['dict'] = memo_dict # Memoization
else: # Memoization
try: # Memoization
return memo_dict[memo_key] # Memoization
except KeyError: # Memoization
pass # Memoization
result = self.compute_bar_value(argument)
memo_dict[memo_key] = result # Memoization
return result
At one point we avoided replicating this sort of logic in all the methods
by putting it right into this module, but we've moved away from that at
present (see the "Historical Note," below.).
Deciding what to cache is tricky, because different configurations
can have radically different performance tradeoffs, and because the
tradeoffs involved are often so non-obvious. Consequently, deciding
whether or not to cache a given method will likely be more of an art than
a science, but should still be based on available data from this module.
Here are some VERY GENERAL guidelines about deciding whether or not to
cache return values from a method that's being called a lot:
-- The first question to ask is, "Can we change the calling code
so this method isn't called so often?" Sometimes this can be
done by changing the algorithm. Sometimes the *caller* should
be memoized, not the method you're looking at.
-- The memoized function should be timed with multiple configurations
to make sure it doesn't inadvertently slow down some other
configuration.
-- When memoizing values based on a dictionary key composed of
input arguments, you don't need to use all of the arguments
if some of them don't affect the return values.
Historical Note: The initial Memoizer implementation actually handled
the caching of values for the wrapped methods, based on a set of generic
algorithms for computing hashable values based on the method's arguments.
This collected caching logic nicely, but had two drawbacks:
Running arguments through a generic key-conversion mechanism is slower
(and less flexible) than just coding these things directly. Since the
methods that need memoized values are generally performance-critical,
slowing them down in order to collect the logic isn't the right
tradeoff.
Use of the memoizer really obscured what was being called, because
all the memoized methods were wrapped with re-used generic methods.
This made it more difficult, for example, to use the Python profiler
to figure out how to optimize the underlying methods.
i����Nt Counterc B s) e Z d Z d � Z d � Z d � Z RS( s�
Base class for counting memoization hits and misses.
We expect that the metaclass initialization will have filled in
the .name attribute that represents the name of the function
being counted.
c C s, | | _ d | _ d | _ t j | � d S( s
i N( t method_namet hitt misst CounterListt append( t selfR ( ( s /usr/lib/scons/SCons/Memoize.pyt __init__� s c C s% d } | | j | j | j f GHd S( Ns %7d hits %7d misses %s()( R R t name( R t fmt( ( s /usr/lib/scons/SCons/Memoize.pyt display� s c C s0 y t | j | j � SWn t k
r+ d SXd S( Ni ( t cmpR t AttributeError( R t other( ( s /usr/lib/scons/SCons/Memoize.pyt __cmp__� s
( t __name__t
__module__t __doc__R R
R ( ( ( s /usr/lib/scons/SCons/Memoize.pyR � s t
CountValuec B s e Z d Z d � Z RS( s
A counter class for simple, atomic memoized values.
A CountValue object should be instantiated in a class for each of
the class's methods that memoizes its return value by simply storing
the return value in its _memo dictionary.
We expect that the metaclass initialization will fill in the
.underlying_method attribute with the method that we're wrapping.
We then call the underlying_method method after counting whether
its memoized value has already been set (a hit) or not (a miss).
c O sO | d } | j | j k r/ | j d | _ n | j d | _ | j | | � S( Ni i ( R t _memoR R t underlying_method( R t argst kwt obj( ( s /usr/lib/scons/SCons/Memoize.pyt __call__� s
( R R R R ( ( ( s /usr/lib/scons/SCons/Memoize.pyR � s t CountDictc B s e Z d Z d � Z d � Z RS( s�
A counter class for memoized values stored in a dictionary, with
keys based on the method's input arguments.
A CountDict object is instantiated in a class for each of the
class's methods that memoizes its return value in a dictionary,
indexed by some key that can be computed from one or more of
its input arguments.
We expect that the metaclass initialization will fill in the
.underlying_method attribute with the method that we're wrapping.
We then call the underlying_method method after counting whether the
computed key value is already present in the memoization dictionary
(a hit) or not (a miss).
c C s t j | | � | | _ d S( s
N( R R t keymaker( R R R ( ( s /usr/lib/scons/SCons/Memoize.pyR � s c O s� | d } y | j | j } Wn! t k
rA | j d | _ nB X| j | | � } | | k rs | j d | _ n | j d | _ | j | | � S( Ni i ( R R t KeyErrorR R R R ( R R R R t memo_dictt key( ( s /usr/lib/scons/SCons/Memoize.pyR � s
( R R R R R ( ( ( s /usr/lib/scons/SCons/Memoize.pyR � s t Memoizerc B s e Z d Z d � Z RS( sM Object which performs caching of method calls for its 'primary'
instance.c C s d S( N( ( R ( ( s /usr/lib/scons/SCons/Memoize.pyR � s ( R R R R ( ( ( s /usr/lib/scons/SCons/Memoize.pyR � s c C s7 | r | GHn t j � x t D] } | j � q Wd S( N( R t sortR
( t titlet counter( ( s /usr/lib/scons/SCons/Memoize.pyt Dump� s
t Memoized_Metaclassc B s e Z d � Z RS( c C s� t t | � j | | | � xi | j d g � D]U } | j } | j d | | _ | | | _ t j | d | � } t | | | � q/ Wd S( Nt memoizer_counterst .( t superR# R t getR R R R t typest
MethodTypet Nonet setattr( t clsR t basest cls_dictR! R t replacement_method( ( s /usr/lib/scons/SCons/Memoize.pyR � s
( R R R ( ( ( s /usr/lib/scons/SCons/Memoize.pyR# � s c C s
d a d S( Ni ( t use_memoizer( ( ( s /usr/lib/scons/SCons/Memoize.pyt EnableMemoization� s ( t __revision__R R( R* R0 R t objectR R R R R"