Skip to content

Commit 25adc19

Browse files
tmshortclaude
andcommitted
⚡ Add slice pre-allocation optimizations
Optimize slice allocations in boxcutter to reduce memory overhead: 1. Pre-allocate trimmedPrevious with ClusterExtensionRevisionPreviousLimit capacity - Avoids reallocation as the slice grows 2. Smart pre-allocation in splitManifestDocuments - Estimates document count based on line count - Reduces allocations when processing large helm manifests - Minimum capacity of 4 for small bundles These micro-optimizations reduce GC pressure during revision processing. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Co-Authored-By: Claude <noreply@anthropic.com>
1 parent 1721d10 commit 25adc19

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

internal/operator-controller/applier/boxcutter.go

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -335,7 +335,8 @@ func (bc *Boxcutter) apply(ctx context.Context, contentFS fs.FS, ext *ocv1.Clust
335335
// ClusterExtensionRevisionPreviousLimit or to the first _active_ revision and deletes trimmed revisions from the cluster.
336336
// NOTE: revisionList must be sorted in chronographical order, from oldest to latest.
337337
func (bc *Boxcutter) setPreviousRevisions(ctx context.Context, latestRevision *ocv1.ClusterExtensionRevision, revisionList []ocv1.ClusterExtensionRevision) error {
338-
trimmedPrevious := make([]ocv1.ClusterExtensionRevisionPrevious, 0)
338+
// Pre-allocate with capacity limit to reduce allocations
339+
trimmedPrevious := make([]ocv1.ClusterExtensionRevisionPrevious, 0, ClusterExtensionRevisionPreviousLimit)
339340
for index, r := range revisionList {
340341
if index < len(revisionList)-ClusterExtensionRevisionPreviousLimit && r.Spec.LifecycleState == ocv1.ClusterExtensionRevisionLifecycleStateArchived {
341342
// Delete oldest CREs from the cluster and list to reach ClusterExtensionRevisionPreviousLimit or latest active revision
@@ -377,9 +378,16 @@ func latestRevisionNumber(prevRevisions []ocv1.ClusterExtensionRevision) int64 {
377378
}
378379

379380
func splitManifestDocuments(file string) []string {
380-
//nolint:prealloc
381-
var docs []string
382-
for _, manifest := range strings.Split(file, "\n") {
381+
// Estimate: typical manifests have ~50-100 lines per document
382+
// Pre-allocate for reasonable bundle size to reduce allocations
383+
lines := strings.Split(file, "\n")
384+
estimatedDocs := len(lines) / 20 // conservative estimate
385+
if estimatedDocs < 4 {
386+
estimatedDocs = 4
387+
}
388+
docs := make([]string, 0, estimatedDocs)
389+
390+
for _, manifest := range lines {
383391
manifest = strings.TrimSpace(manifest)
384392
if len(manifest) == 0 {
385393
continue

0 commit comments

Comments
 (0)