Skip to content

Commit ebcf6f6

Browse files
authored
Revert "fix: add and leverage helper functions for [safely] setting capacity (#2318)" (#2320)
This reverts commit ae3c12f. This package adds unnecessary noise around a very common operation. If a melange program actually has anywhere close to `math.MaxInt` values of anything, we're never going to actually finish executing it, or we'll run out of memory pretty quickly. Go is a memory safe language. If you attempt to allocate a slice with negative capacity, it will panic, which is the only reasonable thing to do. Attempting to "safely" allocate, instead, `math.MaxInt` of anything just delays the problem very slightly. If we actually need that capacity, we're not going to be able to execute the program. Signed-off-by: Jon Johnson <jon.johnson@chainguard.dev>
1 parent bd13253 commit ebcf6f6

9 files changed

Lines changed: 9 additions & 90 deletions

File tree

internal/capacity/cap.go

Lines changed: 0 additions & 25 deletions
This file was deleted.

internal/capacity/cap_test.go

Lines changed: 0 additions & 50 deletions
This file was deleted.

pkg/build/sbom/spdx/spdx.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,6 @@ import (
2727
"chainguard.dev/apko/pkg/sbom/generator/spdx"
2828
"github.com/spdx/tools-golang/spdx/v2/common"
2929

30-
"chainguard.dev/melange/internal/capacity"
3130
build "chainguard.dev/melange/pkg/build/sbom"
3231
"chainguard.dev/melange/pkg/sbom"
3332
)
@@ -100,7 +99,7 @@ type Generator struct{}
10099
// It returns a map of package names to their corresponding SPDX documents.
101100
func (g *Generator) GenerateSPDX(ctx context.Context, gc *build.GeneratorContext) (map[string]spdx.Document, error) {
102101
// Collect all package names
103-
pkgNames := make([]string, 0, capacity.Add(len(gc.Configuration.Subpackages), 1))
102+
pkgNames := make([]string, 0, len(gc.Configuration.Subpackages)+1)
104103
pkgNames = append(pkgNames, gc.Configuration.Package.Name)
105104
for _, sp := range gc.Configuration.Subpackages {
106105
pkgNames = append(pkgNames, sp.Name)

pkg/build/sca_interface.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import (
2222
"chainguard.dev/apko/pkg/apk/apk"
2323
apkofs "chainguard.dev/apko/pkg/apk/fs"
2424

25-
"chainguard.dev/melange/internal/capacity"
2625
"chainguard.dev/melange/pkg/config"
2726
"chainguard.dev/melange/pkg/sca"
2827
)
@@ -42,7 +41,7 @@ func (scabi *SCABuildInterface) PackageName() string {
4241
// RelativeNames returns all the package names relating to the package being
4342
// built.
4443
func (scabi *SCABuildInterface) RelativeNames() []string {
45-
targets := make([]string, 0, capacity.Add(len(scabi.PackageBuild.Build.Configuration.Subpackages), 1))
44+
targets := make([]string, 0, len(scabi.PackageBuild.Build.Configuration.Subpackages)+1)
4645
targets = append(targets, scabi.PackageBuild.Origin.Name)
4746

4847
for _, target := range scabi.PackageBuild.Build.Configuration.Subpackages {

pkg/cli/scan.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ import (
3434
"github.com/spf13/cobra"
3535
"go.opentelemetry.io/otel"
3636

37-
"chainguard.dev/melange/internal/capacity"
3837
"chainguard.dev/melange/pkg/build"
3938
"chainguard.dev/melange/pkg/config"
4039
"chainguard.dev/melange/pkg/sca"
@@ -431,7 +430,7 @@ func (s *scaImpl) PackageName() string {
431430
}
432431

433432
func (s *scaImpl) RelativeNames() []string {
434-
targets := make([]string, 0, capacity.Add(len(s.pb.Build.Configuration.Subpackages), 1))
433+
targets := make([]string, 0, len(s.pb.Build.Configuration.Subpackages)+1)
435434
targets = append(targets, s.pb.Origin.Name)
436435

437436
for _, target := range s.pb.Build.Configuration.Subpackages {

pkg/cli/test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"go.opentelemetry.io/otel"
3030
"golang.org/x/sync/errgroup"
3131

32-
"chainguard.dev/melange/internal/capacity"
3332
"chainguard.dev/melange/pkg/build"
3433
)
3534

@@ -206,7 +205,7 @@ func TestCmd(ctx context.Context, archs []apko_types.Architecture, baseOpts ...b
206205
// https://github.com/distroless/nginx/runs/7219233843?check_suite_focus=true
207206
bcs := []*build.Test{}
208207
for _, arch := range archs {
209-
opts := make([]build.TestOption, 0, capacity.Add(len(baseOpts), 1))
208+
opts := make([]build.TestOption, 0, len(baseOpts)+1)
210209
opts = append(opts, build.WithTestArch(arch))
211210
opts = append(opts, baseOpts...)
212211

pkg/config/config.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,13 +39,13 @@ import (
3939
"github.com/github/go-spdx/v2/spdxexp"
4040
purl "github.com/package-url/packageurl-go"
4141

42-
"chainguard.dev/melange/internal/capacity"
4342
"chainguard.dev/melange/pkg/sbom"
44-
"chainguard.dev/melange/pkg/util"
4543

4644
"github.com/chainguard-dev/clog"
4745
"github.com/joho/godotenv"
4846
"gopkg.in/yaml.v3"
47+
48+
"chainguard.dev/melange/pkg/util"
4949
)
5050

5151
const (
@@ -1273,7 +1273,7 @@ func buildConfigMap(cfg *Configuration) map[string]string {
12731273
}
12741274

12751275
func replacerFromMap(with map[string]string) *strings.Replacer {
1276-
replacements := make([]string, 0, capacity.Mul(len(with), 2))
1276+
replacements := make([]string, 0, len(with)*2)
12771277
for k, v := range with {
12781278
replacements = append(replacements, k, v)
12791279
}

pkg/container/qemu_runner.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -58,7 +58,6 @@ import (
5858
"golang.org/x/crypto/ssh/knownhosts"
5959
"golang.org/x/term"
6060

61-
"chainguard.dev/melange/internal/capacity"
6261
"chainguard.dev/melange/internal/logwriter"
6362
"chainguard.dev/melange/pkg/license"
6463
)
@@ -2007,7 +2006,7 @@ func generateBaseInitramfs(ctx context.Context, cfg *Config, initramfsPath, cach
20072006
}
20082007

20092008
// Start with base packages and add any additional packages from environment
2010-
packages := make([]string, 0, capacity.Add(len(additionalPkgs), 1))
2009+
packages := make([]string, 0, len(additionalPkgs)+1)
20112010
packages = append(packages, "microvm-init")
20122011
packages = append(packages, additionalPkgs...)
20132012

pkg/sca/kernel_sca.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,6 @@ import (
2929
"strings"
3030
"sync"
3131

32-
"chainguard.dev/melange/internal/capacity"
3332
"chainguard.dev/melange/pkg/config"
3433

3534
"github.com/chainguard-dev/clog"
@@ -59,7 +58,7 @@ func generateKernelDeps(ctx context.Context, hdl SCAHandle, generated *config.De
5958
return err
6059
}
6160

62-
allKernelDirs := make([]string, 0, capacity.Add(len(BootDirs), len(ModuleDirs)))
61+
allKernelDirs := make([]string, 0, len(BootDirs)+len(ModuleDirs))
6362
allKernelDirs = append(allKernelDirs, BootDirs...)
6463
allKernelDirs = append(allKernelDirs, ModuleDirs...)
6564

0 commit comments

Comments
 (0)