godebug icon indicating copy to clipboard operation
godebug copied to clipboard

diff.Diff optimization if one input is empty.

Open shichuzhu opened this issue 2 years ago • 1 comments

https://github.com/kylelemons/godebug/blob/e693023230a4a8be4e28c9bd02f467b0534ac08b/diff/diff.go#L37-L40

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 avatar Mar 06 '23 21:03 shichuzhu

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

shichuzhu avatar Mar 06 '23 22:03 shichuzhu