From 6cf5ff6b97d59a4b762736af5d4dcab062994909 Mon Sep 17 00:00:00 2001 From: Kyle Gorman Date: Thu, 23 Apr 2026 11:53:28 -0700 Subject: [PATCH] RmFinalEpsilon: Use constexpr if to check if the semiring is left-distributive; if it is, we don't need to check the weight value, just for epsilon arcs. PiperOrigin-RevId: 904566301 --- openfst/lib/rmfinalepsilon.h | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/openfst/lib/rmfinalepsilon.h b/openfst/lib/rmfinalepsilon.h index c828e13e..8e1fc981 100644 --- a/openfst/lib/rmfinalepsilon.h +++ b/openfst/lib/rmfinalepsilon.h @@ -71,15 +71,22 @@ void RmFinalEpsilon(MutableFst* fst) { const auto& arc = aiter.Value(); // Next state is in the list of finals. if (finals.find(arc.nextstate) != finals.end()) { - // Sums up all epsilon arcs if the semiring is left distributive. - // When left distributivity does not hold, at most one arc can be - // removed. - if (arc.ilabel == 0 && arc.olabel == 0 && - (weight == Weight::Zero() || - (Weight::Properties() & kLeftSemiring))) { - weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + // If the semiring is left-distributive we don't need to check if the + // weight is zero. + constexpr bool kIsLeftSemiring = + (Weight::Properties() & kLeftSemiring) != 0; + if constexpr (kIsLeftSemiring) { + if (arc.ilabel == 0 && arc.olabel == 0) { + weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + } else { + arcs.push_back(arc); + } } else { - arcs.push_back(arc); + if (arc.ilabel == 0 && arc.olabel == 0 && weight == Weight::Zero()) { + weight = Plus(Times(arc.weight, fst->Final(arc.nextstate)), weight); + } else { + arcs.push_back(arc); + } } } else { arcs.push_back(arc);