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

diff.Diff optimization if one input is empty. #32

Open
shichuzhu opened this issue Mar 6, 2023 · 1 comment
Open

diff.Diff optimization if one input is empty. #32

shichuzhu opened this issue Mar 6, 2023 · 1 comment

Comments

@shichuzhu
Copy link

godebug/diff/diff.go

Lines 37 to 40 in e693023

// Diff returns a string containing a line-by-line unified diff of the linewise
// changes required to make A into B. Each line is prefixed with '+', '-', or
// ' ' to indicate if it should be added, removed, or is correct respectively.
func Diff(A, B string) string {

It seems if one input of Diff is empty, then the algorithm uses O(N^2) space, where N is the size of the other input.

The result is I see out of memory error if I diff an empty chunk with a non-empty chunk.

It'll be a good improvement if we can just do an empty check before calling the Diff algorithm. And generating diffstring in such case should not be very hard.

@shichuzhu
Copy link
Author

Clarify: In this case, I'm calling pretty.Compare(a, b)
where a is an empty Struct (zero value), where b is not.

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

No branches or pull requests

1 participant