Skip to content

refactor(model): refactor TimelineBuilder revisions and events to lightweight types - #941

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

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

Conversation

@kyasbal

@kyasbal kyasbal commented Sep 2, 2026

Copy link
Copy Markdown
Member

Description

In khifilev6, TimelineBuilder previously accumulated events as []*pb.Event and revisions as []*pb.Revision. Each mutation converted data into heap-allocated Protobuf message structs and pointers (*uint32, *timestamppb.Timestamp, etc.), leading to excessive memory allocations, GC pressure, and coupling with LogAccumulator during timeline sorting (timeline_sorter.go).

This change refactors TimelineBuilder to accumulate lightweight value-type structs:

  1. Lightweight Value Types (pkg/model/khifile/v6/timeline_builder.go):

    • pendingEvent: Holds Timestamp time.Time (copied from the associated log) and LogID uint32 in a contiguous slice []pendingEvent.
    • pendingRevision: Holds primitive IDs (uint32), time.Time, and unexported pendingFieldAnnotation structs in a contiguous slice []pendingRevision.
    • Removed unused internPool field from TimelineBuilder.
  2. Decoupled Sorting via TimelineBuilder.FindOldestTime():

    • Finds the oldest timestamp among accumulated events and revisions directly under mutex lock.
    • Eliminates slices.Clone and queries to registry.GetLog in timeline_sorter.go.
  3. Deferred Protobuf Serialization:

    • TimelineBuilder.ToProto() delegates to modular helper functions (convertEventsToProtoLocked, convertRevisionsToProtoLocked, etc.).
    • Events are sorted chronologically by timestamp during Protobuf conversion.
  4. Updated Accumulation and Tests:

    • TimelineChangeSet.Flush: Converts staging events and revisions to lightweight pending types.
    • Updated unit tests in timeline_builder_test.go, timeline_serializer_test.go, timeline_sorter_test.go, and builder_test.go to match the new internal signatures and verified event sorting and oldest time detection.

Test Plan

  • Run make pre-commit
  • Run make lint-go and make test-go
  • Verified all unit and integration tests pass.
…htweight types

Store events and revisions in TimelineBuilder as lightweight value-type structs
(pendingEvent and pendingRevision) rather than full Protobuf heap pointers.
This reduces memory allocations, GC overhead, and decouples timeline sorting
from log accumulator queries.

- Define pendingEvent and pendingRevision in pkg/model/khifile/v6.
- Convert mutations in TimelineChangeSet.Flush to lightweight pending types.
- Add TimelineBuilder.FindOldestTime to eliminate slices.Clone and external log lookups during sorting.
- Defer Protobuf conversion and chronological sorting to ToProto during serialization.

@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 timeline accumulation logic to use lightweight, value-type structs (such as pendingEvent and pendingRevision) instead of protobuf heap pointers, reducing memory footprint and GC overhead. The review feedback suggests utilizing generated protobuf getters (GetTs() and GetId()) to simplify nil-checking logic and pre-allocating the annotations slice to optimize memory allocations.

Comment thread pkg/model/khifile/v6/changeset.go Resolved
Comment thread pkg/model/khifile/v6/changeset.go Resolved
Comment thread pkg/model/khifile/v6/changeset.go Resolved
@kyasbal
kyasbal merged commit 6fdfe4c into GoogleCloudPlatform:main Sep 2, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-vkrwytsmkrvk branch September 2, 2026 06:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

1 participant