Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/escaping performance #93

Merged
merged 2 commits into from
Apr 20, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions lmformatenforcer/jsonschemaparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,15 @@ def add_character(self, new_character: str) -> CharacterLevelParser:
option_json_schema_parsers.append(option_parser)
return UnionParser(option_json_schema_parsers)

# For some performance optimizations to work, we want to make sure we don't leave irrelevant
# objects at the top of the stack, which we know will be passed over next timestep
new_object_stack = updated_parser.object_stack
while new_object_stack and new_object_stack[-1].can_end() and new_object_stack[-1].get_allowed_characters() == '':
finished_receiver = new_object_stack[-1]
if isinstance(finished_receiver, StringParsingState):
updated_parser.last_parsed_string = finished_receiver.parsed_string
del new_object_stack[-1]

return updated_parser

def get_allowed_characters(self) -> str:
Expand Down
Loading