From 39ad002d6fcb2093dd4214db682d1d407b1134a6 Mon Sep 17 00:00:00 2001 From: Noam Gat Date: Thu, 21 Dec 2023 10:41:47 +0200 Subject: [PATCH] Minor profiling-driven performance improvement for regex parser --- lmformatenforcer/regexparser.py | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lmformatenforcer/regexparser.py b/lmformatenforcer/regexparser.py index 0268e38..c782531 100644 --- a/lmformatenforcer/regexparser.py +++ b/lmformatenforcer/regexparser.py @@ -42,13 +42,13 @@ def add_character(self, new_character: str) -> 'RegexParser': symbol = anything_else transition = fsm.alphabet[symbol] - # Missing transition = transition to dead state - if not (state in fsm.map and transition in fsm.map[state]): + try: + # Prefer try-catch to checking if transition exists to avoid double lookup perf hit in valid case + state = fsm.map[state][transition] # type: ignore + return RegexParser(self.context, self.config, state) + except KeyError: + # Missing transition = transition to dead state return RegexParser(self.context, self.config, RegexParser.INVALID_STATE) - - state = fsm.map[state][transition] - - return RegexParser(self.context, self.config, state) def can_end(self) -> bool: return self.current_state in self.context.pattern.finals