Repository navigation
FIX: Check the default shortcut-reference image pattern in linear time in MarkdownInjectionScorer - #4223
Open
Jaafer Rahmani (bIackr0se) wants to merge 2 commits into
Open
FIX: Check the default shortcut-reference image pattern in linear time in MarkdownInjectionScorer#4223Jaafer Rahmani (bIackr0se) wants to merge 2 commits into
Jaafer Rahmani (bIackr0se) wants to merge 2 commits into
Conversation
This branch has not been deployed
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Description
MarkdownInjectionScorer()still runs the default shortcut-reference image pattern withre.search, which takes quadratic time in the number of images or definition lines in a reply: a 300 KB reply of![a]repeated takes 25 s to score. This PR checks that pattern in linear time with the same verdict, as #3966 did for the default inline and reference patterns.[label]:line after every![label], and its second branch scans the rest for an image after every definition line.[to the first], so each![and each definition line has exactly one label._has_shortcut_pairreads each label once, backwards from its], into a trie of lowercased characters, so equal labels share a node. One pass then checks the two orders the pattern accepts: a definition line that starts after an image ends, or an image that starts after a definition's value.ch.lower()[0]is the per-character comparisonremakes for a case-insensitive backreference, so![İ]still pairs with[i]: xand![ſ]still does not pair with[s]: x.re.search.Default
MarkdownInjectionScorer().score_text_async(reply)withinitialize_pyrit_async(memory_db_type=IN_MEMORY), Python 3.12:The new check gives the same verdict as
re.searchwith the default shortcut pattern on every sequence of up to six tokens over![a],![A], and on 600,000 random strings with case variants such asİ,ſ,KandΣ, with no difference.