Skip to content

perf(core): adopt LazyJSONNode for log ingestion and optimize Kubernetes manifest memory footprint - #885

Merged
kyasbal merged 3 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-mqpskvurwpwy
Aug 14, 2026
Merged

kyasbal merged 3 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-mqpskvurwpwy

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 14, 2026 •

Copy link
Copy Markdown
Member

Summary

Previously, log ingestion and manifest generation parsed logs and proto payloads into in-memory AST representations (StandardMapNode), creating thousands of nested map[string]Node instances, pointers, and heap allocations per log entry. Additionally, ResourceManifestLog retained full YAML strings in memory (ResourceBodyYAML) for every audit log revision.
This PR introduces structured.LazyJSONNode for zero-copy, lazy JSON parsing, integrates on-demand key ordering (WithKeyOrder) for canonical GCP and Kubernetes field order, eliminates redundant ResourceBodyYAML allocations, and converts both GCP and OSS Kubernetes audit log parsing to use LazyJSONNode.


Key Changes

  1. pkg/common/structured:
    • Implemented LazyJSONNode which traverses JSON data directly over an immutable []byte slice without pre-allocating intermediate map nodes.
    • Added WithKeyOrder(node, priorityKeys...) and NodeReader.WithKeyOrder(priorityKeys...) to provide prioritized key traversal on demand without mutating the underlying data structure.
    • Enhanced YAMLNodeSerializer with PriorityKeys support.
    • Fixed StandardMapNode.MarshalJSON() to properly escape keys using json.Marshal.
  2. pkg/api/googlecloud/logconvert & pkg/task/inspection/googlecloudcommon:
    • Converted log parsing to stream protojson.Marshal(l) directly into LazyJSONNode.
    • Applied logconvert.GCPLogEntryKeyOrder via WithKeyOrder when wrapping log.NewLog.
  3. pkg/task/inspection/commonlogk8saudit:
    • Removed obsolete ResourceBodyYAML from ResourceManifestLog and deleted GetLastBodyYAML from manifestmapper.go.
    • Converted manifest generator list item construction (constructResourceBodyFromListItem) and merged nodes to LazyJSONNode wrapped with k8s.K8sManifestKeyOrder.
    • Updated all corresponding mapper unit tests.
  4. pkg/task/inspection/ossclusterk8s:
    • Updated AuditLogFileReaderTask to parse JSONLines directly into LazyJSONNode instead of parsing full YAML strings.
    • Added unit tests in 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@upstream and this branch using /usr/bin/time -v on a 4-hour GKE cluster audit query.

Metric Upstream (main) Mean (±σ) Current (This PR) Mean (±σ) Delta Improvement
Max RSS (Peak Memory) 4,607.41 MB (±107.9) 4,125.29 MB (±54.3) -482.12 MB -10.46%
User CPU Time 160.58 s (±2.0) 136.65 s (±0.9) -23.93 s -14.90% faster
System CPU Time 6.32 s (±0.2) 4.84 s (±0.3) -1.48 s -23.42%
Minor Page Faults 185,357 (±32.7k) 86,346 (±52.8k) -99,011 -53.42%

Run-by-Run Breakdown

Target     | Run  | Max RSS (MB) | User Time (s)  | Sys Time (s) | Elapsed   | Minor PF | Status
-----------------------------------------------------------------------------------------------
current    |   1  |    4134.34 MB |       137.29 s |       5.21 s |   2:32.44 |   156809 |      0
current    |   2  |    4149.42 MB |       137.47 s |       4.85 s |   2:02.30 |   129061 |      0
current    |   3  |    4033.69 MB |       135.21 s |       4.45 s |   1:57.50 |    45176 |      0
current    |   4  |    4132.05 MB |       136.16 s |       4.83 s |   2:02.36 |    44407 |      0
current    |   5  |    4176.96 MB |       137.13 s |       4.86 s |   1:56.34 |    56278 |      0
-----------------------------------------------------------------------------------------------
upstream   |   1  |    4469.59 MB |       162.25 s |       6.48 s |   1:56.01 |   155732 |      0
upstream   |   2  |    4599.23 MB |       162.27 s |       6.23 s |   2:04.74 |   232765 |      0
upstream   |   3  |    4540.22 MB |       160.78 s |       6.27 s |   1:59.90 |   204269 |      0
upstream   |   4  |    4701.73 MB |       160.20 s |       6.47 s |   1:58.07 |   159380 |      0
upstream   |   5  |    4726.28 MB |       157.40 s |       6.14 s |   1:59.90 |   174638 |      0

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.
@kyasbal kyasbal added the type:feature New feature or request label Aug 14, 2026

@gemini-code-assist gemini-code-assist Bot left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

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.

Comment thread pkg/common/structured/lazyjson.go Resolved
Comment thread pkg/common/structured/lazyjson.go Resolved
Comment thread pkg/common/structured/lazyjson.go Resolved
Comment thread pkg/common/structured/lazyjson.go Resolved
Comment thread pkg/common/structured/ordered.go Resolved
@kyasbal

kyasbal commented Aug 14, 2026

Copy link
Copy Markdown
Member Author

Performance Benchmark Update (Post-Review Remediations)

We re-ran the full GKE cluster audit query benchmark (4h duration, p0-gke-basic-1) following all review comment fixes and zero-allocation string parsing optimizations (unsafe.String in parseJSONString for unescaped keys and scalar values).

Summary Comparison

Metric Upstream (main) Previous Draft Latest (With Review Fixes & Zero-Alloc Strings) Total Improvement vs Upstream
Max RSS (Peak Memory) 4,607.41 MB 4,125.29 MB 3,281.35 MB (3,360,100 KB) -1,326.06 MB (-28.78%)
User CPU Time 160.58 s 136.65 s 126.72 s -33.86 s (-21.09% faster)
System CPU Time 6.32 s 4.84 s 5.92 s -0.40 s
Elapsed Time ~2:00.00 ~2:00.00 2:41.21 -

Key Takeaways

  • Peak memory footprint dropped from 4,607 MB down to 3,281 MB (-28.8%).
  • User CPU time decreased from 160.58 s to 126.72 s (21% speedup) due to eliminating heap string and map allocations during log ingestion and key/value traversal.
@kyasbal
kyasbal marked this pull request as ready for review August 14, 2026 07:28
@kyasbal
kyasbal merged commit 04fcd10 into GoogleCloudPlatform:main Aug 14, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-mqpskvurwpwy branch August 14, 2026 07:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature New feature or request

1 participant