Remove allocs from io.ReadAll (hard cases)#606
Remove allocs from io.ReadAll (hard cases)#606SuperSandro2000 wants to merge 3 commits intomasterfrom
Conversation
34e4642 to
948d327
Compare
948d327 to
5130fb1
Compare
majewsky
left a comment
There was a problem hiding this comment.
I don't like that we're just ignoring most of the errors from Close(). I would like to have helper functions ReadAllAndClose(io.ReadCloser) ([]byte, error) and CopyAllAndClose(io.Writer, io.ReadCloser) error instead.
| parsedReport, err := trivy.UnmarshalReportFromJSON(payload.Contents) | ||
| unmodifiedReport := &bytes.Buffer{} | ||
| parsedReport, err := trivy.UnmarshalReportFromJSON(io.TeeReader(payload.Contents, unmodifiedReport)) | ||
| if err != nil { |
There was a problem hiding this comment.
TeeReader means that we will not be able to save allocations on this part, which as I understand is the most critical part for actually reducing memory usage in the janitor.
In the original, retaining the unmodified report in payload.Contents allowed skipping MarshalJSON if no modifications are required. We can trade this CPU optimization for a RAM optimization by instead always marshalling, and not holding unmodifiedReport. parsedReport is able to faithfully reserialize the original report, so no information would be lost here.
There was a problem hiding this comment.
Yep, that was a consideration I was making when doing that change and I was not sure if the CPU cost outweighs the memory but probably yet.
There was a problem hiding this comment.
Before:
➜ go test ./internal/keppel/ -bench=BenchmarkEnrichReport -benchmem
goos: linux
goarch: amd64
pkg: github.com/sapcc/keppel/internal/keppel
cpu: Intel(R) Xeon(R) Platinum 8260 CPU @ 2.40GHz
BenchmarkEnrichReport-8 130 9156652 ns/op 1414337 B/op 814 allocs/op
PASS
ok github.com/sapcc/keppel/internal/keppel 1.216s
After:
➜ go test ./internal/keppel/ -bench=BenchmarkEnrichReport -benchmem
goos: linux
goarch: amd64
pkg: github.com/sapcc/keppel/internal/keppel
cpu: Intel(R) Xeon(R) Platinum 8260 CPU @ 2.40GHz
BenchmarkEnrichReport-8 117 10143171 ns/op 1161177 B/op 832 allocs/op
PASS
ok github.com/sapcc/keppel/internal/keppel 1.218s
| type trivyTestReport struct { | ||
| payload trivy.ReportPayload | ||
| buf []byte | ||
| } |
There was a problem hiding this comment.
Just flagging that I don't like how this is modeled, without yet offering a specific suggestion immediately. Code using this type looks unnecessarily convoluted.
148b6e5 to
62e6d3e
Compare
62e6d3e to
d90ff28
Compare
Merging this branch changes the coverage (7 decrease, 1 increase)
Coverage by fileChanged files (no unit tests)
Please note that the "Total", "Covered", and "Missed" counts above refer to code statements instead of lines of code. The value in brackets refers to the test coverage of that file in the old version of the code. Changed unit test files
|
Lets not merge this until Stefan gave a review.