Skip to content

Commit

Permalink
Fix slowdown after generating backslash escape sequences
Browse files Browse the repository at this point in the history
  • Loading branch information
AriX committed Apr 19, 2024
1 parent 47d545a commit 15b0121
Showing 1 changed file with 7 additions and 2 deletions.
9 changes: 7 additions & 2 deletions lmformatenforcer/jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -119,8 +119,13 @@ def can_end(self) -> bool:
return all(parser.can_end() for parser in self.object_stack)

def shortcut_key(self) -> Optional[Hashable]:
if self.object_stack:
current_parser = self.object_stack[-1]
current_parsers = []
for parser in reversed(self.object_stack):
current_parsers.append(parser)
if not parser.can_end():
break

for current_parser in current_parsers:
if isinstance(current_parser, StringParsingState):
if not current_parser.allowed_strings and current_parser.seen_opening_quote and not current_parser.seen_closing_quote and not current_parser.regex_parser:
# Performance optimization: When we are parsing a string that is not from a list of allowed strings, most tokens
Expand Down

0 comments on commit 15b0121

Please sign in to comment.