Skip to content

Commit 1fc8bef

Browse files
rwestclaude
andcommitted
Avoid calling fails_species_constraints twice on rejection
We called fails_species_constraints in the `if` condition and then again to retrieve the `reason` string for the exception message. Since the function already returns either False or the reason string, store the result once and branch on it. This halves the constraint-checking work on the rejection path (the expensive path, where a failing check actually runs) with no change in behavior. Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
1 parent 31b220b commit 1fc8bef

3 files changed

Lines changed: 8 additions & 8 deletions

File tree

rmgpy/data/kinetics/family.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1655,8 +1655,8 @@ def _generate_product_structures(self, reactant_structures, maps, forward, relab
16551655
for struct in product_structures:
16561656
if self.is_molecule_forbidden(struct):
16571657
raise ForbiddenStructureException()
1658-
if fails_species_constraints(struct):
1659-
reason = fails_species_constraints(struct)
1658+
reason = fails_species_constraints(struct)
1659+
if reason:
16601660
raise ForbiddenStructureException(
16611661
"Species constraints forbids product species {0}. Please "
16621662
"reformulate constraints, or explicitly "

rmgpy/rmg/main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -734,11 +734,11 @@ def initialize(self, **kwargs):
734734
"Input species {0} is globally forbidden. You may explicitly "
735735
"allow it by adding 'input species' to the `generatedSpeciesConstraints` `allowed` list.".format(spec.label)
736736
)
737-
if fails_species_constraints(spec):
737+
reason = fails_species_constraints(spec)
738+
if reason:
738739
if "allowed" in self.species_constraints and "input species" in self.species_constraints["allowed"]:
739740
self.species_constraints["explicitlyAllowedMolecules"].append(spec.molecule[0])
740741
else:
741-
reason = fails_species_constraints(spec)
742742
raise ForbiddenStructureException(
743743
"Species constraints forbids input species {0}. Please "
744744
"reformulate constraints, remove the species, or explicitly "

rmgpy/rmg/model.py

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1739,11 +1739,11 @@ def add_seed_mechanism_to_core(self, seed_mechanism, react=False, requires_rms=F
17391739
"found in a seed mechanism or reaction "
17401740
"library.".format(spec.label, seed_mechanism.label)
17411741
)
1742-
if fails_species_constraints(spec):
1742+
reason = fails_species_constraints(spec)
1743+
if reason:
17431744
if "allowed" in rmg.species_constraints and "seed mechanisms" in rmg.species_constraints["allowed"]:
17441745
rmg.species_constraints["explicitlyAllowedMolecules"].extend(spec.molecule)
17451746
else:
1746-
reason = fails_species_constraints(spec)
17471747
raise ForbiddenStructureException(
17481748
"Species constraints forbids species {0} from seed mechanism {1}."
17491749
" Please reformulate constraints, remove the species, or"
@@ -1867,11 +1867,11 @@ def add_reaction_library_to_edge(self, reaction_library, requires_rms=False):
18671867
"inert unless found in a seed mechanism or reaction "
18681868
"library.".format(spec.label, reaction_library.label)
18691869
)
1870-
if fails_species_constraints(spec):
1870+
reason = fails_species_constraints(spec)
1871+
if reason:
18711872
if "allowed" in rmg.species_constraints and "reaction libraries" in rmg.species_constraints["allowed"]:
18721873
rmg.species_constraints["explicitlyAllowedMolecules"].extend(spec.molecule)
18731874
else:
1874-
reason = fails_species_constraints(spec)
18751875
raise ForbiddenStructureException(
18761876
"Species constraints forbids species {0} from reaction library "
18771877
"{1}. Please reformulate constraints, remove the species, or "

0 commit comments

Comments
 (0)