
    *e9                        d Z ddlmZ ddlZddlZddlmZ ddlmZm	Z	m
Z
 ddlmZmZ ddlmZ ddlmZ ddZdddZddZdS )zUtility for dumping memory usage stats.

This is tailored to mypy and knows (a little) about which list objects are
owned by particular AST nodes, etc.
    )annotationsN)defaultdict)DictIterablecast)FakeInfoNode)Type)get_class_descriptorsreturn%tuple[dict[str, int], dict[str, int]]c                 h   t          j                    } t          |            i }| D ]y}t          |          t          u rt          |          j        }t          |d          r| d|t          |j                  <   t          |t          t          f          rt          |d          rp|j                                        D ]V}t          |t                    r| d|t          |          <   t          |t                    r| d|t          |          <   Wt          t          |                    D ]g}t!          ||d          }t          |t                    r| d|t          |          <   t          |t                    r| d|t          |          <   h{i }i }| D ]}t          |          |v r|t          |                   }nt          |          j        }|                    |d          dz   ||<   |                    |d          t%          j        |          z   ||<   ||fS )zReturn stats about memory use.

    Return a tuple with these items:
      - Dict from object kind to number of instances of that kind
      - Dict from object kind to total bytes used by all instances of that kind
    __dict__z (__dict__)z (list)z (tuple)Nr      )gcget_objectsfind_recursive_objectstyper   __name__hasattridr   
isinstancer	   r
   valueslisttupler   getattrgetsys	getsizeof)	objsinferredobjnxkfreqsmemusenames	            /lib/python3.11/site-packages/mypy/memprofile.pycollect_memory_statsr*      s0    >D4   H 5 599  II3
## 	;,-):):):HR%%&cD$<(( 	5sJ'' 9,,.. 9 9A!!T** 8-.---A!!U++ 9-....A*49955 5 5CD))a&& 4)*mmmHRUUOa'' 5)*nnnHRUUOEF @ @c77hBsGG$DD99%Diia((1,dzz$**S]3-?-??t&=    Trun_gcboolNonec                J   t           j                            d          s$dd l}|                    |j                  j        }nd}| rt          j                     t                      \  }}t          ddz             t          d           d}d}t          |                                d           D ]9\  }}||         }	|d	k     rt          d
|	|dz  ||	z  |fz             |dz  }||z  }:t                       t          d|dz             t          d|dz             d S )Nwinr   z%7s  %7s  %7s  %s)FreqzSize(k)AvgSizer
   z+-------------------------------------------c                    | d          S )Nr    )r$   s    r)   <lambda>z&print_memory_profile.<locals>.<lambda>R   s    !u r+   )key2   z%7d  %7d  %7.0f  %si   r   zMem usage RSS   zTotal reachable )r   platform
startswithresource	getrusageRUSAGE_SELF	ru_maxrssr   collectr*   printsorteditems)
r,   r;   system_memuser&   r'   totalmemir#   memfs
             r)   print_memory_profilerH   D   sC   <""5))  **8+?@@J 

(**ME6	
 F
FGGG	
7888H	A__===  3!Hr66'1cTk37A*FFGGG	QC	GGG	
mt3444	
h$./////r+   r    list[object]c           	         d  D             d
 fd}                                  D ]T}t          |          t          u rt          |          t          t          fv rUt          t          t          t          f         |                                          D ]\  }} ||            ||           t          |          t          t          t          fv r.t          t          t                   |          D ]} ||           t          |d          rgt                              t          |                    D ]?}t          |dd          D ]+}t          ||          r |t          ||                     ,@Vd	S )zFind additional objects referenced by objs and append them to objs.

    We use this since gc.get_objects() does not return objects without pointers
    in them such as strings.
    c                ,    h | ]}t          |          S r5   )r   ).0os     r)   	<setcomp>z)find_recursive_objects.<locals>.<setcomp>c   s       aBqEE   r+   rM   objectr   r.   c                    t          |           vr9                    |                                t          |                      d S d S )N)r   appendadd)rM   r    seens    r)   visitz%find_recursive_objects.<locals>.visite   sD    a55KKNNNHHRUUOOOOO r+   	__slots__r5   N)rM   rO   r   r.   )copyr   r   dictr   r   r   rO   rB   r   r   setr   r   mror   )	r    rT   r"   r7   valr$   baseslotrS   s	   `       @r)   r   r   ]   s    ! 4   D      
 yy{{ 2 299  99{+++ ffn!5s;;AACC  Sc


c



99uc***(6*C00  a3$$ 	2c++ 2 2#D+r:: 2 2DsD)) 2gc40011122 2r+   )r   r   )T)r,   r-   r   r.   )r    rI   r   r.   )__doc__
__future__r   r   r   collectionsr   typingr   r   r   
mypy.nodesr   r	   
mypy.typesr
   	mypy.utilr   r*   rH   r   r5   r+   r)   <module>rd      s     # " " " " " 				 



 # # # # # # ' ' ' ' ' ' ' ' ' ' % % % % % % % %       + + + + + +. . . .b0 0 0 0 022 2 2 2 2 2r+   