B
    ›³ëbÐ$  ã               @   s`   d Z dai aG dd„ dƒZG dd„ deƒZG dd„ deƒZddd	„Zd
d„ Zdd„ Zdd„ Z	dS )a|  Decorator-based memoizer to count caching stats.

A decorator-based 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. In particular, it requires that the class uses a
dictionary named "_memo" to store the cached values.

Here is an example of wrapping a method that returns a computed value,
with no input parameters::

    @SCons.Memoize.CountMethodCall
    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

    @SCons.Memoize.CountDictCall(_bar_key)
    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

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.

Nc               @   s0   e Zd ZdZdd„ Zdd„ Zdd„ Zdd	„ Zd
S )ÚCounterzñ
    Base class for counting memoization hits and misses.

    We expect that the initialization in a matching decorator will
    fill in the correct class name and method name that represents
    the name of the function being counted.
    c             C   s   || _ || _d| _d| _dS )z	
        é    N)Úcls_nameÚmethod_nameÚhitÚmiss)Úselfr   r   © r   ú,lib/python3.7/site-packages/SCons/Memoize.pyÚ__init__r   s    zCounter.__init__c             C   s   | j d | j S )NÚ.)r   r   )r   r   r   r	   Úkeyy   s    zCounter.keyc             C   s   t d | j| j|  ¡ ¡ƒ d S )Nz#    {:7d} hits {:7d} misses    {}())ÚprintÚformatr   r   r   )r   r   r   r	   Údisplay{   s    zCounter.displayc             C   s*   y|   ¡ |  ¡ kS  tk
r$   dS X d S )NT)r   ÚAttributeError)r   Úotherr   r   r	   Ú__eq__}   s    zCounter.__eq__N)Ú__name__Ú
__module__Ú__qualname__Ú__doc__r
   r   r   r   r   r   r   r	   r   j   s
   r   c               @   s   e Zd ZdZdd„ ZdS )Ú
CountValuezÿ
    A counter class for simple, atomic memoized values.

    A CountValue object should be instantiated in a decorator for each of
    the class's methods that memoizes its return value by simply storing
    the return value in its _memo dictionary.
    c             O   s2   |d }| j |jkr"| jd | _n| jd | _dS )ze Counts whether the memoized value has already been
            set (a hit) or not (a miss).
        r   é   N)r   Ú_memor   r   )r   ÚargsÚkwÚobjr   r   r	   Úcount‹   s    zCountValue.countN)r   r   r   r   r   r   r   r   r	   r   ƒ   s   r   c                   s(   e Zd ZdZ‡ fdd„Zdd„ Z‡  ZS )Ú	CountDicta_  
    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 decorator 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.
    c                s   t ƒ  ||¡ || _dS )z	
        N)Úsuperr
   Úkeymaker)r   r   r   r    )Ú	__class__r   r	   r
   Ÿ   s    zCountDict.__init__c             O   sl   |d }y|j | j }W n  tk
r8   | jd | _Y n0X | j||Ž}||kr\| jd | _n| jd | _dS )z„ Counts whether the computed key value is already present
           in the memoization dictionary (a hit) or not (a miss).
        r   r   N)r   r   ÚKeyErrorr   r    r   )r   r   r   r   Z	memo_dictr   r   r   r	   r   ¤   s    zCountDict.count)r   r   r   r   r
   r   Ú__classcell__r   r   )r!   r	   r   •   s   	r   c             C   s.   | rt | ƒ xttƒD ]}t|  ¡  qW dS )zL Dump the hit/miss count for all the counters
        collected so far.
    N)r   ÚsortedÚCounterListr   )ÚtitleZcounterr   r   r	   ÚDump´   s    r'   c               C   s   da d S )Nr   )Úuse_memoizerr   r   r   r	   ÚEnableMemoization½   s    r)   c                s$   t r‡ fdd„}ˆ j|_|S ˆ S dS )a   Decorator for counting memoizer hits/misses while retrieving
        a simple value in a class method. It wraps the given method
        fn and uses a CountValue object to keep track of the
        caching statistics.
        Wrapping gets enabled by calling EnableMemoization().
    c                sT   | j jd ˆ j }|tkr.t| j jˆ jƒt|< t| j| f|ž|Ž ˆ | f|ž|ŽS )Nr   )r!   r   r%   r   r   )r   r   Úkwargsr   )Úfnr   r	   ÚwrapperÉ   s
    z CountMethodCall.<locals>.wrapperN)r(   r   )r+   r,   r   )r+   r	   ÚCountMethodCallÁ   s
    r-   c                s   ‡ fdd„}|S )aÐ   Decorator for counting memoizer hits/misses while accessing
        dictionary values with a key-generating function. Like
        CountMethodCall above, it wraps the given method
        fn and uses a CountDict object to keep track of the
        caching statistics. The dict-key function keyfunc has to
        get passed in the decorator call and gets stored in the
        CountDict instance.
        Wrapping gets enabled by calling EnableMemoization().
    c                s&   t r‡ ‡fdd„}ˆ j|_|S ˆ S d S )Nc                sV   | j jd ˆ j }|tkr0t| j jˆ jˆƒt|< t| j| f|ž|Ž ˆ | f|ž|ŽS )Nr   )r!   r   r%   r   r   )r   r   r*   r   )r+   Úkeyfuncr   r	   r,   á   s
    z1CountDictCall.<locals>.decorator.<locals>.wrapper)r(   r   )r+   r,   )r.   )r+   r	   Ú	decoratorß   s
    z CountDictCall.<locals>.decoratorr   )r.   r/   r   )r.   r	   ÚCountDictCallÕ   s    
r0   )N)
r   r(   r%   r   r   r   r'   r)   r-   r0   r   r   r   r	   Ú<module>b   s   
	