Repository navigation
perf(core): adopt LazyJSONNode for log ingestion and optimize Kubernetes manifest memory footprint - #885
Conversation
There was a problem hiding this comment.
Code Review
This pull request introduces LazyJSONNode to enable zero-allocation sub-slice reuse when parsing JSON, refactors GCP log conversion to utilize this lazy node, and adds a WithKeyOrder helper to customize map iteration order. It also simplifies Kubernetes manifest generation by removing the redundant ResourceBodyYAML field in favor of ResourceBodyReader. The review feedback focuses on further optimizing the performance of LazyJSONNode by eliminating heap allocations (using unsafe conversions, pre-allocating strings.Builder, performing direct byte comparisons, and implementing a custom hex parser) and preventing redundant nesting in WithKeyOrder.
Performance Benchmark Update (Post-Review Remediations)We re-ran the full GKE cluster audit query benchmark (4h duration, Summary Comparison
Key Takeaways
|
Summary
Previously, log ingestion and manifest generation parsed logs and proto payloads into in-memory AST representations (
StandardMapNode), creating thousands of nestedmap[string]Nodeinstances, pointers, and heap allocations per log entry. Additionally,ResourceManifestLogretained full YAML strings in memory (ResourceBodyYAML) for every audit log revision.This PR introduces
structured.LazyJSONNodefor zero-copy, lazy JSON parsing, integrates on-demand key ordering (WithKeyOrder) for canonical GCP and Kubernetes field order, eliminates redundantResourceBodyYAMLallocations, and converts both GCP and OSS Kubernetes audit log parsing to useLazyJSONNode.Key Changes
pkg/common/structured:LazyJSONNodewhich traverses JSON data directly over an immutable[]byteslice without pre-allocating intermediate map nodes.WithKeyOrder(node, priorityKeys...)andNodeReader.WithKeyOrder(priorityKeys...)to provide prioritized key traversal on demand without mutating the underlying data structure.YAMLNodeSerializerwithPriorityKeyssupport.StandardMapNode.MarshalJSON()to properly escape keys usingjson.Marshal.pkg/api/googlecloud/logconvert&pkg/task/inspection/googlecloudcommon:protojson.Marshal(l)directly intoLazyJSONNode.logconvert.GCPLogEntryKeyOrderviaWithKeyOrderwhen wrappinglog.NewLog.pkg/task/inspection/commonlogk8saudit:ResourceBodyYAMLfromResourceManifestLogand deletedGetLastBodyYAMLfrommanifestmapper.go.constructResourceBodyFromListItem) and merged nodes toLazyJSONNodewrapped withk8s.K8sManifestKeyOrder.pkg/task/inspection/ossclusterk8s:AuditLogFileReaderTaskto parse JSONLines directly intoLazyJSONNodeinstead of parsing full YAML strings.auditfilereader_task_test.go.Benchmark Results (5-Run Comparison on Real GKE Cluster Logs, 4h Query)
We executed 5 consecutive benchmark runs for both
main@upstreamand this branch using/usr/bin/time -von a 4-hour GKE cluster audit query.main) Mean (±σ)Run-by-Run Breakdown
Verification
make test-go: All backend tests passed.make test-web: All frontend tests passed.make pre-commit: All formatters and linters passed with 0 issues.