Skip to content

perf(workbench): optimize memory footprint and ingestion performance for .khi datasets - #922

Merged
kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-xmwmvtzkwwrm
Aug 28, 2026
Merged

kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-xmwmvtzkwwrm

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 28, 2026

Copy link
Copy Markdown
Member

Summary

This PR significantly optimizes memory usage and ingestion performance when loading .khi inspection files in Workbench.

Key Optimizations

  1. FlatStructStore & Direct YAML Serializer:

    • Stores interned Protobuf structs in a pointer-free Structure-of-Arrays (SoA) layout (uint32 slices for ID, FieldPathSet ID, and byte-packed values).
    • Directly serializes YAML from flat slices without round-tripping through Protobuf structs.
  2. ReadonlyInternPool with Array Indexing:

    • Replaces sync.Map with compact, ID-indexed flat slices (strings []string, fieldSets [][]uint32) for reading workflows.
    • Eliminates reverse lookup maps (strToID, fieldSetToID) during read operations.
    • Restores the original InternPool for write operations (Inspector, Builder, etc.) to keep writing pipelines untainted.
  3. ReadonlyPool Interface:

    • Introduced polymorphic ReadonlyPool interface (ResolveStringFromID, ResolveFieldSetFromID, ResolveStructFromID) allowing both InternPool and ReadonlyInternPool to be consumed transparently by CEL evaluator, graph builders, and test suites.
  4. Primitive-Only cel.LogData & On-Demand Style Resolution:

    • Stripped pointer references from cel.LogData (now purely primitive uint32 IDs for log type, severity, summary, and struct).
    • Introduced StyleResolver to resolve log type strings and severity orders on demand during CEL evaluation.
    • Completely eliminated the pendingLogChunks buffer from Workbench.
  5. Streaming Ingestion in Worker Goroutines:

    • Decoupled LogChunk, TimelineChunk, and InterningPoolChunk inside parallel worker goroutines.
    • Ingests interned strings, field path sets, and flat structs directly via StoreProtoBatch within worker goroutines, completely avoiding queuing raw Protobuf intern chunks in channel buffers.

Real-Data Benchmark Results (1.4M Logs / 165k Timelines)

Measured against data/inspection-imported-1.khi (138.66 MB compressed, 1,413,091 logs, 165,938 timelines):

Metric Baseline (Before) This PR Improvement
Elapsed Time 13.51s 7.01s -48.1% (Nearly 2x faster)
Peak HeapAlloc 7,360 MB 6,544 MB -11.1% (~816 MB reduction)
Post-GC Resident Heap 6,206 MB 1,553 MB -75.0% (~4.65 GB reduction)
Total Allocated 16,846 MB 9,791 MB -41.9% (Sub-10GB achieved)
Total Mallocs 267.1M 151.8M -43.1% (>115M allocs eliminated)
GC Cycles 33 cycles 12 cycles -63.6%
…for .khi datasets

- Introduce pointer-free FlatStructStore (SoA layout) for interned structs and direct YAML serialization.
- Introduce ReadonlyInternPool with array-based lookups for strings, fieldsets, and structs, eliminating sync.Map and reverse lookup maps in reading phase.
- Streamline LogChunk and TimelineChunk processing directly into primitive-only cel.LogData and raw timelines within parallel worker goroutines.
- Resolve log types and severities on demand during CEL evaluation via StyleResolver, eliminating pendingLogChunks buffer.
- Add ReadonlyPool interface for polymorphic struct and value restoration across InternPool and ReadonlyInternPool.
- Real-data ingestion benchmarks on 1.4M logs show:
  - Post-GC heap reduced by 75% (6,206 MB -> 1,553 MB, ~4.65 GB reduction)
  - Ingestion time reduced by 48% (13.51s -> 7.01s)
  - Peak heap reduced by 11% (7,360 MB -> 6,544 MB)
  - Total allocations reduced by 42% (16.8 GB -> 9.79 GB)

@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 a memory-optimized, pointer-free FlatStructStore and ReadonlyInternPool to store interned strings, field path sets, and structs in primitive scalar slices, significantly reducing GC overhead. It also updates the direct YAML serializer and integrates these flat structures into the CEL log and timeline evaluators. The review feedback identifies critical issues, including a recursive locking deadlock in FlatStructStore, an out-of-bounds panic in StoreProtoBatch when the maximum struct ID is 0, a logical error in StructRefs that includes orphaned IDs, and a violation of the interning pool rule regarding the deletion of orphaned IDs.

Comment thread pkg/model/khifile/v6/flat_struct.go Resolved
Comment thread pkg/model/khifile/v6/flat_struct.go Resolved
Comment thread pkg/model/khifile/v6/flat_struct.go Resolved
Comment thread pkg/model/khifile/v6/flat_struct.go Resolved
Comment thread pkg/model/khifile/v6/intern.go Resolved
Comment thread pkg/model/khifile/v6/intern.go Resolved
@kyasbal kyasbal added the area:backend-core Core backend frameworks, DAG task runner, and server API label Aug 28, 2026
@kyasbal
kyasbal marked this pull request as ready for review August 28, 2026 06:41
@kyasbal
kyasbal merged commit f75d633 into GoogleCloudPlatform:main Aug 28, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-xmwmvtzkwwrm branch August 28, 2026 06:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

area:backend-core Core backend frameworks, DAG task runner, and server API

1 participant