Repository navigation
Conversation
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.
splits,split/2,sub, andgsubtake quadratic time in the number of matches (#336). They are defined in jq on top ofmatch, and for every match they slice the input by codepoint index, which scans the string from the beginning.subalso appends to the result string for every match, which copies it each time.This adds an internal function
_splitsthat splits the string at the matches using the byte indices of the regexp, and optionally returns the named captures of each match.splitsandsplit/2call it directly.subcollects the replacement pieces in arrays and joins them once at the end.In-process timings on
"<a>é "repeated n times (go1.27.3, linux/amd64):[splits("<")], n = 8000[splits("<")], n = 64000gsub("<"; "["), n = 8000gsub("<"; "["), n = 64000The total allocation of
gsubat n = 64000 goes from 12.7 GB to 37 MB (peak memory from 72 MB to 32 MB). On a 1.4 MB HTML string with 80,000<characters,[splits("<")]now takes 37 ms andgsub("<"; "<")105 ms.The behavior does not change. Errors of
splitsandsubare still reported asmatch(...) cannot be applied to: .... I comparedsplits,split,sub, andgsubbefore and after this change on 2.8 million cases (random inputs including invalid UTF-8, non-string inputs, invalid flags and patterns, and replacement filters with multiple outputs, no outputs, errors, and non-string values), and the outputs and error messages are identical. The added tests cover replacements with a different number of outputs per match, unmatched named captures, and multibyte input.This is the second part of #336 and does not depend on #338, which covers
match. Together they cover everything in the issue.