Skip to content

[perf] Pack Mooncake non-tensor PUTs directly into pooled buffers - #247

Open
chenjr97 wants to merge 1 commit into
Ascend:mainfrom
chenjr97:chenjr/mooncake-direct-pack-put
Open

chenjr97 wants to merge 1 commit into
Ascend:mainfrom
chenjr97:chenjr/mooncake-direct-pack-put

Conversation

@chenjr97

Copy link
Copy Markdown
Contributor

Overview

Extends Mooncake's pre-registered BufferPool optimization (#216) to non-tensor RDMA PUTs (MooncakeStoreClient._put_bytes_thread_worker) with two changes:

  1. Direct pack into pre-registered BufferPool memory (behind use_put_buffer_pool, default false), replacing per-batch allocation and register_buffer/unregister_buffer with zero extra staging copy.
  2. Remove the nested packing thread pool (MAX_SERIAL_WORKER_THREADS) so each outer worker packs its sub-batch inline.

Across the 22 reported payload/batch shapes on the launcher with one HCA, the full change delivers 1.185×–4.951× end-to-end PUT speedup over main, measured at P50.

Detail

1. Direct pack into pre-registered BufferPool

  • Before: use_put_buffer_pool only covered CPU-backed tensors. Non-tensor PUTs allocated a fresh contiguous uint8 buffer per sub-batch (allocate_empty_tensors), packed into it, and ran register_buffer → batch_upsert_from → unregister_buffer.
  • After: When use_put_buffer_pool=True and the aligned sub-batch fits within _pool_share_bytes, _put_bytes_thread_worker leases a pre-registered slice via _acquire_buffer, packs items directly into memoryview(lease.buffer) using serial_utils.pack_into, and calls _batch_upsert_with_retry from [lease.ptr + off] (0 dynamic registrations).
  • Fallback & safety: Oversized or unpooled batches fall back to the single-region registration path. memoryviews in pack_into and _put_bytes_thread_worker are with-scoped, batch_items are dropped before upsert so source views are not kept alive across errors, and the lease is always released on success or exception.
  • Direct packing (A → B) reduced P50 on all 22 shapes (1.059×–1.880× speedup), including 1.721×–1.880× on the four 4–8 GiB batches.

2. Remove inner packing thread pool (MAX_SERIAL_WORKER_THREADS)

  • Before: Each outer worker (up to MAX_BATCH_WORKER_THREADS = 4, 400 keys/sub-batch) created a nested ThreadPoolExecutor(4) inside batch_encode_into, spawning up to 16 Python threads contending on the GIL plus per-sub-batch executor and task overhead.
  • After: Removed MAX_SERIAL_WORKER_THREADS and inlined sequential pack_into on each outer batch worker (for both pooled and fallback paths), keeping outer sub-batch parallelism intact.
  • Inline packing (B → C) delivered a P50 speedup of 1.437×–1.514× at 128–400 keys and 2.122×–3.047× at 800–1,600 keys for 1 and 16 KiB payloads. For 4 MiB–1 GiB payloads, B/C ranged from 0.894× to 1.030×, with an overall range of 0.894× to 3.047× across the 22 shapes.

Eval

Setup

  • Variants:
    • A (main): use_put_buffer_pool=False, nested inner ThreadPoolExecutor(4), per-sub-batch register/unregister.
    • B (Pool only): use_put_buffer_pool=True, direct pack into BufferPool with inner ThreadPoolExecutor(4).
    • C (Full): use_put_buffer_pool=True, direct pack into BufferPool inline on outer workers (no inner thread pool).
  • Environment: Python 3.12.8, Mooncake 0.3.12.post1, 1 × 200 Gb/s RDMA HCA (MC_STORE_MEMCPY=0, use_gdr=False), colocated client/controller/storage; local_buffer_size=80 GiB (10 GiB/worker share), global_segment_size=32 GiB, BATCH_SIZE_LIMIT=400, MAX_BATCH_WORKER_THREADS=4.
  • A single client on the launcher issued one await client.async_put(..., metadata=None) per batch. Each PUT used fresh random bytes. For each of the 22 reported shapes, each variant ran 5 warmups followed by 10 measured PUTs. Payload generation, full readback and partition cleanup were outside the timed call.

Results on the launcher (1 HCA)

The table covers 22 shapes with at most 1,600 keys per PUT (up to four 400-key sub-batches). Each latency cell reports P50 / max in milliseconds from the same 10 measured PUTs. Speedups use unrounded P50 latencies. A/B measures direct pool packing, B/C measures the effect of removing the inner pool, and A/C measures the full change. A ratio above 1 means the denominator variant was faster.

Payload / key Keys / PUT A P50 / max (ms) B P50 / max (ms) C P50 / max (ms) A/B B/C A/C
1 KiB 128 8.38 / 9.09 7.09 / 7.70 4.71 / 5.64 1.182× 1.504× 1.778×
1 KiB 256 13.45 / 14.32 10.37 / 10.81 7.22 / 33.97 1.297× 1.437× 1.864×
1 KiB 400 18.93 / 19.54 14.38 / 15.08 9.73 / 10.02 1.316× 1.478× 1.946×
1 KiB 800 38.05 / 122.65 30.93 / 123.89 14.57 / 17.26 1.230× 2.122× 2.612×
1 KiB 1600 119.47 / 225.44 73.53 / 188.13 24.13 / 28.07 1.625× 3.047× 4.951×
16 KiB 128 9.08 / 23.98 7.44 / 7.75 5.06 / 5.22 1.219× 1.472× 1.795×
16 KiB 256 14.52 / 15.90 11.53 / 11.98 7.74 / 8.03 1.259× 1.490× 1.877×
16 KiB 400 21.41 / 24.49 16.53 / 134.32 10.92 / 11.66 1.295× 1.514× 1.960×
16 KiB 800 40.06 / 153.04 37.83 / 173.55 17.22 / 17.85 1.059× 2.197× 2.326×
16 KiB 1600 123.81 / 237.27 83.02 / 205.84 28.47 / 29.43 1.491× 2.916× 4.348×
256 KiB 128 34.99 / 36.26 21.87 / 24.08 16.89 / 21.47 1.599× 1.295× 2.071×
256 KiB 256 76.08 / 86.41 50.91 / 56.68 41.42 / 48.02 1.494× 1.229× 1.837×
256 KiB 400 122.74 / 241.39 80.55 / 116.51 68.39 / 72.99 1.524× 1.178× 1.795×
256 KiB 800 246.31 / 333.55 149.53 / 203.43 137.94 / 142.59 1.647× 1.084× 1.786×
256 KiB 1600 468.03 / 565.07 337.99 / 458.86 267.74 / 290.00 1.385× 1.262× 1.748×
4 MiB 128 406.27 / 546.33 306.46 / 362.20 342.89 / 422.59 1.326× 0.894× 1.185×
4 MiB 256 1,006.27 / 1,161.90 695.94 / 784.91 695.03 / 744.30 1.446× 1.001× 1.448×
4 MiB 400 1,362.09 / 1,982.37 1,067.97 / 1,149.26 1,060.83 / 1,101.94 1.275× 1.007× 1.284×
32 MiB 256 10,207.59 / 10,570.64 5,692.16 / 6,186.65 5,857.12 / 6,119.86 1.793× 0.972× 1.743×
64 MiB 128 9,835.55 / 10,002.09 5,716.40 / 6,138.40 5,549.39 / 6,103.30 1.721× 1.030× 1.772×
256 MiB 32 10,653.04 / 10,952.53 5,665.45 / 5,914.33 5,695.63 / 5,986.44 1.880× 0.995× 1.870×
1 GiB 4 5,368.41 / 5,533.49 2,905.09 / 3,000.24 2,964.64 / 3,015.48 1.848× 0.980× 1.811×

Analysis

  • Direct packing (A → B) yields a consistent speedup across all 22 shapes (1.059×–1.880×, and 1.721×–1.880× on 4–8 GiB batches), since packing directly into pre-registered BufferPool memory eliminates per-batch register_buffer/unregister_buffer overhead without any additional cost.
  • Removing the inner thread pool (B → C) shows that inner multithreading brings no clear benefit on large payloads (4 MiB–1 GiB: 0.894×–1.030×, with overlapping 10-run latency ranges on 32 MiB × 256 and 1 GiB × 4), while severely aggravating GIL contention on non-tensor small packets—removing it speeds up all 1–256 KiB shapes (1.084×–1.295× at 256 KiB, 1.437×–1.514× at 1–16 KiB ≤400 keys, and 2.122×–3.047× at 800–1,600 keys).

Signed-off-by: chenjr97 <chenjr97@mail2.sysu.edu.cn>
@chatgpt-codex-connector

chatgpt-codex-connector Bot commented Nov 30, 2026 •

Copy link
Copy Markdown
Codex Review Summary: ✅ Completed

This comment shows the latest Codex review activity on this pull request.

Review Status Commit Review trigger
📝 Code Review ✅ Completed 2026-11-30T12:54:58.473057Z 441f935 PR opened
ℹ️ About Codex in GitHub

Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you

  • Open a pull request for review
  • Mark a draft as ready
  • Comment "@codex review" or "@codex security review".

Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings.

@0oshowero0 0oshowero0 left a comment

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the clean optimization and thorough benchmarks! Packing non-tensor payloads directly into pre-registered BufferPool memory avoids both the extra staging copy and dynamic registration, and removing the nested packing thread pool clearly eliminates GIL contention and tail latency. The tests (verifying zero-copy destinations via pack_into and view cleanup on failure) are also very solid.

A few minor suggestions and one design question:

  1. Update use_put_buffer_pool comments/docs to reflect non-tensor coverage

    • transfer_queue/config.yaml (L71) and scripts/performance_test/perftest_config.yaml (L55) still state that use_put_buffer_pool only applies to CPU-backed tensor PUTs:
      # Reuse the pre-registered buffer pool for CPU-backed tensor PUTs over RDMA.
    • Since it now covers both CPU-backed tensor and non-tensor PUTs (and the Mooncake #4491 caveat applies to both), please update those config comments, along with the fallback warning at mooncake_client.py:244 and the test module docstring at tests/test_mooncake_buffer_pool.py:15.
  2. Empty-batch guard and scoping _aligned_offsets (mooncake_client.py:451-482)

    • Although public put() never dispatches an empty non-tensor chunk, calling _put_bytes_thread_worker([], []) directly leaves items unbound and raises UnboundLocalError at del batch_items, items (and calls _acquire_buffer(0) on the pooled path), whereas _put_tensors_thread_worker([], []) returns cleanly. Adding an early if not batch_values: return [] prevents that.
    • Moving _aligned_offsets(batch_sizes) inside if self.use_put_buffer_pool and self._buffer_pool is not None: also keeps the default (use_put_buffer_pool=False) and TCP paths from doing unnecessary offset calculation.
  3. Factor out the duplicated lease.release() error-handling block (mooncake_client.py)

    • The cause-preserving try / except Exception: try: lease.release() ... raise; lease.release() boilerplate now appears in 3 places (_write_via_buffer_pool, _put_bytes_thread_worker, and _read_via_buffer_pool).
    • Since we're touching this path anyway, it would be nice to extract a small internal context manager (e.g., _leased_buffer(lease, role)) so this invariant lives in one place.
  4. Minor nits (non-blocking)

    • Tests: Consider adding a non-tensor value to TestPutBufferPoolGuards.test_opt_out_registers_put_and_still_pools_get to guard the use_put_buffer_pool=False contract for non-tensors, and asserting store.unregistered == [ptrs[0]] (plus a get() roundtrip) in test_put_bytes_over_one_share_registers_the_whole_batch_once.
    • Source views on pack_into failure (serial_utils.py:583): del batch_items, items nicely ensures source views are released on transport errors. If pack_into itself raises (e.g., target buffer too small or struct.error on a >4 GiB item), target_mv is released by the with block, but item_mvs remains referenced by the traceback frame. Releasing item_mvs in a try/finally would make cleanup symmetric.
  5. Design choice (up to you / non-blocking): whole-batch fallback above _pool_share_bytes

    • With the default local_buffer_size=1 GiB, _pool_share_bytes is 128 MiB. Unlike _put_tensors_thread_worker (which uses split_by_bytes), any 400-key non-tensor sub-batch exceeding 128 MiB (e.g., 400 × 350 KiB, or one 130 MiB value mixed with small values) silently falls back to register_buffer for the entire batch without hitting _acquire_buffer (so nothing is logged).
    • Either choice makes sense—leaving it up to you:
      • Keep whole-batch all-or-nothing (simplest code, preserves key order, single RPC, and fallback is still a single registration); in that case, a logger.debug when skipping the pool due to total > self._pool_share_bytes would help observability.
      • Or switch to in-order greedy chunking (no reordering, so duplicate keys stay safe) if you'd like >128 MiB batches to still use the pool under the default 1 GiB local_buffer_size.

@0oshowero0

Copy link
Copy Markdown
Collaborator

#252 I just support the last piece of buffer pool for non-tensor get. We plan to release the next version in next week, hope we can meet the ddl~

@chenjr97

chenjr97 commented Dec 3, 2026

Copy link
Copy Markdown
Contributor Author

Thanks for the review! We've finished the implementation. We're doing some final cleanup and will publish the update shortly.

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