Optimization fixes#126
Conversation
Add thread-safe singleton pattern using sync.Once for NonAdminScheme() and CodecFactory creation in output package. This eliminates redundant allocations on every JSON/YAML output operation. Changes: - NonAdminScheme() now uses sync.Once to create scheme only once - CodecFactory is cached as singleton, created only on first use - Add TestNonAdminSchemeConcurrency to verify thread-safety Impact: Reduces latency and memory allocations for all nonadmin get commands with -o yaml/json output. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add sync.Map-based cache for HTTP clients indexed by timeout duration. This eliminates repeated client allocations during download operations. Changes: - httpClientWithTimeout() now caches clients by timeout value - Use sync.Map for thread-safe caching without lock contention - Add TestHTTPClientCaching to verify caching behavior Impact: Reduces allocations during describe --details operations (4 downloads per command) and all other download workflows. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
Add sync.Map-based cache for runtime.Scheme objects indexed by ClientOptions configuration. This eliminates repeated scheme creation across different commands using the same configuration. Changes: - NewSchemeWithTypes() now caches schemes by configuration key - Add schemeKey type to uniquely identify scheme configurations - Add TestNewSchemeWithTypesCaching tests to verify caching Impact: Reduces allocations for ~19 client creation calls across different commands that use the same scheme configuration. Co-Authored-By: Claude Sonnet 4.5 <noreply@anthropic.com>
📝 WalkthroughWalkthroughReworks NABSL request lookup, adds caching for runtime schemes and HTTP clients, converts non-admin output to singleton initialization, removes the non-admin backup force flag and related flows, extends command-wrapping detection to parent commands, and adds concurrency and cache tests. Changes
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes Possibly related PRs
Suggested labels
Suggested reviewers
Poem
🚥 Pre-merge checks | ✅ 1 | ❌ 2❌ Failed checks (1 warning, 1 inconclusive)
✅ Passed checks (1 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches
🧪 Generate unit tests (beta)
Comment |
There was a problem hiding this comment.
🧹 Nitpick comments (1)
cmd/nabsl-request/get.go (1)
129-136: Comment is misleading for the all-namespaces case.The comment states "List all requests in admin namespace" but the code also handles the
--all-namespacesflag which lists across all namespaces. Consider updating the comment to reflect both cases.📝 Suggested comment fix
- // List all requests in admin namespace + // List requests: either across all namespaces or in admin namespace only var requestList nacv1alpha1.NonAdminBackupStorageLocationRequestList var err error if o.AllNamespaces {🤖 Prompt for AI Agents
Verify each finding against the current code and only fix it if needed. In `@cmd/nabsl-request/get.go` around lines 129 - 136, Update the misleading comment above the listing logic to accurately describe both branches: when o.AllNamespaces is true the code lists NonAdminBackupStorageLocationRequestList across all namespaces via o.client.List, otherwise it lists only in the admin namespace using kbclient.InNamespace(adminNS); reference the variable requestList, the type nacv1alpha1.NonAdminBackupStorageLocationRequestList, the flag o.AllNamespaces, and the call o.client.List so the comment clearly reflects both cases.
🤖 Prompt for all review comments with AI agents
Verify each finding against the current code and only fix it if needed.
Nitpick comments:
In `@cmd/nabsl-request/get.go`:
- Around line 129-136: Update the misleading comment above the listing logic to
accurately describe both branches: when o.AllNamespaces is true the code lists
NonAdminBackupStorageLocationRequestList across all namespaces via
o.client.List, otherwise it lists only in the admin namespace using
kbclient.InNamespace(adminNS); reference the variable requestList, the type
nacv1alpha1.NonAdminBackupStorageLocationRequestList, the flag o.AllNamespaces,
and the call o.client.List so the comment clearly reflects both cases.
73adf77 to
cf096e6
Compare
There was a problem hiding this comment.
The schemeCache adds complexity (schemeKey struct, sync.Map, LoadOrStore semantics, shared mutable state) to cache something that is created exactly once per process lifetime. It provides no performance benefit for a short-lived CLI process. The sync.Once approach in
output.go is at least defensible since NonAdminScheme() could theoretically be called from multiple places within one command, but the sync.Map scheme cache in client.go solves a problem that doesn't exist.
do others agree?
| // NonAdminScheme returns a runtime.Scheme with NonAdmin types registered | ||
| func NonAdminScheme() *runtime.Scheme { |
There was a problem hiding this comment.
| // NonAdminScheme returns a runtime.Scheme with NonAdmin types registered | |
| func NonAdminScheme() *runtime.Scheme { | |
| // NonAdminScheme returns a runtime.Scheme with NonAdmin types registered | |
| // callers must not mutate the returned object | |
| func NonAdminScheme() *runtime.Scheme { |
|
Agree with @kaovilai's points. Since this is a CLI (short-lived process), scheme and client creation happens once per command execution anyway, so the caching wouldn't have much impact here. The Would be helpful to see some profiling data or benchmarks showing where time is being spent. That would help us evaluate whether this complexity is worth it. |
Why the changes were made
This PR introduces performance optimizations and code simplifications to reduce memory allocations and improve command execution latency across the OADP CLI.
Performance Optimizations:
singletons using
sync.Oncedescribe --detailsand otherdownload workflows)
runtime.Schemeobjects by configuration to eliminate repeated scheme creation across ~19 client creation calls that use the sameconfiguration
Impact: These changes reduce memory allocations and latency for all non-admin get commands with
-o yaml/jsonoutput, download operations, and commands that createKubernetes clients.