Skip to content

perf(khifile): optimize InternPool memory retention, ID resolutions, and ToInternedStruct allocations - #935

Merged
kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-ukollznkqynl
Sep 2, 2026
Merged

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

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 2, 2026

Copy link
Copy Markdown
Member

Description

This PR introduces memory and allocation optimizations to khifilev6 interning and struct conversion:

  1. Prevent pinning underlying byte buffers in InternString:

    • Strings passed to InternPool.InternString (e.g., extracted from large protojson byte buffers) could keep entire input buffers alive in memory.
    • Cloned interned strings via strings.Clone before storing in the pool to allow transient buffers to be garbage-collected.
  2. Replace sync.Map with mutex-protected slices for ID resolutions in InternPool:

    • Replaced sync.Map for idToStr, idToFieldSet, and idToStruct with dense slices ([]string, [][]uint32, []*pb.InternedStruct) guarded by sync.RWMutex.
    • Monotonically increasing sequential IDs allow direct O(1) index lookups without map hashing overhead or interface boxing allocations.
  3. Fast-path binary key computation in ToInternedStruct:

    • Avoid allocating []*pb.InternedValue and proto messages when a struct is already interned in InternPool.
    • Implemented structKeyFromNodes to compute the cache key directly from structured.Node values using a reusable byte buffer.
    • Pre-allocated singleton proto messages for bool (true/false) and null interned values.

Test Plan

  • Run make pre-commit
  • Run make lint-go and make test-go
  • Unit tests pass including pkg/model/khifile/v6

@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 optimizes the InternPool by replacing several sync.Map lookups with slice-based index resolution protected by read-write mutexes. It also introduces a fast-path key computation (structKeyFromNodes) to avoid redundant protobuf allocations for already interned structs, and reuses static interned values for null and boolean types. The review feedback suggests further optimizing the slice-growing functions (storeString, storeFieldSet, and storeStruct) to avoid temporary slice allocations during growth, and simplifying the locking/unlocking flow in resolveStringFromID to reduce lock contention and nested conditions.

Comment thread pkg/model/khifile/v6/intern.go Resolved
Comment thread pkg/model/khifile/v6/intern.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 Sep 2, 2026
@kyasbal
kyasbal merged commit d403781 into GoogleCloudPlatform:main Sep 2, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-ukollznkqynl branch September 2, 2026 01:02
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