Skip to content

Commit 6441197

Browse files
rwestclaude
andcommitted
Modest cleanup of ensure_independent_atom_ids.
Inlines the nested independent_ids() closure so the unique-IDs check is part of the function body. Microbenchmarks show ~6% speedup on a typical 2-species/10-atoms case (3.6us -> 3.4us) but the real value is readability: one less function definition and one less call per invocation (it's called ~115K times in the catalytic profile). Also collapses the slow-path's two list comprehensions over species.molecule (one filtering reactive, one filtering not reactive) into a single pass that splits into two lists. The bigger time sink in this code path is actually filter_structures and its deepcopy fanout (cumtime ~105s in the same profile vs ~242s self-time for this function); that's a separate target. Co-Authored-By: Claude Opus 4.7 (1M context) <noreply@anthropic.com>
1 parent b547b09 commit 6441197

1 file changed

Lines changed: 17 additions & 16 deletions

File tree

rmgpy/data/kinetics/common.py

Lines changed: 17 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -195,28 +195,29 @@ def ensure_independent_atom_ids(input_species, resonance=True):
195195
"""
196196
ensure_species(input_species) # do not generate resonance structures since we do so below
197197

198-
# Method to check that all species' atom ids are different
199-
def independent_ids():
200-
num_atoms = 0
201-
ids = []
202-
for spcs in input_species:
203-
num_atoms += len(spcs.molecule[0].atoms)
204-
ids.extend([atom.id for atom in spcs.molecule[0].atoms])
205-
num_id = len(set(ids))
206-
return num_id == num_atoms
207-
208-
# If they are not all different, reassign ids and remake resonance structures
209-
if not independent_ids():
198+
# Inline check that all atom IDs across all species' first molecule are unique.
199+
# Building a list then converting to set is faster than incremental set.add()
200+
# because set(list) has a vectorized C path.
201+
ids = []
202+
for spcs in input_species:
203+
atoms = spcs.molecule[0].atoms
204+
ids.extend([atom.id for atom in atoms])
205+
206+
if len(set(ids)) != len(ids):
207+
# Collision: reassign IDs and remake resonance structures
210208
for species in input_species:
211-
unreactive_mol_list = [mol for mol in species.molecule if not mol.reactive]
212-
mol = [mol for mol in species.molecule if mol.reactive][0] # Choose first reactive molecule
209+
reactive_mols = []
210+
unreactive_mols = []
211+
for m in species.molecule:
212+
(reactive_mols if m.reactive else unreactive_mols).append(m)
213+
mol = reactive_mols[0] # Choose first reactive molecule
213214
mol.assign_atom_ids()
214215
species.molecule = [mol]
215216
# Remake resonance structures with new labels
216217
if resonance:
217218
species.generate_resonance_structures(keep_isomorphic=True)
218-
if len(unreactive_mol_list):
219-
species.molecule.extend(unreactive_mol_list)
219+
if unreactive_mols:
220+
species.molecule.extend(unreactive_mols)
220221
elif resonance:
221222
# IDs are already independent, generate resonance structures if needed
222223
for species in input_species:

0 commit comments

Comments
 (0)