Skip to content

perf(core): optimize structured Node fast lookup and flattenNode allocations - #933

Merged
kyasbal merged 6 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-kvwxsxzroksn
Sep 1, 2026
Merged

kyasbal merged 6 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-kvwxsxzroksn

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 1, 2026

Copy link
Copy Markdown
Member

Description

This PR introduces Phase 2 memory and CPU optimizations for structured log parsing and KHI file serialization:

  1. Polymorphic GetChildByKey on structured.Node:

    • Added GetChildByKey(key string) (Node, bool) method to the Node interface.
    • Implemented fast-path key lookup in LazyJSONNode using direct byte scanning without parsing irrelevant JSON keys or allocating child node closures.
    • Implemented GetChildByKey in StandardMapNode, orderedMapNode (transparently delegating to inner node), fieldFilterNode, and other Node implementations.
    • Refactored NodeReader.GetNode into a completely polymorphic traversal loop without concrete type assertions (*orderedMapNode, *StandardMapNode, *LazyJSONNode), preserving clean abstraction.
    • Benchmark Result: Total allocations reduced from 4.95 GB to 3.77 GB (-1.18 GB / -23.8%).
  2. Optimize flattenNode string concatenation & slice reuse:

    • Replaced repeated string concatenations (prefix + "." + key) with a reusable byte slice buffer ([]byte).
    • Pre-sized initial capacity for keys and values slice buffers.
    • Benchmark Result: flattenNode cumulative allocations reduced from 481.59 MB to 356.07 MB (-26.1%).

Test Plan

  • Run make test-go and make lint-go
  • Unit tests added for LazyJSONNode.GetChildByKey and NodeReader path navigation with various node types.

@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 new GetChildByKey method to the Node interface and its implementations, optimizing child node lookups and simplifying NodeReader.GetNode. It also optimizes ToInternedStruct and flattenNode by using a reusable byte buffer (keyBuf) to avoid string concatenation allocations. The feedback suggests pre-allocating keyBuf with extra capacity when a prefix is present to prevent immediate reallocation, and adding a test case to verify that GetChildByKey correctly handles JSON keys containing escape sequences.

Comment thread pkg/model/khifile/v6/struct.go Resolved
Comment thread pkg/common/structured/lazyjson_test.go Resolved
Comment thread pkg/common/structured/lazyjson.go Resolved
@kyasbal kyasbal added the area:backend-core Core backend frameworks, DAG task runner, and server API label Sep 1, 2026
@kyasbal kyasbal closed this Sep 1, 2026
@kyasbal kyasbal reopened this Sep 1, 2026
@kyasbal
kyasbal merged commit 5d55918 into GoogleCloudPlatform:main Sep 1, 2026
30 checks passed
@kyasbal
kyasbal deleted the push-kvwxsxzroksn branch September 1, 2026 05:56
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