Skip to content

perf: cut flush encoding allocations and page-write copies - #1133

Open
luoluoyuyu wants to merge 1 commit into
apache:developfrom
luoluoyuyu:perf/flush-encode-gc-dev
Open

luoluoyuyu wants to merge 1 commit into
apache:developfrom
luoluoyuyu:perf/flush-encode-gc-dev

Conversation

@luoluoyuyu

@luoluoyuyu luoluoyuyu commented Nov 19, 2026 •

Copy link
Copy Markdown
Member

Summary

Reduce allocations, boxing and buffer copies in the page/chunk write path, without changing chunk size, page size or produced bytes.

  • Replace the single doubling chunk PublicBAOS with ChunkPageBuffer, a sequential slab stream: a slab is filled before the next is taken, slab size is learned from the first page, and slabs are reused through size-class LIFO pools. There is no per-page block slack, no linear pool scan, no 2x chunk capacity and no page-count preallocation.
  • Keep page writer and page body buffers lazily sized and reused across chunks; retained capacity is bounded and reported.
  • Expose retained buffer bytes and use them for the global flush memory threshold. The logical page/chunk size estimates used for page splitting and chunk sizing are unchanged.
  • Treat expectedPageCount as a clamped layout hint only (it decides whether the first-page statistics can be written up front); it no longer sizes any buffer.
  • Remove the per-value lambda and Integer boxing from the dictionary encoder; encode int RLE values from a primitive buffer.
  • Write the first page statistics up front for multi-page chunks and compact the first page in place for single-page chunks.
  • Batch Gorilla/Chimp bit writes through a 64-bit accumulator and account for buffered bytes when sizing pages.
  • Remove unused page-buffer release and encoder-level retained-byte reporting paths, and simplify LongRleEncoder back to a single buffered implementation.
  • Keep ChunkPageBuffer package-private, fix the raw-page copy path for the segmented buffer, and deprecate the test-only snapshot page-buffer getter.

Results

Local IoTDB flush benchmark (deterministic allocation):

Metric Before After
End-to-end flush, 1.6M points 40.4 MiB, 54 ms 17.4 MiB, ~28 ms
End-to-end flush, 3.2M points ~84.5 MiB (estimated), ~89 ms 23.6 MiB, ~52 ms
Encode allocation (dense, 2 chunks) 37.9 MiB ~7.1 MiB
Encode peak heap 32 MiB 4 MiB

End-to-end throughput is about 57–62 M points/s, roughly 2x the baseline. In a controlled alternating A/B it matches the previous pre-grow version while allocating ~32% less, because the slab stream keeps page bytes exact and still avoids chunk-level doubling.

Compatibility

  • Chunk size, page size and output bytes are unchanged. Verified with SHA-256 comparisons against the base for TEXT, INT32, INT64, BOOLEAN, FLOAT/DOUBLE GORILLA and CHIMP, the page layout file, and reused vs. fresh chunk writers.
  • Full unit suite passes locally (1131 tests, 0 failures), including TsFileLastReaderTest with -Xmx512m and the new ChunkPageBufferTest.

Test plan

  • Full ./mvnw -P with-java -pl java/tsfile -am test (1131 tests)
  • ChunkPageBufferTest (exact page storage, writeTo, replace/truncate, reset/release, slab pool reuse)
  • Byte-level hash comparison against base for TEXT / all types / page layout / writer reuse
  • TsFileLastReaderTest with -Xmx512m
  • spotless:apply

@luoluoyuyu
luoluoyuyu force-pushed the perf/flush-encode-gc-dev branch 3 times, most recently from cc79087 to 57f31ff Compare November 19, 2026 08:19

@Caideyipi Caideyipi 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.

The implementation is otherwise consistent with the compatibility claims, and the PR head checks are green. I found one memory-accounting issue in the retained page scratch buffers; see the inline comment.

public long estimateMaxMemSize() {
return timeOut.size()
+ valueOut.size()
+ timeEncoder.getPendingByteCount()

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.

[P2] Include retained page scratch in memory estimates. PageBodyBuffer retains its uncompressed and compressed arrays for the lifetime of each page writer. After a page is written and page outputs are reset, these arrays remain reachable, but this estimate (and the chunk writers' estimateMaxSeriesMemSize()) has no capacity term for them. The memory-control path therefore undercounts retained heap as pages and series accumulate, which can defeat the configured memory limit on high-cardinality workloads. Please account for the retained capacities or release them at the memory-control boundary.


@Override
public void setExpectedPageCount(int expectedPageCount) {
this.expectedPageCount = expectedPageCount;

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.

[P3] Wire the expected-page count into the writer path. There are no call sites for setExpectedPageCount in this repository, so this field remains 0 for normal writers; growPageBufferAfterFirstPage() therefore always returns and the advertised single pre-sizing optimization never runs. Please set the estimate where the page count is known, or remove this unused path.

@luoluoyuyu
luoluoyuyu force-pushed the perf/flush-encode-gc-dev branch 4 times, most recently from c916a9c to 496e668 Compare November 20, 2026 06:55
Reduce allocations, boxing and buffer copies in the page/chunk write path
without changing chunk size, page size or produced bytes.

- store chunk pages in ChunkPageBuffer as a sequential slab stream: a slab
  is filled before the next one is taken, slab size is learned from the
  first page, and slabs are reused through size-class LIFO pools, so there
  is no per-page block slack, no linear pool scan and no 2x chunk capacity
- keep page writer and page body buffers lazily sized and reused across
  chunks of a writer
- expose retained buffer bytes and use them for the global flush memory
  threshold, while keeping the logical page/chunk size estimates unchanged
- treat expectedPageCount as a clamped layout hint only; it no longer
  sizes any buffer
- remove the per-value lambda and Integer boxing from the dictionary
  encoder; encode int RLE values from a primitive buffer
- write the first page statistics up front for multi-page chunks and
  compact the first page in place for single-page chunks
- batch Gorilla/Chimp bit writes through a 64-bit accumulator and account
  for buffered bytes when sizing pages
- keep ChunkPageBuffer package-private, fix the raw-page copy path for the
  segmented buffer, and deprecate the test-only snapshot page-buffer getter
- simplify LongRleEncoder back to a single buffered implementation and
  remove the unused page-buffer release and encoder-level retained-byte
  reporting paths
@luoluoyuyu
luoluoyuyu force-pushed the perf/flush-encode-gc-dev branch from 496e668 to 663b4fe Compare November 20, 2026 07:07
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.

2 participants