Repository navigation
perf(khifile): optimize in-memory model via flat structure-of-arrays representation - #950
Conversation
Refactor InternPool and LogAccumulator to flush compressed chunks directly to the destination Writer as items are added and the batch size threshold is reached, instead of retaining all logs and intern items in memory until the end of the inspection. - Pass *Writer to InternPool and LogAccumulator and enable progressive chunk compression and writing. - Remove full in-memory log list and GetLog from LogAccumulator to drastically reduce memory usage during inspection. - Add thread-safe Mutex to Writer and introduce MustNewTestWriter for testing. - Update Builder lifecycle and inspection runner to initialize Writer at the start of inspection. - Update TimelineChangeSet.Flush to accept LogAccumulator directly. - Add streaming flush unit tests for InternPool and LogAccumulator. - Relax strict non-decreasing timestamp check in web LogStore to support multi-chunk streaming loads.
There was a problem hiding this comment.
Code Review
This pull request refactors the KHI v6 file generation to stream chunks directly to a destination writer during ingestion, optimizing memory usage and heap allocations through a new FlatStructStore and streaming flushes in InternPool and LogAccumulator. Additionally, the frontend LogStore was updated to accept out-of-order timestamps since the backend now handles chronological sorting during flushes. The reviewer feedback suggests robust error handling for background streaming flushes by adding an error field to InternPool and using a deferred function in Builder.Build to guarantee the writer is closed, preventing potential resource leaks.
…nd handle writer errors
E2E Performance Benchmark Update (Post-Review Feedback)Following the review feedback to store unflushed strings and field path sets as compact Go value types (
Key Highlights
|
Overview
Analysis of heap profiles during the log inspection (collection & generation) phase showed that ~88% of live heap allocations were concentrated in
ToInternedStruct(individual*pb.InternedValueallocations) andLogAccumulator(pointer graph of staged logs).This PR transitions the in-memory staging models to flat, pointer-free Structure-of-Arrays (SoA) representations while preserving on-disk
.khiformat and streaming chunk compatibility.Key Changes
1. Direct Node Encoding into
FlatStructStoreStoreFromNodestoFlatStructStore, encodingstructured.Nodescalars and sequences directly into contiguous primitive value slices (kinds,scalarInts,scalarFloats,scalarStrings,listOffsets).ensureNestedMapsInternedto pre-intern nested maps intoInternPoolbefore acquiring store lock, preventing lock contention and deadlocks.2. Integrate
FlatStructStoreintoInternPoolandToInternedStructidToStruct []*pb.InternedStructandunflushedStructs []*pb.InternedStructinInternPoolwithflatStructs *FlatStructStoreandunflushedStructIDs []uint32.ToInternedStructnow directly callsStoreFromNodeson cache miss, eliminating temporary*pb.InternedValueallocations.ToInternedValue,scalarToInternedValue, etc.) adhering to the zero-backward-compatibility policy.*pb.InternedStructmessages are only constructed during chunk protobuf serialization and compressed immediately.3. Store Staged Logs in Flat Primitive Slices in
LogAccumulatorcurrentBatch []*pb.LoginLogAccumulatorwith flat SoA slices (batchIDs,batchTimestamps,batchLogTypeIDs,batchSeverityIDs,batchSummaryIDs,batchBodyStructIDs).AddLogconverts metadata and appends scalar values directly into primitive arrays without allocating heap*pb.Logor*timestamppb.Timestampobjects.flushLockedstably sorts permutation indices based on timestamps, writes the chunk, and resets slice lengths ([:0]) to reuse allocated backing capacity across batches.E2E Benchmark: Memory Consumption Results
Measured via
/usr/bin/time -vand Go pprof memory profiles (--memprofile) on a 4-hour production cluster log query (p0-gke-basic-1, 10 features, 42,478+ logs):.khiSizeTop Allocations Breakdown (pprof diff)
scalarToInternedValue: -320.52 MB (-5,988,669 objects) (100% eliminated from live heap)ToInternedValue: -384.02 MB cum (-7,658,528 objects cum) (100% eliminated)LogAccumulator.AddLog: -315.67 MB cum (-7,431,052 objects cum) (-48.4% live memory)FlatStructStore: Only +104.61 MB of primitive array capacity was needed to hold all structured data, yielding a net reduction of ~545 MB in struct storage.Verification
make pre-commit: PASSmake lint-go && make lint-web: PASS (0 issues)make test-go && make test-web: PASS (all Go tests and 1088 web tests pass)