Skip to content

diff: replace external diff crate with internal diff_engine - #415

Open
shadowmkj wants to merge 3 commits into
uutils:mainfrom
shadowmkj:feat/myer-diff-similar
Open

shadowmkj wants to merge 3 commits into
uutils:mainfrom
shadowmkj:feat/myer-diff-similar

Conversation

@shadowmkj

@shadowmkj shadowmkj commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Summary

Replaces the external diff crate dependency with an internal diff_engine module wrapping the modern similar crate. All diff consumers (context_diff, ed_diff, normal_diff, side_diff, and unified_diff) have been refactored to utilize the new internal engine and its Change enum types.

Resolves: #401

Key Changes

  • Added a new diff_engine module wrapping similar (version 3.2.1) using Eugene Myers' linear-space algorithm.
  • Defined an internal Change enum (Left, Right, Both) mirroring the sequence comparison outputs.
  • Removed the legacy diff = "0.1.13" external dependency from Cargo.toml.
  • Refactored context_diff, ed_diff, normal_diff, side_diff, and unified_diff to consume diff_slices and Change from diff_engine.
  • Included robust unit tests covering identical, empty, modified, and inserted/deleted slice scenarios within src/diff_engine.rs.

Performance benchmarks

541KB (10k lines) file with sparse differences

Command Mean [ms] Min [ms] Max [ms] Relative
./old_diff bench_data/med_sparse_a.txt bench_data/med_sparse_b.txt 180.3 ± 1.2 179.4 184.6 44.09 ± 1.00
./new_diff bench_data/med_sparse_a.txt bench_data/med_sparse_b.txt 4.1 ± 0.1 3.9 4.7 1.00

2.7MB (50k lines) file with sparse differences

Command Mean [s] Min [s] Max [s] Relative
./old_diff bench_data/large_sparse_a.txt bench_data/large_sparse_b.txt 3.969 ± 0.081 3.880 4.145 389.14 ± 12.26
./new_diff bench_data/large_sparse_a.txt bench_data/large_sparse_b.txt 0.010 ± 0.000 0.010 0.013 1.00

Benchmark against GNU diff using the 2.7MB file

NOTE diff is technically not the latest GNU diff, since I'm on a macbook. But I reckon the performance difference will be nearly the same. Also since diff is part of system library the startup time would be less so theres that!

Command Mean [ms] Min [ms] Max [ms] Relative
diff bench_data/large_sparse_a.txt bench_data/large_sparse_b.txt 10.5 ± 0.2 10.3 11.6 1.01 ± 0.03
./new_diff bench_data/large_sparse_a.txt bench_data/large_sparse_b.txt 10.5 ± 0.2 10.1 11.4 1.00

Tested on: Macbook Air M2

- Remove external `diff` crate dependency from Cargo.toml and Cargo.lock
- Update context, ed, normal, side, and unified diff implementations to use the new internal engine
Comment thread Cargo.toml Outdated
itoa = "1.0.11"
regex = "1.10.4"
same-file = "1.0.6"
similar = { version = "3.2.1", default-features = false, features = ["std"] }

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is bstr pulled into Cargo.lock?
we only enable std, and in similar it is bstr?/std, so it shouldn't be activated

Comment thread src/side_diff.rs Outdated
diff(from_file, to_file, &mut output, &params);

assert_eq!(output, vec![]);
assert_eq!(output, Vec::<u8>::new());

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

why is this change needed? looks like a side effect of bstr being compiled in

Comment thread src/diff_engine.rs Outdated
new_index,
new_len,
} => {
for i in 0..old_len {

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

almost the same loop 4 times
could be changes.extend(expected[old_index..old_index + old_len].iter().map(Change::Left)), no?

Comment thread src/diff_engine.rs Outdated
actual: &'a [T],
) -> Vec<Change<&'a T>> {
let ops = capture_diff_slices(Algorithm::Myers, expected, actual);
let mut changes = Vec::new();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Vec::with_capacity(expected.len() + actual.len())?

Comment thread src/diff_engine.rs

#[test]
fn test_insertions_and_deletions() {
let a: &[&[u8]] = &[b"line1", b"line2", b"line3"];

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

please add a test where Myers and the old LCS output differ, so we know the hunks we now produce
and one with a larger input

@sylvestre

Copy link
Copy Markdown
Collaborator

thanks for the work on this!
The bstr dependency in Cargo.lock and the side_diff test change need explaining (see inline).
Could you please also enable a diff bench on files that differ in benches/bench-diffutils.rs (diff_compare_files is commented out), so codspeed tracks the speed-up?

fix: disable std feature for similar dependency and fix the Cargo.lock file

diff_engine: replace manual loops with idiomatic rust (iterators)

diff_engine: preallocate vector

test(diff): add more unit and integration tests for diff engine
@shadowmkj

Copy link
Copy Markdown
Contributor Author

Thanks for the review!
I did not notice bstr sliding into the lockfiles 😅. Turns out similar already has std on by default.
Also changed the manual loops and made them more idiomatic. I have also added regression tests like you asked.

@codspeed

codspeed Bot commented Nov 17, 2026 •

Copy link
Copy Markdown
Contributor

Merging this PR will degrade performance by 10.83%

2 warnings

⚠️ 32 benchmarks are dominated by system calls

System calls cannot be simulated consistently and are left out of the measure. The reported value understates the real cost. Switch to the Walltime instrument to measure them.

Learn more about system calls

See affected benchmarks

⚠️ 16 benchmarks with a significant change ran on different hardware

Their base and head results were measured on machines with a different CPU, core count or memory, so the changes might not come from the code. Run base and head on the same runner type.

Learn more about Macro Runners

See affected benchmarks

❌ 16 regressed benchmarks
✅ 10 untouched benchmarks
🆕 16 new benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
❌ cmd_cmp_gnu_equal[1000] 22.9 µs 26.6 µs -13.93%
❌ cmd_cmp_gnu_equal[100] 22.9 µs 26.4 µs -13.11%
❌ cmd_cmp_gnu_equal[25000] 23.1 µs 26.5 µs -12.66%
❌ cmd_cmp_gnu_equal[10000] 23.1 µs 26.5 µs -12.66%
❌ cmd_cmp_release_equal[25000] 24.3 µs 27.3 µs -11.22%
❌ cmd_diff_release_equal[25000] 24.8 µs 27.7 µs -10.65%
❌ cmd_diff_release_equal[10000] 24.8 µs 27.7 µs -10.63%
❌ cmd_cmp_release_equal[1000] 24.4 µs 27.3 µs -10.62%
❌ cmd_diff_gnu_equal[100] 23.6 µs 26.3 µs -10.39%
❌ cmd_diff_gnu_equal[1000] 23.6 µs 26.3 µs -10.38%
❌ cmd_cmp_release_equal[100] 24.4 µs 27.2 µs -10.31%
❌ cmd_diff_gnu_equal[10000] 24.1 µs 26.9 µs -10.29%
❌ cmd_cmp_release_equal[10000] 24.4 µs 27.2 µs -10.06%
❌ cmd_diff_gnu_equal[25000] 24.2 µs 26.8 µs -9.73%
❌ cmd_diff_release_equal[1000] 24.9 µs 27.6 µs -9.66%
❌ cmd_diff_release_equal[100] 25.5 µs 27.3 µs -6.68%
🆕 cmd_diff_gnu_different[100] N/A 26.4 µs N/A
🆕 cmd_diff_gnu_different[1000] N/A 26.7 µs N/A
🆕 cmd_diff_gnu_different[10000] N/A 27.2 µs N/A
🆕 cmd_diff_gnu_different[25000] N/A 27.1 µs N/A
... ... ... ... ...

ℹ️ Only the first 20 benchmarks are displayed. Go to the app to view all benchmarks.

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing shadowmkj:feat/myer-diff-similar (6bf7996) with main (b93330b)

Open in CodSpeed

@shadowmkj

shadowmkj commented Nov 18, 2026 •

Copy link
Copy Markdown
Contributor Author

The regressed codspeed benches doesn't look right. All the tests are for files that are equal. I think this is some system overhead happening (BASE and HEAD shows microseconds difference).

Something else I wanted to point out is that similar crate does not implement the Myers algorithm like GNU diff does.
An important distinction I found is that if prefers insertions first before deletions (this is not standard diff). Since it becomes harder for humans to read the diff output.

I need a bit more research before implementing next steps, thanks

@shadowmkj shadowmkj closed this Nov 18, 2026
@shadowmkj shadowmkj reopened this Nov 18, 2026
@shadowmkj

Copy link
Copy Markdown
Contributor Author

Oops, I meant to just comment, accidently pressed some shortcut and closed the PR, sorry 😅

@codecov

codecov Bot commented Nov 18, 2026

Copy link
Copy Markdown

Codecov Report

✅ All modified and coverable lines are covered by tests.
✅ Project coverage is 0.00%. Comparing base (471217f) to head (6bf7996).
⚠️ Report is 1 commits behind head on main.

Additional details and impacted files
@@     Coverage Diff     @@
##   main   #415   +/-   ##
===========================
===========================

☔ View full report in Codecov by Harness.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[PROPOSAL] Replace diff crate with a Myers diff implementation

2 participants