Repository navigation
Conversation
The tokenizer split on /(\n|\r\n)/ and then concatenated each line with its line break, building a new string for every line. Instead, find each \n with indexOf and slice the token straight out of the input. With newlineIsToken, the line and its line break are separate tokens. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
The regex split used to throw a TypeError for non-string input as a side effect. The indexOf loop doesn't, and would diff e.g. a number or an object as if it were an empty string, so check explicitly. String objects still work. diffJson also ends up here when its input serializes to undefined, so the message doesn't mention line diffs. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Contributor
Author
|
Another thing I realised: this also cuts peak memory by about a third (33–36% in I also tried optimizing the other tokenizers. |
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.
The line tokenizer split the input with
/(\n|\r\n)/and then glued each line back onto its line break with+=, building a new string for every line. This PR finds each\nwithindexOfand slices the token straight out of the input. WithnewlineIsToken, the same loop slices the line and its line break as two tokens. The tokens are unchanged.This mostly helps when diffing a big file with few changes, where tokenizing is a large part of the time.
diffLines, one changed line in ~8.9kcreatePatch, one changed line in ~8.9kdiffLines, ~800 changed lines in ~8.5kInputs are a
yarn.lockbefore and after a dependency bump, and the newer one with a single line changed. Median of 7 fresh Node 26.10 processes, against8d706c8.The savings are modest in the bigger picture, but it's testable and isolated, doesn't collide with #732 or any other PRs.
Errors
value.split(...)used to throw aTypeErroron non-string input. Without it, the new loop would diff a number or an object as an empty string, so to retain original behaviortokenizenow throwsTypeError: Expected a string to split into lines, got <type>.Testing
I added a
tokenizetest for the edge cases, and compared the new tokenizer with the old one on 800,000 random strings ofa,b, space,\rand\n, with every combination ofnewlineIsTokenandstripTrailingCr. All tokens were identical.