@@ -189,14 +189,17 @@ def _read_ncu(self, thicket, ncu_report_mapping, debug=False, disable_tqdm=False
189189 rollup_dict = {}
190190 # Kernel mapping from NCU kernel to thicket node to save re-querying
191191 kernel_map = {}
192+ # Query results keyed by normalized NCU call trace so the same thicket
193+ # tree query is only executed once.
194+ query_cache = {}
195+ profile_mapping_flipped = {v : k for k , v in thicket .profile_mapping .items ()}
192196
193197 # Loop through NCU files
194198 for ncu_report_file in ncu_report_mapping :
195199 # Set error check flag
196200 call_trace_found = False
197201
198202 # NCU hash
199- profile_mapping_flipped = {v : k for k , v in thicket .profile_mapping .items ()}
200203 ncu_hash = profile_mapping_flipped [ncu_report_mapping [ncu_report_file ]]
201204
202205 # Load file
@@ -274,12 +277,18 @@ def _read_ncu(self, thicket, ncu_report_mapping, debug=False, disable_tqdm=False
274277 f"\t Kernel already in mapping: { demangled_kernel_name } "
275278 )
276279 else : # kernel hasn't been seen yet
277- # Build query
278- query = _build_query_from_ncu_trace (
279- kernel_call_trace , debug
280- )
281- # Apply the query
282- node_set = query .apply (thicket )
280+ query_key = tuple (kernel_call_trace )
281+ if query_key in query_cache :
282+ node_set = query_cache [query_key ]
283+ if debug :
284+ print (f"\t Query already in mapping: { query_key } " )
285+ else :
286+ # Build and apply the query once per unique trace.
287+ query = _build_query_from_ncu_trace (
288+ kernel_call_trace , debug
289+ )
290+ node_set = query .apply (thicket )
291+ query_cache [query_key ] = node_set
283292 # Find the correct node. This may also get the parent so we take the last one
284293 matched_nodes = _match_kernel_str_to_cali (
285294 node_set ,
0 commit comments