Skip to content

Commit

Permalink
Reduce memory usage by 30% through use of pointers (chainguard-dev#211)
Browse files Browse the repository at this point in the history
* Use pointers wherever

* Make it so that tests pass
  • Loading branch information
tstromberg authored May 10, 2024
1 parent fd09da0 commit 1b18970
Show file tree
Hide file tree
Showing 14 changed files with 89 additions and 84 deletions.
2 changes: 1 addition & 1 deletion bincapz.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ func main() {
log.Fatal("failed", slog.Any("error", err))
}

err = renderer.Full(ctx, *res)
err = renderer.Full(ctx, res)
if err != nil {
log.Fatal("render failed", slog.Any("error", err))
}
Expand Down
24 changes: 12 additions & 12 deletions pkg/action/diff.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/chainguard-dev/clog"
)

func relFileReport(ctx context.Context, c Config, path string) (map[string]bincapz.FileReport, error) {
func relFileReport(ctx context.Context, c Config, path string) (map[string]*bincapz.FileReport, error) {
fromPath := path
fromConfig := c
fromConfig.Renderer = nil
Expand All @@ -23,7 +23,7 @@ func relFileReport(ctx context.Context, c Config, path string) (map[string]binca
if err != nil {
return nil, err
}
fromRelPath := map[string]bincapz.FileReport{}
fromRelPath := map[string]*bincapz.FileReport{}
for _, f := range fromReport.Files {
if f.Skipped != "" || f.Error != "" {
continue
Expand Down Expand Up @@ -54,10 +54,10 @@ func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
return nil, err
}

d := bincapz.DiffReport{
Added: map[string]bincapz.FileReport{},
Removed: map[string]bincapz.FileReport{},
Modified: map[string]bincapz.FileReport{},
d := &bincapz.DiffReport{
Added: map[string]*bincapz.FileReport{},
Removed: map[string]*bincapz.FileReport{},
Modified: map[string]*bincapz.FileReport{},
}

// things that appear in the source
Expand All @@ -73,9 +73,9 @@ func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
continue
}

rbs := bincapz.FileReport{
rbs := &bincapz.FileReport{
Path: tr.Path,
Behaviors: map[string]bincapz.Behavior{},
Behaviors: map[string]*bincapz.Behavior{},
PreviousRiskScore: fr.RiskScore,
PreviousRiskLevel: fr.RiskLevel,
RiskLevel: tr.RiskLevel,
Expand Down Expand Up @@ -107,9 +107,9 @@ func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
continue
}

abs := bincapz.FileReport{
abs := &bincapz.FileReport{
Path: tr.Path,
Behaviors: map[string]bincapz.Behavior{},
Behaviors: map[string]*bincapz.Behavior{},
PreviousRiskScore: fr.RiskScore,
PreviousRiskLevel: fr.RiskLevel,

Expand Down Expand Up @@ -151,12 +151,12 @@ func Diff(ctx context.Context, c Config) (*bincapz.Report, error) {
}

// We think that this file moved from rpath to apath.
abs := bincapz.FileReport{
abs := &bincapz.FileReport{
Path: tr.Path,
PreviousRelPath: rpath,
PreviousRelPathScore: score,

Behaviors: map[string]bincapz.Behavior{},
Behaviors: map[string]*bincapz.Behavior{},
PreviousRiskScore: fr.RiskScore,
PreviousRiskLevel: fr.RiskLevel,

Expand Down
6 changes: 3 additions & 3 deletions pkg/action/scan.go
Original file line number Diff line number Diff line change
Expand Up @@ -89,7 +89,7 @@ func recursiveScan(ctx context.Context, c Config) (*bincapz.Report, error) {
logger := clog.FromContext(ctx)
logger.Debug("scan", slog.Any("config", c))
r := &bincapz.Report{
Files: map[string]bincapz.FileReport{},
Files: map[string]*bincapz.FileReport{},
}
if len(c.IgnoreTags) > 0 {
r.Filter = strings.Join(c.IgnoreTags, ",")
Expand Down Expand Up @@ -194,11 +194,11 @@ func processFile(
if fr.RiskScore < c.MinFileScore {
return nil
}
if err := c.Renderer.File(ctx, *fr); err != nil {
if err := c.Renderer.File(ctx, fr); err != nil {
return fmt.Errorf("render: %w", err)
}
}
r.Files[path] = *fr
r.Files[path] = fr
return nil
}

Expand Down
33 changes: 16 additions & 17 deletions pkg/bincapz/bincapz.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,14 +27,14 @@ type FileReport struct {
Path string
SHA256 string
// compiler -> x
Error string `json:",omitempty" yaml:",omitempty"`
Skipped string `json:",omitempty" yaml:",omitempty"`
Meta map[string]string `json:",omitempty" yaml:",omitempty"`
Syscalls []string `json:",omitempty" yaml:",omitempty"`
Pledge []string `json:",omitempty" yaml:",omitempty"`
Capabilities []string `json:",omitempty" yaml:",omitempty"`
Behaviors map[string]Behavior `json:",omitempty" yaml:",omitempty"`
FilteredBehaviors int `json:",omitempty" yaml:",omitempty"`
Error string `json:",omitempty" yaml:",omitempty"`
Skipped string `json:",omitempty" yaml:",omitempty"`
Meta map[string]string `json:",omitempty" yaml:",omitempty"`
Syscalls []string `json:",omitempty" yaml:",omitempty"`
Pledge []string `json:",omitempty" yaml:",omitempty"`
Capabilities []string `json:",omitempty" yaml:",omitempty"`
Behaviors map[string]*Behavior `json:",omitempty" yaml:",omitempty"`
FilteredBehaviors int `json:",omitempty" yaml:",omitempty"`

// The relative path we think this moved from.
PreviousRelPath string `json:",omitempty" yaml:",omitempty"`
Expand All @@ -43,23 +43,22 @@ type FileReport struct {
PreviousRiskScore int `json:",omitempty" yaml:",omitempty"`
PreviousRiskLevel string `json:",omitempty" yaml:",omitempty"`

RiskScore int
RiskLevel string `json:",omitempty" yaml:",omitempty"`
PackageRisk []string `json:",omitempty" yaml:",omitempty"`
RiskScore int
RiskLevel string `json:",omitempty" yaml:",omitempty"`

IsBincapz bool `json:",omitempty" yaml:",omitempty"`
}

type DiffReport struct {
Added map[string]FileReport `json:",omitempty" yaml:",omitempty"`
Removed map[string]FileReport `json:",omitempty" yaml:",omitempty"`
Modified map[string]FileReport `json:",omitempty" yaml:",omitempty"`
Added map[string]*FileReport `json:",omitempty" yaml:",omitempty"`
Removed map[string]*FileReport `json:",omitempty" yaml:",omitempty"`
Modified map[string]*FileReport `json:",omitempty" yaml:",omitempty"`
}

type Report struct {
Files map[string]FileReport `json:",omitempty" yaml:",omitempty"`
Diff DiffReport `json:",omitempty" yaml:",omitempty"`
Filter string `json:",omitempty" yaml:",omitempty"`
Files map[string]*FileReport `json:",omitempty" yaml:",omitempty"`
Diff *DiffReport `json:",omitempty" yaml:",omitempty"`
Filter string `json:",omitempty" yaml:",omitempty"`
}

type IntMetric struct {
Expand Down
4 changes: 2 additions & 2 deletions pkg/render/json.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func NewJSON(w io.Writer) JSON {
return JSON{w: w}
}

func (r JSON) File(_ context.Context, _ bincapz.FileReport) error {
func (r JSON) File(_ context.Context, _ *bincapz.FileReport) error {
return nil
}

func (r JSON) Full(_ context.Context, rep bincapz.Report) error {
func (r JSON) Full(_ context.Context, rep *bincapz.Report) error {
j, err := json.MarshalIndent(rep, "", " ")
if err != nil {
return err
Expand Down
18 changes: 11 additions & 7 deletions pkg/render/markdown.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,20 +41,24 @@ func matchFragmentLink(s string) string {
return fmt.Sprintf("[%s](https://github.com/search?q=%s&type=code)", s, url.QueryEscape(s))
}

func (r Markdown) File(ctx context.Context, fr bincapz.FileReport) error {
markdownTable(ctx, &fr, r.w, tableConfig{Title: fmt.Sprintf("## %s [%s]", fr.Path, mdRisk(fr.RiskScore, fr.RiskLevel))})
func (r Markdown) File(ctx context.Context, fr *bincapz.FileReport) error {
markdownTable(ctx, fr, r.w, tableConfig{Title: fmt.Sprintf("## %s [%s]", fr.Path, mdRisk(fr.RiskScore, fr.RiskLevel))})
return nil
}

func (r Markdown) Full(ctx context.Context, rep bincapz.Report) error {
func (r Markdown) Full(ctx context.Context, rep *bincapz.Report) error {
if rep.Diff == nil {
return nil
}

for f, fr := range rep.Diff.Removed {
fr := fr
markdownTable(ctx, &fr, r.w, tableConfig{Title: fmt.Sprintf("## Deleted: %s [%s]", f, mdRisk(fr.RiskScore, fr.RiskLevel)), DiffRemoved: true})
markdownTable(ctx, fr, r.w, tableConfig{Title: fmt.Sprintf("## Deleted: %s [%s]", f, mdRisk(fr.RiskScore, fr.RiskLevel)), DiffRemoved: true})
}

for f, fr := range rep.Diff.Added {
fr := fr
markdownTable(ctx, &fr, r.w, tableConfig{Title: fmt.Sprintf("## Added: %s [%s]", f, mdRisk(fr.RiskScore, fr.RiskLevel)), DiffAdded: true})
markdownTable(ctx, fr, r.w, tableConfig{Title: fmt.Sprintf("## Added: %s [%s]", f, mdRisk(fr.RiskScore, fr.RiskLevel)), DiffAdded: true})
}

for f, fr := range rep.Diff.Modified {
Expand Down Expand Up @@ -85,14 +89,14 @@ func (r Markdown) Full(ctx context.Context, rep bincapz.Report) error {
}

if added > 0 {
markdownTable(ctx, &fr, r.w, tableConfig{
markdownTable(ctx, fr, r.w, tableConfig{
Title: fmt.Sprintf("### %d new behaviors", added),
SkipRemoved: true,
})
}

if removed > 0 {
markdownTable(ctx, &fr, r.w, tableConfig{
markdownTable(ctx, fr, r.w, tableConfig{
Title: fmt.Sprintf("### %d removed behaviors", removed),
SkipAdded: true,
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/render/render.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,8 +13,8 @@ import (

// Renderer is a common interface for Renderers.
type Renderer interface {
File(context.Context, bincapz.FileReport) error
Full(context.Context, bincapz.Report) error
File(context.Context, *bincapz.FileReport) error
Full(context.Context, *bincapz.Report) error
}

// New returns a new Renderer.
Expand Down
8 changes: 6 additions & 2 deletions pkg/render/simple.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ func NewSimple(w io.Writer) Simple {
return Simple{w: w}
}

func (r Simple) File(_ context.Context, fr bincapz.FileReport) error {
func (r Simple) File(_ context.Context, fr *bincapz.FileReport) error {
fmt.Fprintf(r.w, "# %s\n", fr.Path)
bs := []string{}

Expand All @@ -34,7 +34,11 @@ func (r Simple) File(_ context.Context, fr bincapz.FileReport) error {
return nil
}

func (r Simple) Full(_ context.Context, rep bincapz.Report) error {
func (r Simple) Full(_ context.Context, rep *bincapz.Report) error {
if rep.Diff == nil {
return nil
}

for f, fr := range rep.Diff.Removed {
fmt.Fprintf(r.w, "--- missing: %s\n", f)

Expand Down
6 changes: 3 additions & 3 deletions pkg/render/stats.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import (
"github.com/chainguard-dev/bincapz/pkg/report"
)

func riskStatistics(files map[string]bincapz.FileReport) ([]bincapz.IntMetric, int, int) {
func riskStatistics(files map[string]*bincapz.FileReport) ([]bincapz.IntMetric, int, int) {
riskMap := make(map[int][]string)
riskStats := make(map[int]float64)

Expand Down Expand Up @@ -49,12 +49,12 @@ func riskStatistics(files map[string]bincapz.FileReport) ([]bincapz.IntMetric, i
return stats, total(), processedFiles
}

func pkgStatistics(files map[string]bincapz.FileReport) ([]bincapz.StrMetric, int, int) {
func pkgStatistics(files map[string]*bincapz.FileReport) ([]bincapz.StrMetric, int, int) {
numNamespaces := 0
pkgMap := make(map[string]int)
pkg := make(map[string]float64)
for _, rf := range files {
for _, namespace := range rf.PackageRisk {
for namespace := range rf.Behaviors {
numNamespaces++
pkgMap[namespace]++
}
Expand Down
21 changes: 13 additions & 8 deletions pkg/render/terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ var maxExampleCount = 8

type KeyedBehavior struct {
Key string
Behavior bincapz.Behavior
Behavior *bincapz.Behavior
}

type tableConfig struct {
Expand Down Expand Up @@ -91,27 +91,32 @@ func ShortRisk(s string) string {
return short
}

func (r Terminal) File(ctx context.Context, fr bincapz.FileReport) error {
renderTable(ctx, &fr, r.w,
func (r Terminal) File(ctx context.Context, fr *bincapz.FileReport) error {
renderTable(ctx, fr, r.w,
tableConfig{
Title: fmt.Sprintf("%s %s", fr.Path, darkBrackets(decorativeRisk(fr.RiskScore, fr.RiskLevel))),
},
)
return nil
}

func (r Terminal) Full(ctx context.Context, rep bincapz.Report) error {
func (r Terminal) Full(ctx context.Context, rep *bincapz.Report) error {
// Non-diff files are handled on the fly by File()
if rep.Diff == nil {
return nil
}

for f, fr := range rep.Diff.Removed {
fr := fr
renderTable(ctx, &fr, r.w, tableConfig{
renderTable(ctx, fr, r.w, tableConfig{
Title: fmt.Sprintf("Deleted: %s %s", f, darkBrackets(decorativeRisk(fr.RiskScore, fr.RiskLevel))),
DiffRemoved: true,
})
}

for f, fr := range rep.Diff.Added {
fr := fr
renderTable(ctx, &fr, r.w, tableConfig{
renderTable(ctx, fr, r.w, tableConfig{
Title: fmt.Sprintf("Added: %s %s", f, darkBrackets(decorativeRisk(fr.RiskScore, fr.RiskLevel))),
DiffAdded: true,
})
Expand Down Expand Up @@ -144,14 +149,14 @@ func (r Terminal) Full(ctx context.Context, rep bincapz.Report) error {
}

if added > 0 {
renderTable(ctx, &fr, r.w, tableConfig{
renderTable(ctx, fr, r.w, tableConfig{
Title: color.HiWhiteString("+++ ADDED: %d behavior(s) +++", added),
SkipRemoved: true,
})
}

if removed > 0 {
renderTable(ctx, &fr, r.w, tableConfig{
renderTable(ctx, fr, r.w, tableConfig{
Title: color.HiWhiteString("--- REMOVED: %d behavior(s) ---", removed),
SkipAdded: true,
})
Expand Down
4 changes: 2 additions & 2 deletions pkg/render/yaml.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,11 @@ func NewYAML(w io.Writer) YAML {
return YAML{w: w}
}

func (r YAML) File(_ context.Context, _ bincapz.FileReport) error {
func (r YAML) File(_ context.Context, _ *bincapz.FileReport) error {
return nil
}

func (r YAML) Full(_ context.Context, rep bincapz.Report) error {
func (r YAML) Full(_ context.Context, rep *bincapz.Report) error {
yaml, err := yaml.Marshal(rep)
if err != nil {
return err
Expand Down
Loading

0 comments on commit 1b18970

Please sign in to comment.