perf: optimize memory allocations in CoProcess middleware#7997
perf: optimize memory allocations in CoProcess middleware#7997probelabs[bot] wants to merge 3 commits intomasterfrom
Conversation
|
API Changes no api changes detected |
|
This PR optimizes memory allocations within the CoProcess (gRPC) middleware to reduce memory churn and improve performance, particularly for requests involving large session objects. Files Changed Analysis
The changes are focused and strategic, targeting key functions responsible for constructing the gRPC payload. The bulk of the new code consists of benchmarks and tests to validate the performance improvements. Architecture & Impact Assessment
VisualizationThe following diagram illustrates the components affected by the optimizations: graph TD
subgraph tyk_gateway [Tyk Gateway]
A[HTTP Request Received] --> B{CoProcess Middleware Enabled?};
B -->|Yes| C[BuildObject];
C --> D[ProtoSessionState];
D --> E[Create gRPC Payload];
E --> F[Dispatch to gRPC Server];
B -->|No| G[Continue Gateway Flow];
end
subgraph pr_optimizations ["PR Optimizations"]
C --|Avoids empty map/slice allocations for<br/>SetHeaders, DeleteHeaders, etc.|--> C;
D --|Conditionally allocates BasicAuthData, JWTData, etc.<br/>Pre-allocates slice capacities|--> D;
end
style C fill:#c9f,stroke:#333,stroke-width:2px
style D fill:#c9f,stroke:#333,stroke-width:2px
Scope Discovery & Context Expansion
Metadata
Powered by Visor from Probelabs Last updated: 2026-04-13T19:39:33.338Z | Triggered by: pr_updated | Commit: a3a2560 💡 TIP: You can chat with Visor using |
✅ Security Check PassedNo security issues found – changes LGTM. Architecture Issues (1)
✅ Performance Check PassedNo performance issues found – changes LGTM. Powered by Visor from Probelabs Last updated: 2026-04-13T19:39:04.851Z | Triggered by: pr_updated | Commit: a3a2560 💡 TIP: You can chat with Visor using |
🚨 Jira Linter FailedCommit: The Jira linter failed to validate your PR. Please check the error details below: 🔍 Click to view error detailsNext Steps
This comment will be automatically deleted once the linter passes. |
|




Problem / Task
The user wants to replicate the memory leak/high memory usage issue with gRPC middleware (coprocess) and implement easy wins to cut down memory allocations in
ProtoSessionStateandCoProcessor.BuildObject.Changes
BenchmarkGRPCDispatch_MemoryOverheadto measure allocations per request for gRPC middleware, andTestGRPCDispatch_MemoryLeakCheckto prove it's churn, not a leak. Added an empty session benchmark.BasicAuthData,JWTData,Monitor,metadata, andaccessDefinitionsif they contain data. Pre-allocateallowedUrlsslice capacity.SetHeaders,DeleteHeaders,AddParams,ExtendedParams,DeleteParamsas nil instead of allocating empty maps/slices. Removed redundantobject.Spec = make(map[string]string)allocation. Pre-allocateMultivalueHeadersslice capacity.nilif the input map is empty, and pre-allocate capacity if it's not.Testing
go test -v -bench BenchmarkGRPCDispatch_MemoryOverhead -run ^$ ./coprocess/grpc/go test -v -run TestGRPCDispatch_MemoryLeakCheck ./coprocess/grpc/