Repository navigation
Add util.diff to node:util - #8332
Conversation
There was a problem hiding this comment.
Superseded by the review of
43d28681.
This review, of 3de4e16a
💬 Approved with comments — The port matches Node and is wired in correctly. One WARNING: memory grows quickly on large, very different inputs, as it does in Node. One INFO: the commit message does not record a compatibility decision for the new API.
Findings: 1 warning · 1 info (2 are inline comments on the diff)
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8332.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Performance] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this.
- **File:** src/node/internal/internal_util_diff.ts:58
## INFO (1)
### [AGENTS.md] The new `util.diff` export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance).
- **File:** src/node/util.ts:104
📖 AGENTS.md: 💬 1 info
The commit message does not record a compatibility decision for the new util.diff API.
Recommendation: 💬 Optional Nits
- [INFO] The new
util.diffexport adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance). (src/node/util.ts:104)
View full review
The repository's guidelines ask that any addition to the API Workers can reach have its compatibility decision recorded in the commit message: either a compatibility flag, or an explicit statement that the risk is accepted. The commit message explains the port but records neither.
⚡ Performance: ⚠️ 1 warning
Each edit level saves a full copy of the V array, so memory grows quickly with large, very different inputs.
Recommendation: 📝 Minor Comments
- [WARNING] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this. (
src/node/internal/internal_util_diff.ts:58)
View full review
myersDiff saves a full Int32Array(2*(N+M)+1) copy into trace at every edit level. Memory is therefore about 8·D·(N+M) bytes, where D is the edit distance. Two completely different 2,000-line inputs need about 130 MB, which can exhaust a Worker's memory. The 2^31 - 1 check does not prevent this. Node behaves the same way.
✅ All other checks passed
🔒 Security: ✅ LGTM
validateString and validateStringArray run before the diff, so a wrong input type cannot reach the algorithm. The diff only uses memory in the calling Worker's own isolate; see the performance section.
📋 Code Quality: ✅ LGTM
The forward pass, the backtrack, validateInput and the exports match Node's implementation. The memory growth on large inputs is covered in the performance section.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 3de4e16a
3de4e16 to
43d2868
Compare
There was a problem hiding this comment.
Superseded by the review of
f9163537.
This review, of 43d28681
💬 Approved with comments — The port matches Node and is wired in correctly. One WARNING: memory grows quickly on large, very different inputs, as it does in Node. One INFO: the commit message does not record a compatibility decision for the new API.
Findings: 1 warning · 1 info
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
🔧 Fix with your agent
Paste the following into your coding agent to investigate and fix these issues.
Fix the following code review issues found in pull request #8332.
Before making changes, review each issue and present a brief summary table:
- For each finding, state whether you agree, disagree, or need clarification
- If you disagree (e.g., the fix would require disproportionate changes
for minimal ROI, or the finding is factually incorrect), explain why
- If you need to clarify something before deciding, ask those questions
- Then share your plan for which issues to tackle and in what order
## WARNING (1)
### [Performance] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this.
- **File:** src/node/internal/internal_util_diff.ts:58
## INFO (1)
### [AGENTS.md] The new `util.diff` export adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance).
- **File:** src/node/util.ts:104
📖 AGENTS.md: 💬 1 info
The commit message does not record a compatibility decision for the new util.diff API.
Recommendation: 💬 Optional Nits
- [INFO] The new
util.diffexport adds to the API Workers can reach, but the commit message does not record a compatibility decision (a compatibility flag or explicit risk acceptance). (src/node/util.ts:104)
View full review
The repository's guidelines ask that any addition to the API Workers can reach have its compatibility decision recorded in the commit message: either a compatibility flag, or an explicit statement that the risk is accepted. The commit message explains the port but records neither.
⚡ Performance: ⚠️ 1 warning
Each edit level saves a full copy of the V array, so memory grows quickly with large, very different inputs.
Recommendation: 📝 Minor Comments
- [WARNING] myersDiff saves the full V array at every edit level, so memory grows quickly with input size and large, very different inputs can exhaust a Worker's memory. The 2^31-1 check does not prevent this. (
src/node/internal/internal_util_diff.ts:58)
View full review
myersDiff saves a full Int32Array(2*(N+M)+1) copy into trace at every edit level. Memory is therefore about 8·D·(N+M) bytes, where D is the edit distance. Two completely different 2,000-line inputs need about 130 MB, which can exhaust a Worker's memory. The 2^31 - 1 check does not prevent this. Node behaves the same way.
✅ All other checks passed
🔒 Security: ✅ LGTM
validateString and validateStringArray run before the diff, so a wrong input type cannot reach the algorithm. The diff only uses memory in the calling Worker's own isolate; see the performance section.
📋 Code Quality: ✅ LGTM
The forward pass, the backtrack, validateInput and the exports match Node's implementation. The memory growth on large inputs is covered in the performance section.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 43d28681 — same changes as 3de4e16a, so this is that commit's review (no new review was run; line numbers are as of 3de4e16a)
There was a problem hiding this comment.
Superseded by the review of
0c9e022b.
This review, of f9163537
✅ Approved — The port matches Node and is wired in correctly. Both notes from the last review were settled in the discussion, and no new issues were found.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Input is validated before it reaches the diff algorithm.
📖 AGENTS.md: ✅ LGTM
@jasnell [approver] said no compatibility flag is needed for the new diff export, so that decision is recorded on this pull request.
📋 Code Quality: ✅ LGTM
The forward pass, the backtrack, validateInput and the exports match Node's implementation.
⚡ Performance: ✅ LGTM
myersDiff keeps a copy of the V array for every edit level, as Node's version does. Memory use is bounded by the isolate heap cap and stays inside the calling Worker's own isolate.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: f9163537
node:util had no diff(), which Node.js added in v22.15.0 and v23.11.0 to expose the Myers diff behind assertion error messages. In this patch util.diff(actual, expected) is ported from Node.js. It accepts strings or arrays of strings and returns [op, value] entries in the same order as Node.js. - Port the Myers diff from lib/internal/assert/myers_diff.js, since the existing diff in internal_diffs.ts uses a different algorithm and does not produce the same ordering. - Port test/parallel/test-diff.js into util-nodejs-test.js. util.diff is a new export of node:util, which is only available under nodejs_compat, so it ships without a compatibility flag: code that doesn't call it can't observe it, as with earlier node:util additions. Like Node.js, the diff keeps one vector per edit step, so memory grows with the product of input size and edit distance, and the input size limit is Node's.
Co-authored-by: James M Snell <jsnell@cloudflare.com>
f916353 to
0c9e022
Compare
There was a problem hiding this comment.
✅ Approved — The util.diff port matches Node's Myers diff and argument checks, and the export and tests are wired in correctly. No new issues since the last review.
The decision is advisory: this review is a comment and never approves, blocks or requests changes on the pull request.
✅ All checks passed
🔒 Security: ✅ LGTM
Input is validated before it reaches the diff algorithm.
📖 AGENTS.md: ✅ LGTM
@jasnell [approver] said the new diff export on node:util does not need a compatibility flag, so that decision is recorded on this pull request.
📋 Code Quality: ✅ LGTM
The forward pass, the backtrack, validateInput and the util.diff export match Node's implementation. The ported tests cover the documented ordering, the identical-input case and the argument errors.
⚡ Performance: ✅ LGTM
myersDiff keeps a copy of the V array for every edit level, as Node's version does. As @jasnell [approver] noted in the discussion, the isolate heap cap bounds this memory, and it stays inside the calling Worker's own isolate. The changes since the last review touch only the util.ts export and the test file.
📝 Documentation: ✅ LGTM
No documentation issues.
Reviewed commit: 0c9e022b
node:utilhad nodiff(), which Node added in v22.15.0 and v23.11.0 to expose the Myers diff behind assertion error messages.In this patch:
util.diff(actual, expected)is ported from Node. It takes strings or arrays of strings and returns[op, value]entries in the same order as Node, with the same argument errors and size limit.lib/internal/assert/myers_diff.js, because the existing diff ininternal_diffs.tsuses a different algorithm and produces a different order.util.diff(['1', '2', '3'], ['1', '3', '4'])TypeError(missing)[[0, '1'], [1, '2'], [0, '3'], [-1, '4']]util.diff('foo', 'foo')TypeError(missing)[]util.diff(1, 'a')TypeError(missing)ERR_INVALID_ARG_TYPETests are ported from Node's
test-diff.js.