Skip to content

perf(workbench): pack CEL TrigramKey into uint64 and serialize StructYAMLs on-demand - #938

Merged
kyasbal merged 3 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-xoyppkomvqqp
Sep 2, 2026
Merged

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

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 2, 2026

Copy link
Copy Markdown
Member

Description

This PR introduces memory and CPU optimizations to Workbench index construction and CEL evaluation:

  1. Pack Trigram into uint64 TrigramKey (pkg/server/workbench/cel):

    • Replaced string keys in TrigramIndex with a compact TrigramKey (uint64).
    • Each Unicode code point (rune) is at most 21 bits (0x000000 to 0x10FFFF). By packing 3 lowercase runes into a single 64-bit integer ((r0 << 42) | (r1 << 21) | r2), we eliminate heap allocations during trigram extraction, map lookups, and index merging across parallel workers.
    • Updated binary serialization format to version 0x03, writing 8-byte uint64 values directly rather than length-prefixed strings.
    • Updated evalTrigramQuery to convert query terms to TrigramKey for candidate bitmap evaluation.
  2. Eliminate Pre-Serialized StructYAMLs in SearchIndex (pkg/server/workbench):

    • Previously, SearchIndex pre-serialized every interned struct body into a YAML string map (map[uint32]string), consuming significant heap memory (~2x the size of InternPool itself) and delaying workbench startup.
    • Replaced StructYAMLs with on-demand serialization via khifilev6model.DirectYAMLSerializer and wb.internPool when needed (e.g., in ReadStructYAMLs).
    • Removed BuildStructYAMLIndexWithProgress from workbench initialization, reducing indexing latency.
    • Preserved InternPool reference in SearchIndex to resolve structs and build indexes efficiently.

Benchmark & Memory Impact

Running BenchmarkMemory_InternPoolVsStructYAML (5,000 realistic unique Pod manifests):

  • Raw YAML Text Size: ~6.0 MB (avg 1,255 bytes/struct)
  • InternPool Heap Allocation: ~3.19 MB
  • StructYAMLs Heap Allocation: ~6.31 MB (~1.98x larger than InternPool)
  • Eliminating StructYAMLs cuts overall heap footprint by ~39% (~16.3 MB -> ~10.0 MB) during workbench initialization.

Test Plan

  • Run make pre-commit
  • Run make test-go and make test-web
  • All tests pass including pkg/server/workbench and pkg/server/workbench/cel

@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 refactors the search indexing and evaluation pipeline by removing the pre-serialized StructYAMLs cache from SearchIndex and LogEvaluator, opting instead to serialize structs to YAML on-demand. Additionally, it optimizes the TrigramIndex by packing 3-rune trigrams into a 64-bit integer (TrigramKey) instead of using strings, which reduces memory usage and simplifies binary serialization. Feedback is provided regarding the instantiation of DirectYAMLSerializer on every call to resolveStructYAML, which defeats its internal schema caching; using a sync.Pool to reuse serializer instances is recommended to avoid significant CPU overhead and memory allocations.

Comment thread pkg/server/workbench/cel/functions.go Resolved
Comment thread pkg/server/workbench/workbench_test.go Resolved
@kyasbal
kyasbal merged commit 7f78597 into GoogleCloudPlatform:main Sep 2, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-xoyppkomvqqp branch September 2, 2026 03:42
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant