
    i-                         S SK r S SKrS SKrS SKJr  S SKJr  S SKJr  S SK	J
r
  S SKJr  S SKJr   " S S5      r\" S	/ S
Q5      rS r " S S5      r\" SSS/5      r " S S\5      rg)    N)
namedtuple)Sequence)contextmanager)cached_property)configc                   0    \ rS rSrSrS/rS rS rS rSr	g)	RecordLLVMPassTimings   z9A helper context manager to track LLVM pass timings.
    _datac                 2    [         R                  " S5        U $ )z)Enables the pass timing in LLVM.
        T)llvmset_time_passesselfs    W/var/www/html/trading/venv/lib/python3.13/site-packages/numba/misc/llvm_pass_timings.py	__enter__RecordLLVMPassTimings.__enter__   s     	T"    c                 d    [         R                  " 5       U l        [         R                  " S5        g)z2Reset timings and save report internally.
        FN)r   report_and_reset_timingsr   r   )r   exc_valexc_typeexc_tbs       r   __exit__RecordLLVMPassTimings.__exit__   s%     224
U#r   c                 ,    [        U R                  5      $ )zTRetrieve timing data for processing.

Returns
-------
timings: ProcessedPassTimings
)ProcessedPassTimingsr   r   s    r   getRecordLLVMPassTimings.get!   s     $DJJ//r   )r   N)
__name__
__module____qualname____firstlineno____doc__	__slots__r   r   r   __static_attributes__ r   r   r	   r	      s     	I0r   r	   PassTimingRecord)
	user_timeuser_percentsystem_timesystem_percentuser_system_timeuser_system_percent	wall_timewall_percent	pass_nameinstructionc                    ^^ U S   mTR                   S:X  d   eU4S jnS Vs/ s H
  o!" U5      PM     snm[        S U 5      nU4S jn[        [        XC5      5      $ s  snf )zAdjust timing records because of truncated information.

Details: The percent information can be used to improve the timing
information.

Returns
-------
res: List[PassTimingRecord]
Totalc                 `   >^^^ U  S3mU  S3m[         R                  " T5      mUUUU4S jnU$ )N_time_percentc                 4   > T" T5      nXT   -  S-  nX T'   U $ )z'Compute percent x total_time = adjustedg{Gz?r'   )dtotaladjustedpercent_attr	time_attrtime_getter	total_recs      r   adjust6_adjust_timings.<locals>.make_adjuster.<locals>.adjustN   s*    	*E.5H#iLHr   )operator
attrgetter)attrrA   r=   r>   r?   r@   s     @@@r   make_adjuster&_adjust_timings.<locals>.make_adjusterI   s;    fEN	x()))4	 	 r   )usersystemuser_systemwallc                 "    U R                  5       $ N)_asdictxs    r   <lambda>!_adjust_timings.<locals>.<lambda>]   s
    !))+r   c                 <   > T H  nU" U 5      n M     [        S0 U D6$ )Nr'   )r(   )r:   fnadj_fnss     r   chained _adjust_timings.<locals>.chained_   s&    B1A   $!$$r   )r1   maplist)recordsrF   rP   dictsrV   rU   r@   s        @@r   _adjust_timingsr\   <   st     I')))  #L"KQa"KG
 %w/E% G#$$s   Ac                   \    \ rS rSrSrS rS rS rS rS r	S r
SS	 jr\S
 5       rS rSrg)r   i   zA class for processing raw timing report from LLVM.

The processing is done lazily so we don't waste time processing unused
timing information.
c                     Xl         g rM   	_raw_data)r   raw_datas     r   __init__ProcessedPassTimings.__init__p   s    !r   c                 ,    [        U R                  5      $ rM   )boolra   r   s    r   __bool__ProcessedPassTimings.__bool__s   s    DNN##r   c                     U R                   $ )z7Returns the raw string data.

Returns
-------
res: str
r`   r   s    r   get_raw_data!ProcessedPassTimings.get_raw_datav   s     ~~r   c                 <    U R                  5       S   R                  $ )zHCompute the total time spend in all passes.

Returns
-------
res: float
r4   )list_recordsr/   r   s    r   get_total_time#ProcessedPassTimings.get_total_time   s       "2&000r   c                     U R                   $ )z[Get the processed data for the timing report.

Returns
-------
res: List[PassTimingRecord]
)
_processedr   s    r   rm   !ProcessedPassTimings.list_records   s     r   c                     U R                  5       n[        R                  " S5      n[        R                  " XSS U5      $ )aG  Returns the top(n) most time-consuming (by wall-time) passes.

Parameters
----------
n: int
    This limits the maximum number of items to show.
    This function will show the ``n`` most time-consuming passes.

Returns
-------
res: List[PassTimingRecord]
    Returns the top(n) most time-consuming passes in descending order.
r/   Nr4   )rm   rC   rD   heapqnlargest)r   nrZ   keys       r   list_topProcessedPassTimings.list_top   s9     ##%!!+.~~a"s33r   c           	        ^^ / mSU-  mUU4S jnU" SU R                  5       S S35        U" S5        U R                  U5       H4  nU" SUR                  S SUR                  S	 S
UR                   35        M6     SR                  T5      $ )aJ  Return a string summarizing the timing information.

Parameters
----------
topn: int; optional
    This limits the maximum number of items to show.
    This function will show the ``topn`` most time-consuming passes.
indent: int; optional
    Set the indentation level. Defaults to 0 for no indentation.

Returns
-------
res: str
 c                 0   > TR                  T U  35        g rM   )append)argbufprefixs    r   ap(ProcessedPassTimings.summary.<locals>.ap   s    JJ&#'(r   zTotal .4fszTop timings:z  zs (5z%) 
)rn   rx   r/   r0   r1   join)r   topnindentr   pr   r   s        @@r   summaryProcessedPassTimings.summary   s     v	) 	VD'')#.a01
>t$AAKK$Cq'9Q[[MJK %yy~r   c                 "    U R                  5       $ )zdA cached property for lazily processing the data and returning it.

See ``_process()`` for details.
)_processr   s    r   rq   ProcessedPassTimings._processed   s     }}r   c                 T    S n[        U" U R                  5      5      n[        U5      $ )zParses the raw string data from LLVM timing report and attempts
to improve the data by recomputing the times
(See `_adjust_timings()``).
c           	   3     #    U R                  5       nSnSU S3n[        U5      nSSSSSS	S
.nU H[  n[        R                  " X65      nU(       d  M"  [        R                  " SU5      nU V	s/ s H  oU	R                  5          PM     n
n	  O   W
S   S	:X  d   e/ nSnSnU
SS  HC  n	U	S:X  a  X-  nM  UR                  U	 S35        UR                  U	 S35        USU SU S3-  nME     0 n[        R                   H  n	X;  d  M
  U	S	:w  d  M  SX'   M     US-  nU H  n[        R                  " X5      nUc  M  [        UR                  5       5      n [        X5       V	Vs0 s H  u  pXb  [        U5      OS_M     nn	nUR                  U5        U S   n[        SS	U0UD6nUv   UR                  S:X  d  M    O   SR                  U5      nU(       a  [!        SU 35      egs  sn	f s  snn	f 7f)z_A generator that parses the raw_data line-by-line to extract
timing information for each pass.
z[a-zA-Z+ ]+z(?:\s*-+z-+)+rH   rI   rJ   rK   r2   r1   )z	User TimezSystem TimezUser+Systemz	Wall TimeInstrNamez[a-zA-Z][a-zA-Z+ ]+r4   z\s*((?:[0-9]+\.)?[0-9]+) Nr7   r8   z\s+(?:z\s*\(z%\)|-+)g        z\s*(.*)r5   r   z'unexpected text after parser finished:
r'   )
splitlinesiterrematchfindallstripr}   r(   _fieldsrY   groupszipfloatupdater1   r   
ValueError)rb   lines	colheadermulticolheaders	line_iter
header_maplnmraw_headerskheadersattrsrv   patmissingvdatar1   rec	remainings                       r   parse,ProcessedPassTimings._process.<locals>.parse   s2     '')E&I!))D9OUI $',#&#J  HH_11"$**-CR"HK>IJk!'')4kGJ   2;+---E+ACSb\%HCLLA3e-LLA3h0fQCuQCw77C " G%-->a;&6!$GJ . :CHHS%=#AHHJ/H(+E(<>(< =uQxcA(<  >KK( (I* "+/3C I}}/    		),I >ykJ  Q K6>s=   AG3G3.G(
A;G3	G3)G3>'G3%G-8G3=6G3)rY   ra   r\   )r   r   rZ   s      r   r   ProcessedPassTimings._process   s)    C	L uT^^,-w''r   r`   N)   r   )r    r!   r"   r#   r$   rc   rg   rj   rn   rm   rx   r   r   rq   r   r&   r'   r   r   r   r   i   sD    "$14$6  M(r   r   NamedTimingsnametimingsc                   l    \ rS rSrSrS r\S 5       rS rS r	S r
\S 5       rSS	 jrS
 rS rS rSrg)PassTimingsCollectioni  z|A collection of pass timings.

This class implements the ``Sequence`` protocol for accessing the
individual timing records.
c                     Xl         / U l        g rM   _name_records)r   r   s     r   rc   PassTimingsCollection.__init__   s    
r   c              #      #    [         R                  (       aA  [        5        nSv   SSS5        WR                  5       nU(       a  U R	                  X5        ggSv   g! , (       d  f       N== f7f)zRecord new timings and append to this collection.

Note: this is mainly for internal use inside the compiler pipeline.

See also ``RecordLLVMPassTimings``

Parameters
----------
name: str
    Name for the records.
N)r   LLVM_PASS_TIMINGSr	   r   _append)r   r   r   r   s       r   recordPassTimingsCollection.record$  sL      ##&(G )++-CT'   )(s    A/A7A/
A,(A/c                 L    U R                   R                  [        X5      5        g)zAppend timing records

Parameters
----------
name: str
    Name for the records.
timings: ProcessedPassTimings
    the timing records.
N)r   r}   r   )r   r   r   s      r   r   PassTimingsCollection._append=  s     	\$89r   c                 ^    U R                   (       a  [        S U R                    5       5      $ g)zComputes the sum of the total time across all contained timings.

Returns
-------
res: float or None
    Returns the total number of seconds or None if no timings were
    recorded
c              3   T   #    U  H  oR                   R                  5       v   M      g 7frM   r   rn   ).0rs     r   	<genexpr>7PassTimingsCollection.get_total_time.<locals>.<genexpr>S  s     I=ayy//11=s   &(N)r   sumr   s    r   rn   $PassTimingsCollection.get_total_timeI  s#     ==I4==IIIr   c                 .    [        U R                  S SS9$ )zqReturns the timings in descending order of total time duration.

Returns
-------
res: List[ProcessedPassTimings]
c                 6    U R                   R                  5       $ rM   r   rO   s    r   rQ   :PassTimingsCollection.list_longest_first.<locals>.<lambda>_  s    AII$<$<$>r   T)rw   reverse)sortedr   r   s    r   list_longest_first(PassTimingsCollection.list_longest_firstW  s     dmm>"$ 	$r   c                 $    U R                   (       + $ )z	
        r   r   s    r   is_emptyPassTimingsCollection.is_emptyb  s     ==  r   c                    U R                   (       a  g/ nUR                  nU" SU R                   35        U R                  5       nU" SUS 35        [	        U R
                  5       Hj  u  pVU" SU SUR                   35        UR                  R                  5       U-  S-  nU" SUS	 S
35        U" UR                  R                  USS95        Ml     SR                  U5      $ )a1  Return a string representing the summary of the timings.

Parameters
----------
topn: int; optional, default=5.
    This limits the maximum number of items to show.
    This function will show the ``topn`` most time-consuming passes.

Returns
-------
res: str

See also ``ProcessedPassTimings.summary()``
zNo pass timings were recordedzPrinting pass timings for zTotal time: r   z== #r{   d   z
 Percent: z.1f%   )r   r   r   )
r   r}   r   rn   	enumerater   r   r   r   r   )r   r   r   r   overall_timeir   percents           r   r   PassTimingsCollection.summaryh  s     ==2CB+DJJ<89..0Ll3/01!$--0T!AaffX&'))224|CcIZ}A./199$$$q$9:	 1
 99S>!r   c                      U R                   U   $ )zGet the i-th timing record.

Returns
-------
res: (name, timings)
    A named tuple with two fields:

    - name: str
    - timings: ProcessedPassTimings
r   )r   r   s     r   __getitem__!PassTimingsCollection.__getitem__  s     }}Qr   c                 ,    [        U R                  5      $ )z#Length of this collection.
        )lenr   r   s    r   __len__PassTimingsCollection.__len__  s     4==!!r   c                 "    U R                  5       $ rM   )r   r   s    r   __str__PassTimingsCollection.__str__  s    ||~r   r   N)r   )r    r!   r"   r#   r$   rc   r   r   r   rn   r   propertyr   r   r   r   r   r&   r'   r   r   r   r     sW      0
:	$ ! !
"< "
r   r   )r   rC   rt   collectionsr   collections.abcr   
contextlibr   	functoolsr   
numba.corer   llvmlite.bindingbindingr   r	   r(   r\   r   r   r   r'   r   r   <module>r      ss    	   " $ % %  0 0:  "*%Zj( j(Z .69*=>@H @r   