From f373f67048e2566af7fb5eaa68c3bec11607010a Mon Sep 17 00:00:00 2001 From: Adin Scannell Date: Fri, 23 Apr 2021 17:31:18 -0700 Subject: Improve nogo action cache-ability. Presently, the standard library facts are not serialized in a deterministic order. This means that they have the possibility to change on each iteration, requiring a large scale re-analysis of all downstream actions, which includes all packages. Improve cache-ability of nogo actions by improving the determinism of the both facts and findings. Internally, default facts should be serialized as a sorted list for this reason already. PiperOrigin-RevId: 370188259 --- tools/nogo/findings.go | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) (limited to 'tools/nogo/findings.go') diff --git a/tools/nogo/findings.go b/tools/nogo/findings.go index 5bd850269..a00cfe813 100644 --- a/tools/nogo/findings.go +++ b/tools/nogo/findings.go @@ -19,6 +19,7 @@ import ( "fmt" "go/token" "io/ioutil" + "sort" ) // Finding is a single finding. @@ -44,6 +45,33 @@ func WriteFindingsToFile(findings []Finding, filename string) error { // WriteFindingsToBytes serializes findings as bytes. func WriteFindingsToBytes(findings []Finding) ([]byte, error) { + // N.B. Sort all the findings in order to maximize cacheability. + sort.Slice(findings, func(i, j int) bool { + switch { + case findings[i].Position.Filename < findings[j].Position.Filename: + return true + case findings[i].Position.Filename > findings[j].Position.Filename: + return false + case findings[i].Position.Line < findings[j].Position.Line: + return true + case findings[i].Position.Line > findings[j].Position.Line: + return false + case findings[i].Position.Column < findings[j].Position.Column: + return true + case findings[i].Position.Column > findings[j].Position.Column: + return false + case findings[i].Category < findings[j].Category: + return true + case findings[i].Category > findings[j].Category: + return false + case findings[i].Message < findings[j].Message: + return true + case findings[i].Message > findings[j].Message: + return false + default: + return false + } + }) return json.Marshal(findings) } -- cgit v1.2.3