Skip to content

perf(server): optimize server-side .khi file loading and timeline filtering - #903

Merged
kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-pukklrsvnpms
Aug 26, 2026
Merged

kyasbal merged 2 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-pukklrsvnpms

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 26, 2026

Copy link
Copy Markdown
Member

Summary

This PR optimizes server-side .khi file loading and timeline filtering performance on the KHI backend.

Background & Motivation

When opening large inspection files (e.g. 200,000+ logs, hundreds of megabytes), decoding interned structures and evaluating CEL filters on timeline logs incurred heavy CPU and memory overhead:

  1. Converting interned structs into intermediate generic objects before YAML encoding generated excessive heap allocations.
  2. Binary reader processed chunks with suboptimal concurrency and decompression buffering.
  3. Timeline bitset decoding and log CEL filtering required repeated conversions and slice reallocations.

Key Changes

1. Direct YAML Serializer (pkg/model/khifile/v6/direct_yaml.go)

  • Implemented DirectYAMLSerializer, directly serializing interned structs and fields from InternPool into YAML bytes without allocating intermediate generic map/struct representations.
  • Added comprehensive unit tests in direct_yaml_test.go.

2. Parallel Binary Chunk Reader (pkg/server/workbench/reader.go)

  • Enhanced binary .khi reader with optimized chunked parallel reading and pooled decompression buffers.
  • Decoupled metadata parsing from body struct decoding to allow streaming access.

3. Timeline Bitset & CEL Filter Optimization (pkg/server/workbench/)

  • Optimized bitset operations in bitset.go for compact timeline presence tracking.
  • Streamlined filter_log_cel.go and filter_timeline_cel.go to directly leverage interned structures and fast evaluation paths.

Verification

  • Backend Tests: make test-go passed (100%).
  • Frontend Tests: make test-web passed (762 tests).
  • Linters: make lint-go passed (0 issues).
  • Pre-commit: make pre-commit passed.
…tering

- Introduce DirectYAMLSerializer for fast serialization of interned structs in InternPool.
- Implement parallel chunk reader for binary .khi files.
- Optimize timeline CEL filtering with fast bitset decoding.
@kyasbal kyasbal added type:feature New feature or request type:refactor Code refactoring without behavioral change labels Aug 26, 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 significant performance optimizations to the workbench and file parsing systems. Key changes include the addition of a DirectYAMLSerializer to serialize interned structs directly to YAML, parallel chunk decompression and parsing in NewFromReader, and refactoring the filter pipeline (FilterContext and various CEL filters) to use roaring.Bitmap instead of Go maps for timeline and log IDs. Review feedback highlights critical thread-safety issues in the new DirectYAMLSerializer due to a shared buffer, potential panics in LogCELFilter from nil bitmaps in parallel workers, and missing defensive nil checks on several nullable protobuf fields. Additionally, a minor performance optimization was suggested to avoid unnecessary string allocations during YAML scalar safety checks.

Comment thread pkg/server/workbench/filter_log_cel.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
Comment thread pkg/model/khifile/v6/direct_yaml.go Resolved
@kyasbal
kyasbal merged commit ad062ac into GoogleCloudPlatform:main Aug 26, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-pukklrsvnpms branch August 26, 2026 01:29
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 type:refactor Code refactoring without behavioral change

1 participant