Skip to content

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

@shichuzhu

Description

@shichuzhu

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.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions