Skip to content

perf(cpp): reduce TsFile close overhead - #962

Open
ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:cpp-close-optimizations
Open

ColinLeeo wants to merge 1 commit into
apache:developfrom
ColinLeeo:cpp-close-optimizations

Conversation

@ColinLeeo

Copy link
Copy Markdown
Contributor

What is changed

This PR optimizes the C++ TsFile close/index-writing path for workloads with many devices and repeated flushes.

  • Increase TsFileIOWriter write-stream pages from 512 B to 16 KiB to reduce small metadata writes.
  • Rework TSMIterator to avoid mutating source ChunkGroupMeta lists, avoid unnecessary temporary list copies, skip sorting already ordered chunk metadata, and preserve recovery write ordering.
  • Build table-to-device metadata maps directly during file-index generation instead of constructing and repartitioning a global device map.
  • Use lower_bound with hint insertion for device index nodes.
  • Add regression coverage for the stream page size, source metadata ordering, and per-ChunkGroup offset ordering.

Benchmark

Workload: 10,000 devices × 100 batches, 1,000,000 physical ChunkGroups, 2,000,000 Chunks, 1,000,000 rows, C++ table writer, arm64 macOS.

Metric Before After
close() median 594.390 ms 298.264 ms
Total lifecycle median 10,127.920 ms 9,819.602 ms

The generated file size remains unchanged at 159,940,628 bytes. The optimization targets close/index finalization; it does not change the flush policy or file format.

Verification

  • cmake -S cpp -B cpp/target/build -DCMAKE_BUILD_TYPE=Release -DBUILD_TEST=ON
  • cmake --build cpp/target/build --target TsFile_Test -j4
  • cpp/target/build/test/lib/TsFile_Test
    • 946 passed
    • 3 skipped
    • 10 disabled
  • clang-format --dry-run --Werror on all changed C++ files
  • git diff --check

The branch is rebased onto the latest origin/develop.

@ColinLeeo
ColinLeeo requested review from hongzhi-gao and a balanced review from Copilot September 17, 2026 08:04

Copilot AI 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.

🟡 Changes recommended

Empty chunk groups can create invalid iterator state and undefined behavior during file close.

Get a fresh assessment by requesting another Copilot review.

Pull request overview

Optimizes C++ TsFile close and metadata-index generation for high-device-count workloads.

Changes:

  • Enlarges write-stream pages to 16 KiB.
  • Reduces metadata copying and sorting overhead.
  • Builds per-table device indexes directly and adds regression tests.
File summaries
File Description
cpp/src/common/tsfile_common.cc Reworks chunk metadata grouping and iteration.
cpp/src/common/tsfile_common.h Simplifies iterator state and map aliases.
cpp/src/file/tsfile_io_writer.cc Optimizes table/device index construction.
cpp/src/file/tsfile_io_writer.h Increases write-stream page size.
cpp/test/common/tsfile_common_test.cc Adds ordering and page-size regression tests.
Review details
  • Files reviewed: 5/5 changed files
  • Comments generated: 1
  • Review effort level: Balanced

💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +86 to 91
if (!device_exists) {
tsm_chunk_meta_info_.emplace_hint(device_pos,
chunk_group_meta->device_id_,
std::move(chunk_group_map));
continue;
}

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.

Please skip empty chunk groups here and add a regression test for this case.

@hongzhi-gao

Copy link
Copy Markdown
Contributor

LGTM

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants