Skip to content

perf(workbench,filter): optimize filter result payload with SparseBitset and IdBitset - #894

Merged
kyasbal merged 4 commits into
GoogleCloudPlatform:mainfrom
kyasbal:push-pnqorlvvxqrw
Aug 21, 2026
Merged

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

Conversation

@kyasbal

@kyasbal kyasbal commented Aug 21, 2026

Copy link
Copy Markdown
Member

Summary

This PR optimizes the Workbench timeline and log filtering pipeline by replacing full ID list transfers ([]uint32) and frontend Set<number> operations with compact SparseBitset serialization and a custom high-performance IdBitset data structure.

Background & Motivation

Previously, the backend FilterTimeline RPC returned explicit slices of all matching timeline_ids and log_ids. On large inspection datasets (e.g. hundreds of thousands of logs and timelines), transferring repetitive arrays over gRPC resulted in substantial serialization overhead and network payload bloat. Furthermore, the frontend converted these arrays into native JavaScript Set<number> instances, causing noticeable garbage collection overhead and memory churn during frequent filter queries.

Solution Overview

  1. Adaptive Sparse Bitset Protobuf (workbench.proto): Defined SparseBitset (indices, masks with 32-bit words) and FilterResultMode (INCLUDE / EXCLUDE).
  2. Dynamic Backend Bitset Encoding (pkg/server/workbench/bitset.go): Backend dynamically selects either INCLUDE mode (when matching count <= 50%) or EXCLUDE mode (when matching count > 50%) to minimize the wire size of the sparse bitset.
  3. High-Performance IdBitset on Frontend (web/.../id-bitset.ts): Replaced Set<number> in LogTimelineFilterContext with a custom 32-bit typed array bitset supporting bitwise intersection (and), bitwise difference/inversion (not), value iterator generation, and fast sparse bitset decoding.
  4. Filter Pipeline Integration: Updated TimelineView, BackendFilter, and CollapseTimelineFilter to operate directly on IdBitset.

Key Changes

1. Protobuf Definitions (proto/api/v1/workbench.proto)

  • Added SparseBitset message representing non-zero 32-bit bitmask blocks.
  • Added FilterResultMode enum (INCLUDE, EXCLUDE).
  • Updated FilterResult to carry timeline_mode, timeline_bitset, log_mode, and log_bitset.
  • Regenerated Go and TypeScript protobuf stubs.

2. Backend (pkg/server/workbench/)

  • Implemented BuildSparseBitset and EncodeFilterResultBitset in bitset.go with full test coverage (bitset_test.go).
  • Updated FilterTimeline in filter.go to encode filter results into SparseBitset based on total timeline/log counts.
  • Updated pipeline_test.go to test sparse bitset decoding.

3. Frontend (web/src/app/)

  • Created IdBitset class (store/domain/filter/id-bitset.ts) and comprehensive unit tests (id-bitset.spec.ts).
  • Updated LogTimelineFilterContext in store/domain/filter/types.ts to use IdBitset instead of Set<number>.
  • Updated WorkbenchClientService to decode SparseBitset into IdBitset.
  • Updated BackendFilter, CollapseTimelineFilter, and TimelineView to compute filter intersections using bitwise operations.

Included Commits

  1. feat(proto): update FilterResult with SparseBitset and FilterResultMode
  2. feat(backend): implement SparseBitset builder and update FilterTimeline in workbench
  3. feat(frontend): implement IdBitset and integrate SparseBitset with filter pipeline

Verification

  • Backend Tests: make test-go (All unit tests passed)
  • Frontend Tests: make test-web (All 736 unit tests passed)
  • Linters: make lint-go && make lint-web (Passed with 0 issues)
  • Pre-commit Formatting: make pre-commit (Formatted cleanly)
@kyasbal kyasbal added the type:feature New feature or request label Aug 21, 2026
@kyasbal

kyasbal commented Aug 21, 2026

Copy link
Copy Markdown
Member Author

Benchmark & Capacity Reference Data

To quantify the performance and memory improvements of this change, benchmarks were conducted across wire payload serialization, frontend decoding, and in-memory filter operations for large datasets (1,000,000 items).


1. Wire Payload Size Comparison (1,000,000 Logs)

Comparison between flat ID arrays ([]uint32), 64-bit sparse bitset (fixed64), and 32-bit sparse bitset with adaptive mode (fixed32):

Scenario (1M logs) Legacy Flat IDs ([]uint32) Sparse Bitset (fixed64) Sparse Bitset (fixed32) Payload Reduction (vs Legacy)
0% match (None) 2 B 2 B 2 B 0% (Equal)
0.1% match (Sparse, 1k items) 4.90 KB 9.63 KB 6.37 KB ~33.8% smaller than fixed64
1% match (10k items) 57.06 KB 71.93 KB 53.97 KB ~25.0% smaller than fixed64
10% match (100k items) 571.74 KB 152.29 KB 190.67 KB 66.6% reduction
50% match (Worst case) 2.85 MB 152.48 KB 197.51 KB 93.1% reduction
90% match 5.14 MB 152.25 KB 190.41 KB 96.3% reduction (via EXCLUDE)
99% match (Dense) 5.65 MB 72.49 KB 54.32 KB 99.0% reduction (via EXCLUDE)
100% match (All) 5.71 MB 2 B 2 B 99.99% reduction (via EXCLUDE)

Key takeaway:

  1. fixed32 with adaptive INCLUDE / EXCLUDE mode yields 25%–33% smaller payloads for sparse and dense filtering scenarios compared to fixed64 due to finer block granularity.
  2. Even in the theoretical worst-case scenario (50% uniform match), payload size is capped at ~197 KB (a 93% reduction over flat []uint32).

2. Frontend Protobuf Deserialization Speed (1,000,000 Items)

Benchmark comparing deserialization and conversion into frontend bitset structures:

Operation (1M items) fixed64 (bigint[]) fixed32 (number[]) Speedup with fixed32
INCLUDE Mode Decode (50% match) 1.354 ms 0.150 ms 9.0x faster
EXCLUDE Mode Decode (50% match) 2.877 ms 1.346 ms 2.1x faster
Sparse Match Decode (1% match) 0.0357 ms 0.0230 ms 1.5x faster

Key takeaway:
@bufbuild/protobuf parses fixed32 directly into native JavaScript numbers. This completely bypasses BigInt splitting and 64-bit shifting overhead in V8, allowing direct typed array copy (words[idx] = mask) in 0.15 ms for 1M items.


3. Frontend In-Memory & Performance Benchmark (Set<number> vs IdBitset)

Measured on Node.js / V8 for 1,000,000 log items:

Metric (1,000,000 items) Set<number> IdBitset (Uint32Array) Improvement (IdBitset vs Set)
Heap Memory Footprint 38.75 MB 6.20 MB 84.0% less memory (raw buffer is only 125 KB)
Construction Time (500k items) 55.61 ms 2.30 ms 24.2x faster
Lookup .has(id) (1M lookups) 64.80 ms 6.38 ms 10.2x faster
Clone Time (.clone()) 6.72 ms 0.053 ms 126.5x faster
Batch Deletion (10k items) 45.92 ms 0.41 ms 110.7x faster

@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 replaces the array-based timeline and log ID filtering with a highly optimized, sparse bitset representation (SparseBitset) and a new FilterResultMode enum (supporting INCLUDE and EXCLUDE modes) to minimize payload size. On the backend, Go helper functions are introduced to build and encode these bitsets, while the frontend implements a high-performance IdBitset using Uint32Array to perform sub-millisecond bitwise operations. The review feedback recommends adding defensive checks in the Go bitset encoding to prevent potential panics, using modern slices.Sort instead of reflection-based sorting, and optimizing the frontend IdBitset decoding by utilizing a popcount helper and directly filling the typed array in fromSequential to eliminate loop overhead.

Comment thread pkg/server/workbench/bitset.go Resolved
Comment thread pkg/server/workbench/bitset.go Resolved
Comment thread pkg/server/workbench/bitset.go Resolved
@kyasbal
kyasbal merged commit 6d543af into GoogleCloudPlatform:main Aug 21, 2026
16 checks passed
@kyasbal
kyasbal deleted the push-pnqorlvvxqrw branch August 21, 2026 07:26
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

type:feature New feature or request

1 participant