@@ -31,7 +31,6 @@ import opcode
3131import os
3232import types
3333from warnings import warn
34- from weakref import WeakSet
3534
3635from line_profiler._diagnostics import (
3736 WRAP_TRACE, SET_FRAME_LOCAL_TRACE, USE_LEGACY_TRACE
@@ -1067,9 +1066,6 @@ cdef class LineProfiler:
10671066 _managers = {}
10681067 # type: ClassVar[dict[bytes, int]], bytes = bytecode
10691068 _all_paddings = {}
1070- # type: ClassVar[dict[int, weakref.WeakSet[LineProfiler]]],
1071- # int = func id
1072- _all_instances_by_funcs = {}
10731069
10741070 def __init__ (self , *functions ,
10751071 wrap_trace = None , set_frame_local_trace = None ):
@@ -1139,26 +1135,23 @@ datamodel.html#user-defined-functions
11391135 code_hashes = []
11401136 if any (co_code): # Normal Python functions
11411137 # Figure out how much padding we need and strip the bytecode
1138+ # Notes:
1139+ # - Profiled function are always padded, so as to
1140+ # distinguish between them and unprofiled bytecode twins
1141+ # - `npad` is strictly increasing, except when the function
1142+ # has already been padded -- we assume that is solely
1143+ # because a (could be the same) profiler instance has seen
1144+ # it
11421145 base_co_code: bytes
1143- npad_code: int
1144- base_co_code, npad_code = multibyte_rstrip(co_code)
1145- try :
1146- npad = self ._all_paddings[base_co_code]
1147- except KeyError :
1148- npad = 0
1149- self ._all_paddings[base_co_code] = max (npad, npad_code) + 1
1150- try :
1151- profilers_to_update = self ._all_instances_by_funcs[func_id]
1152- profilers_to_update.add(self )
1153- except KeyError :
1154- profilers_to_update = WeakSet({self })
1155- self ._all_instances_by_funcs[func_id] = profilers_to_update
1156- # Maintain `.dupes_map` (legacy)
1157- try :
1158- self .dupes_map[base_co_code].append(code)
1159- except KeyError :
1160- self .dupes_map[base_co_code] = [code]
1161- if npad > npad_code:
1146+ npad: int
1147+ is_padded: int
1148+ base_co_code, is_padded = multibyte_rstrip(co_code)
1149+ if not is_padded:
1150+ try :
1151+ npad = self ._all_paddings[base_co_code]
1152+ except KeyError :
1153+ npad = 1
1154+ self ._all_paddings[base_co_code] = npad + 1
11621155 # Code hash already exists, so there must be a duplicate
11631156 # function (on some instance);
11641157 # (re-)pad with no-op
@@ -1168,8 +1161,11 @@ datamodel.html#user-defined-functions
11681161 func.__code__ = code
11691162 except AttributeError as e:
11701163 func.__func__.__code__ = code
1171- else : # No re-padding -> no need to update the other profs
1172- profilers_to_update = {self }
1164+ # Maintain `.dupes_map` (legacy)
1165+ try :
1166+ self .dupes_map[base_co_code].append(code)
1167+ except KeyError :
1168+ self .dupes_map[base_co_code] = [code]
11731169 # TODO: Since each line can be many bytecodes, this is kinda
11741170 # inefficient
11751171 # See if this can be sped up by not needing to iterate over
@@ -1204,21 +1200,17 @@ datamodel.html#user-defined-functions
12041200 # because Cython shim code objects don't support local
12051201 # events
12061202 code = code.replace(co_filename = cython_source)
1207- profilers_to_update = {self }
12081203 # Update `._c_code_map` and `.code_hash_map` with the new line
1209- # hashes on `self` (and other instances profiling the same
1210- # function if we padded the bytecode)
1211- for instance in profilers_to_update:
1212- prof = < LineProfiler> instance
1213- try :
1214- line_hashes = prof.code_hash_map[code]
1215- except KeyError :
1216- line_hashes = prof.code_hash_map[code] = []
1217- for code_hash in code_hashes:
1218- line_hash = < int64> code_hash
1219- if not prof._c_code_map.count(line_hash):
1220- line_hashes.append(line_hash)
1221- prof._c_code_map[line_hash]
1204+ # hashes
1205+ try :
1206+ line_hashes = self .code_hash_map[code]
1207+ except KeyError :
1208+ line_hashes = self .code_hash_map[code] = []
1209+ for code_hash in code_hashes:
1210+ line_hash = < int64> code_hash
1211+ if not self ._c_code_map.count(line_hash):
1212+ line_hashes.append(line_hash)
1213+ self ._c_code_map[line_hash]
12221214
12231215 self .functions.append(func)
12241216
0 commit comments