[shell-operator] chore: reduce memory consumption#871
Open
Conversation
Signed-off-by: Pavel Okhlopkov <pavel.okhlopkov@flant.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
This PR targets several high-frequency allocation patterns across the hook execution and event processing pipeline. Together these changes reduce GC pressure and peak memory usage, especially for hooks with large snapshots or high-churn informers.
Changes
1. Zero-alloc deep copy for jq filter results (
pkg/filter/jq/apply.go)Replaced the
json.Marshal/json.Unmarshalround-trip indeepCopyAnywith a recursive type-switch walker. The old approach serialized every filter result to JSON and back just to get a copy — the new implementation copies maps, slices, and primitives directly without any serialization. This also preserves original numeric types (e.g.intstaysint) instead of coercing everything tofloat64.2. Streaming JSON for binding context files (
pkg/hook/hook.go,pkg/hook/binding_context/binding_context.go)Added
WriteJson(io.Writer)toBindingContextListand rewiredprepareBindingContextJsonFileto stream JSON directly to the file viajson.NewEncoder. Previously the entire binding context was marshaled into a[]byte(often multi-MB for synchronization events with many objects) and then written in a second step. The streaming approach cuts peak memory by eliminating this intermediate buffer. Also switchedJson()fromMarshalIndenttoMarshal— the indentation was never needed by hook scripts and added ~30% overhead in both CPU and memory.3. Pooled MD5 hashers for checksum computation (
pkg/utils/checksum/checksum.go)Introduced a
sync.Poolformd5.Hashinstances used byCalculateChecksum. Every watch event computes at least one checksum for the filter result, so this pool eliminates onemd5.New()allocation per event. Also addedCalculateChecksumOfBytes([]byte)to avoid the[]byte → string → []byteround-trip that occurred when checksum was computed from filter output bytes.4. Slice reuse in proxy logger (
pkg/executor/executor.go)Changed
pl.buf = []byte{}topl.buf = pl.buf[:0]to reuse the existing backing array instead of allocating a new slice on every log line.5. Checksum byte-path in filter (
pkg/kube_events_manager/filter.go)Switched all three
CalculateChecksum(string(filteredBytes))call sites toCalculateChecksumOfBytes(filteredBytes), eliminating a needless copy of the filter output on every watch event.