Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Improve optimality of diffing algorithm #244

Closed
Eun opened this issue Nov 20, 2020 · 2 comments
Closed

Improve optimality of diffing algorithm #244

Eun opened this issue Nov 20, 2020 · 2 comments

Comments

@Eun
Copy link

Eun commented Nov 20, 2020

Given following example:

func TestDiff(t *testing.T) {
	fmt.Println(cmp.Diff(
		// Hello Universe
		[]uint8{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x55, 0x6e, 0x69, 0x76, 0x65, 0x72, 0x73, 0x65},
		// Hello World
		[]uint8{0x48, 0x65, 0x6c, 0x6c, 0x6f, 0x20, 0x57, 0x6f, 0x72, 0x6c, 0x64},
	))
}

sometimes reports

  []uint8{
  	... // 4 identical elements
  	0x6f,
  	0x20,
- 	0x55,
+ 	0x57,
- 	0x6e,
+ 	0x6f,
- 	0x69,
- 	0x76,
- 	0x65,
  	0x72,
- 	0x73,
+ 	0x6c,
- 	0x65,
+ 	0x64,
  }

sometimes

  bytes.Join({
  	"Hello ",
- 	"Universe",
+ 	"World",
  }, "")

Originally posted by @Eun in #238 (comment)

@Eun Eun changed the title Diff generates different output randomly Diff produces output inconsistently Nov 20, 2020
@dsnet dsnet changed the title Diff produces output inconsistently Improve optimality of diffing algorithm Nov 20, 2020
@dsnet
Copy link
Collaborator

dsnet commented Nov 20, 2020

Implementation note: the current diffing algorithm is guaranteed to run in O(n) based on a greedy search algorithm. There are other diffing algorithms that produce diffs that are guaranteed to be optimal (i.e., have the shortest Levenshtein distance), but run in O(n^2). We should consider improving the diffing algorithm to have some degree of back-tracking so that it doesn't get stuck in a sub-optimal (i.e., a local optima) solution. At the same time, it's an important goal that the diffing algorithm avoid O(n^2) behavior.

@dsnet
Copy link
Collaborator

dsnet commented Apr 23, 2022

This appears to have been fixed by 449e17c

@dsnet dsnet closed this as completed Apr 23, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

2 participants