From 425a4c0ff3f70e365c764f1ac1935d0cc657068f Mon Sep 17 00:00:00 2001 From: Mauri de Souza Meneguzzo Date: Tue, 3 Nov 2026 19:16:46 -0300 Subject: [PATCH] loopvec: compute -d diffs in process printDiff wrote both sources to temporary files and ran the system diff command. That made -d depend on the host: Windows has no diff, the headers differed between GNU and BSD diff until #10 labeled them, and the error was ignored, so a missing or failing diff printed nothing while loopvec exited successfully. This change computes the unified diff with github.com/pmezard/go-difflib instead, with the same labels and three lines of context, and no temporary files or exec. Its SplitLines adds an empty line after a final newline, which would show up as context near the end of a file, so the lines are split here. go-difflib may align a hunk differently from GNU diff, placing an added line on the other side of an unchanged blank line or brace. The result is the same patch: over std, every file loopvec -d prints applies with patch -F0 and gives the same source as GNU diff's output. Since the output no longer depends on the machine, flag_diff.txt now checks it whole. --- go.mod | 1 + go.sum | 1 + main.go | 47 +++++++++++++------------------- testdata/flag_diff.txt | 62 ++++++++++++++++++++++++++++++++++++------ 4 files changed, 74 insertions(+), 37 deletions(-) diff --git a/go.mod b/go.mod index f1a8121..2025be4 100644 --- a/go.mod +++ b/go.mod @@ -5,6 +5,7 @@ go 1.27.1 toolchain go1.27.3 require ( + github.com/pmezard/go-difflib v1.0.0 golang.org/x/tools v0.52.1-0.20261103172851-aba6dfd3c19d rsc.io/script v0.0.2 ) diff --git a/go.sum b/go.sum index 457ea15..73d56f3 100644 --- a/go.sum +++ b/go.sum @@ -478,6 +478,7 @@ github.com/pelletier/go-toml/v2 v2.4.3/go.mod h1:2gIqNv+qfxSVS7cM2xJQKtLSTLUE9V8 github.com/pkg/errors v0.8.0/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.8.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= github.com/pkg/errors v0.9.1/go.mod h1:bwawxfHBFNV+L2hUp1rHADufV3IMtnDRdf1r5NINEl0= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= github.com/prometheus/client_golang v0.9.1/go.mod h1:7SWBe2y4D6OKWSNQJUaRYU/AaXPKyh/dDVn+NZz0KFw= github.com/prometheus/client_golang v1.0.0/go.mod h1:db9x61etRT2tGnBNRi70OPL5FsnadC4Ky3P0J6CfImo= diff --git a/main.go b/main.go index 61dd22f..adb0b26 100644 --- a/main.go +++ b/main.go @@ -21,11 +21,11 @@ import ( "go/format" "go/token" "os" - "os/exec" "runtime" "runtime/pprof" "strings" + "github.com/pmezard/go-difflib/difflib" "golang.org/x/tools/go/packages" "github.com/mauri870/loopvec/internal/analysis" @@ -319,34 +319,25 @@ func addBuildTag(src []byte, tag string) []byte { return formatted } -// printDiff prints a unified diff between orig and rewritten for path, -// using the system diff command. Both sides are labeled with path, so the -// header does not depend on the temporary files or on which diff is installed -// (GNU and BSD diff print their timestamps differently). +// printDiff prints a unified diff between orig and rewritten for path, with +// both sides labeled with path. It needs no diff command, so the output is the +// same everywhere. func printDiff(path string, orig, rewritten []byte) error { - origFile, err := os.CreateTemp("", "loopvec-orig-*.go") - if err != nil { - return err - } - defer func() { _ = os.Remove(origFile.Name()) }() - if _, err := origFile.Write(orig); err != nil { - _ = origFile.Close() - return err - } - _ = origFile.Close() + return difflib.WriteUnifiedDiff(os.Stdout, difflib.UnifiedDiff{ + A: diffLines(orig), + B: diffLines(rewritten), + FromFile: path, + ToFile: path, + Context: 3, + }) +} - newFile, err := os.CreateTemp("", "loopvec-new-*.go") - if err != nil { - return err - } - defer func() { _ = os.Remove(newFile.Name()) }() - if _, err := newFile.Write(rewritten); err != nil { - _ = newFile.Close() - return err +// diffLines splits src into lines that keep their newline. difflib.SplitLines +// would add an empty line after a final newline. +func diffLines(src []byte) []string { + lines := strings.SplitAfter(string(src), "\n") + if lines[len(lines)-1] == "" { + lines = lines[:len(lines)-1] } - _ = newFile.Close() - - out, _ := exec.Command("diff", "-u", "-L", path, "-L", path, origFile.Name(), newFile.Name()).Output() - _, err = os.Stdout.Write(out) - return err + return lines } diff --git a/testdata/flag_diff.txt b/testdata/flag_diff.txt index 5d7bf4a..0bd5f39 100644 --- a/testdata/flag_diff.txt +++ b/testdata/flag_diff.txt @@ -1,16 +1,11 @@ -# -d prints a unified diff, labeled with the file's path on both sides so the -# headers are the same with GNU and BSD diff, and leaves the source untouched. +# -d prints a unified diff, labeled with the file's path on both sides, and +# leaves the source untouched. loopvec computes the diff itself, so the output +# does not depend on a diff command and is checked whole. cp ops.go ops.go.orig loopvec -d . cmpenv stderr stderr.want -stdout -count=1 '^--- \S+/ops\.go$' -stdout -count=1 '^\+\+\+ \S+/ops\.go$' -stdout -count=1 '^@@ -1,7 \+1,43 @@$' -stdout -count=1 '^\+//go:build goexperiment.simd$' -stdout -count=1 '^\+import \($' -stdout -count=1 '^- for i := range dst \{$' -stdout -count=1 '^\+ _v1.Add\(_v2\).StorePart\(dst\[_i:\]\)$' +cmpenv stdout stdout.want cmp ops.go ops.go.orig ! exists ops_simd.go @@ -28,3 +23,52 @@ func AddInt32s(dst, a, b []int32) { } -- stderr.want -- loopvec: rewrote 1 loop(s) in $WORK/ops.go +-- stdout.want -- +--- $WORK/ops.go ++++ $WORK/ops.go +@@ -1,7 +1,43 @@ ++//go:build goexperiment.simd ++ + package test + ++import ( ++ "simd" ++ "unsafe" ++) ++ + func AddInt32s(dst, a, b []int32) { +- for i := range dst { +- dst[i] = a[i] + b[i] ++ if _loopvecOverlap(dst, a) || _loopvecOverlap(dst, b) { ++ for i := range dst { ++ dst[i] = a[i] + b[i] ++ } ++ } else { ++ if len(dst) > 0 { ++ _ = a[len(dst)-1] ++ _ = b[len(dst)-1] ++ for _i := 0; _i < len(dst); { ++ _v1, _n := simd.LoadInt32sPart(a[_i:]) ++ _v2, _ := simd.LoadInt32sPart(b[_i:]) ++ _v1.Add(_v2).StorePart(dst[_i:]) ++ _i += _n ++ } ++ } + } + } ++ ++func _loopvecOverlap[T any](a, b []T, window ...int) bool { ++ aLo, aHi, bLo, bHi := 0, len(a), 0, len(b) ++ if len(window) == 4 { ++ aLo, aHi, bLo, bHi = window[0], window[1], window[2], window[3] ++ } ++ if aLo >= aHi || bLo >= bHi { ++ return false ++ } ++ size := unsafe.Sizeof(a[0]) ++ aBase := uintptr(unsafe.Pointer(unsafe.SliceData(a))) ++ bBase := uintptr(unsafe.Pointer(unsafe.SliceData(b))) ++ aStart, aEnd := aBase+uintptr(aLo)*size, aBase+uintptr(aHi)*size ++ bStart, bEnd := bBase+uintptr(bLo)*size, bBase+uintptr(bHi)*size ++ return aStart < bEnd && bStart < aEnd ++}