Merged
Conversation
From AI: 1. **Escape Checking**: The code rechecks all trailing backslashes in the accumulated `data` string for every character, which becomes increasingly expensive as `data` grows. 2. **String Slicing**: When an escape character is found, `data = data.slice(0, -1)` creates a new string by copying almost the entire accumulated text. 3. **Repeated String Concatenation**: Adding to `data` one character at a time with `data += char` creates a new string object each time. 4. **Regular Expression Replacements**: At the end, it uses complex regex replacements that need to scan the entire string. 5. **Repeated Delimiter Checking**: For each character, it checks against multiple delimiters with `template.startsWith()`, which involves multiple string comparisons. Our optimization approach for the `text` method focused on several improvements: 1. **Simplified Escape Handling**: - Instead of recounting backslashes for every character, we now use a boolean flag `isEscaped` to track whether the current character is escaped - When we encounter a backslash, we toggle the escape state and skip adding it to the data string directly - This eliminates the expensive backslash counting operation that grew in cost with the size of the text 2. **Eliminated String Slicing**: - We no longer use `data.slice(0, -1)` which was creating a new string by copying almost the entire accumulated text - Instead, we simply don't add the backslash to the data string at all 3. **More Efficient Processing Flow**: - The code now has a clearer path for handling escaped characters - We continue to the next iteration immediately after processing an escape sequence, reducing unnecessary checks 4. **Simplified Data Processing**: - We removed the complex regular expression replacements at the end - Since we're already handling escape sequences correctly during parsing, we don't need additional processing
Parsing is an expensive computation, we now expose it to the client and accept the ast as an argument in both scan method and Chain constructor, so that clients can optimize performance.
andresgutgon
approved these changes
Jun 5, 2025
fe5252c to
d926311
Compare
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.
From AI:
Escape Checking: The code rechecks all trailing backslashes in the accumulated
datastring for every character, which becomes increasingly expensive asdatagrows.String Slicing: When an escape character is found,
data = data.slice(0, -1)creates a new string by copying almost the entire accumulated text.Repeated String Concatenation: Adding to
dataone character at a time withdata += charcreates a new string object each time.Regular Expression Replacements: At the end, it uses complex regex replacements that need to scan the entire string.
Repeated Delimiter Checking: For each character, it checks against multiple delimiters with
template.startsWith(), which involves multiple string comparisons.Our optimization approach for the
textmethod focused on several improvements:Simplified Escape Handling:
isEscapedto track whether the current character is escapedEliminated String Slicing:
data.slice(0, -1)which was creating a new string by copying almost the entire accumulated textMore Efficient Processing Flow:
Simplified Data Processing: