perf: reduce busybox init-time memory allocation#20024
perf: reduce busybox init-time memory allocation#20024
Conversation
Reduce init-time memory for the busybox binary by eliminating unnecessary imports, deferring allocations with sync.OnceValue, and breaking heavy transitive dependency chains. Results (Linux amd64): - Busybox: 16.1 MB -> 12.9 MB heap (-20%), 245K -> 173K mallocs (-29%) - AC standalone: 9.1 MB -> 7.2 MB heap (-21%), 87K -> 51K mallocs (-41%) - Binary size: 205 MB -> 194 MB (-5%) Generated with assistance from AI Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
Skipping CI for Draft Pull Request. |
f70d9c4 to
88838a7
Compare
🚀 Build Images ReadyImages are ready for commit 9e5c763. To use with deploy scripts: export MAIN_IMAGE_TAG=4.11.x-665-g9e5c7631a3 |
Test files still accessed schema variables and regex variables directly instead of calling the lazy wrapper functions: - schema.XxxSchema.OptionsMap → schema.XxxSchema().OptionsMap (16 files) - deploymentBaseSchema = schema.DeploymentsSchema → schema.DeploymentsSchema() - comparatorDecimalValueRegex → comparatorDecimalValueRegex() (validate_test.go) Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
88838a7 to
2b0ea0e
Compare
The schema.go.tpl template was updated to use sync.OnceValue but the test schemas in migrator/migrations/postgreshelper/schema/ and pkg/postgres/schema/test_*.go were not regenerated. CI's check-generated-files step regenerates them, producing a mismatch. Run: make go-generated-srcs Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
8070e24 to
89ee853
Compare
With sync.OnceValue schemas, registration happens on first access. Tests that explicitly call RegisterCategoryToTable or RegisterTable after accessing a schema would cause fatal duplicate registration. Make both functions idempotent — silently ignore re-registration of the same table. Also fix select_field_test.go which incorrectly added () to TestStructsSchema (a test schema not converted to sync.OnceValue). Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
da02250 to
3afcaae
Compare
StackRox lint rules require "github.com/stackrox/rox/pkg/sync" instead of stdlib "sync". Add OnceValue wrapper to pkg/sync/common_aliases.go and update the schema template to use it. Regenerated all schema files. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
3afcaae to
e521989
Compare
With sync.OnceValue, schema construction is lazy — but ApplyAllSchemas needs table create statements in registeredTables at startup to create database tables. Split registration into two phases: 1. init(): RegisterTableStmt registers table name + create statement (cheap, no walker.Walk) 2. Lazy: RegisterTable updates with the full walker.Schema on first schema function access Also fixes: - goimports formatting on pkg/branding, pkg/cloudproviders/aws - Remove unused //nolint:wrapcheck directive - Regenerate all schema files with init() registration Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
c39e023 to
59839c0
Compare
Codecov Report❌ Patch coverage is Additional details and impacted files@@ Coverage Diff @@
## master #20024 +/- ##
==========================================
+ Coverage 49.61% 49.70% +0.09%
==========================================
Files 2765 2765
Lines 208628 209432 +804
==========================================
+ Hits 103509 104107 +598
- Misses 97462 97663 +201
- Partials 7657 7662 +5
Flags with carried forward coverage won't be shown. Click here to find out more. ☔ View full report in Codecov by Sentry. 🚀 New features to boost your workflow:
|
|
/test gke-qa-e2e-tests |
|
/test gke-qa-e2e-tests |
- Remove tools/measure-busybox-mem/ (unrelated to this PR, measures import chain costs not sync.OnceValue improvement) - Fix schema.go.tpl import ordering: move pkg/sync to third-party group (was incorrectly mixed with stdlib imports) - Regenerate all schema files with corrected import ordering Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
|
/test gke-qa-e2e-tests |
Reduce init-time allocations by deferring computation to first use: - pkg/booleanpolicy: lazy regexp compilation via sync.OnceValue, lazy evaluator.Factory via lazyFactory struct - pkg/branding: lazy branded logo loading - pkg/cloudproviders/aws: lazy root CA pool construction - pkg/probeupload: lazy probe name regex - pkg/printers/table: moved to sub-package to break transitive import chain (roxctl → pkg/printers → tablewriter) - pkg/postgres/id: inline CVE ID utilities (avoids heavyweight pkg/cve import chain) - sensor/kubernetes/fake: //go:build fakeworkloads tag excludes fake workload generator from production binary - sensor/common/centralproxy: move test code to _test.go Depends on: #20024 (for pkg/sync.OnceValue wrapper) AI-assisted. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
d165ee7 to
e321a4b
Compare
Keep only schema lazy loading (sync.OnceValue) changes in this PR: - Template changes (schema.go.tpl, singleton.go.tpl, store.go.tpl) - Schema infrastructure (all.go, mapping.go, common_aliases.go) - Generated schema files - Mechanical caller updates (Schema.Field → Schema().Field) Moved to separate PR: - pkg/booleanpolicy lazy regexp/factory - pkg/branding lazy logo - pkg/cloudproviders/aws lazy certs - pkg/probeupload lazy regex - pkg/printers/table sub-package move - pkg/postgres/id inline utilities - sensor/kubernetes/fake build tags - sensor/common/centralproxy testutils rename Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
e321a4b to
9e5c763
Compare
|
/test gke-qa-e2e-tests |
Description
Reduce init-time heap allocations in the busybox binary through lazy initialization patterns. The busybox binary is the shared entrypoint for sensor, admission-control, config-controller, and other components — savings here apply to all.
Changes
pkg/postgres/schemaregistration withsync.OnceValue(~200 generated files)pkg/booleanpolicypkg/printers/tableto sub-package (breaks transitive import chain)sensor/common/centralproxy/testutils.goto_test.gosensor/kubernetes/faketo exclude from production binarypkg/cveimport chainMeasurements
Risk
Low. Changes are lazy wrappers around existing code. sync.OnceValue is thread-safe. Schema registration is idempotent. Build tags verified with CI.
Part of the memory baseline optimization series.
User-facing documentation
Testing and quality
Automated testing
How I validated my change
sensor/kubernetes/fakefrom production binaryAI-assisted.