Skip to content

[Performance][Wheel] Avoid PIL RGB pixel copies - #3413

Draft
zxpdemonio wants to merge 2 commits into
kvcache-ai:mainfrom
openanolis:cruz/pil-rgb-arrow-fast-path
Draft

zxpdemonio wants to merge 2 commits into
kvcache-ai:mainfrom
openanolis:cruz/pil-rgb-arrow-fast-path

Conversation

@zxpdemonio

@zxpdemonio zxpdemonio commented Aug 12, 2026 •

Copy link
Copy Markdown
Collaborator

Motivation

The structured-object PIL codec introduced by #3412 uses Image.tobytes() for RGB pixels. That creates a full packed-RGB allocation before Mooncake copies the payload into BufferPool staging. GET then reconstructs another full pixel buffer. For rollout dictionaries containing many decoded images, those CPU copies dominate transfer latency.

This PR is stacked on #3412 and contains only the follow-up performance change. It should be rebased onto main after #3412 merges.

Description

  • Read Pillow's immutable RGBX storage through the Arrow C Data Interface without an intermediate tobytes() allocation.
  • Copy that view directly into the existing BufferPool payload path.
  • Reconstruct full-GET images with Image.fromarrow, keeping the BufferPool lease alive until the last decoded image is released.
  • Keep the packed-RGB codec as a compatibility fallback when Pillow or the requested layout does not support Arrow.
  • Validate the Arrow schema, array layout, dimensions, and ownership transitions before exposing a view.

Three implementations were measured with the same wheel, master, client, Kodak24 RGB inputs, 64 MiB chunks, one warmup, and seven cross-node RDMA trials. Values are median PUT+GET latency; lower is better.

Logical payload Packed RGB with tobytes() RGBX Arrow view RGBX fused to packed RGB in BufferPool
128 MiB 186.8 ms 63.9 ms 164.0 ms
512 MiB 861.6 ms 177.4 ms 538.4 ms

The fused packed path reduces wire bytes but still requires a full reconstruction copy on GET. Direct RGBX sends 33% more pixel bytes, yet wins at every measured size because it removes the expensive copies on both sides. It also remained fastest for 1, 8, and 24-image payloads, so the implementation does not add a size threshold.

The final reviewed wheel was rebuilt and retested independently:

Logical payload Layout PUT GET PUT+GET Speedup
128 MiB Packed RGB 76.1 ms 112.1 ms 188.1 ms 1.00x
128 MiB RGBX Arrow 43.2 ms 20.2 ms 63.2 ms 2.98x
512 MiB Packed RGB 397.9 ms 431.0 ms 828.8 ms 1.00x
512 MiB RGBX Arrow 105.8 ms 71.9 ms 178.9 ms 4.63x

Module

  • Transfer Engine (mooncake-transfer-engine)
  • Mooncake Store (mooncake-store)
  • Mooncake EP (mooncake-ep)
  • Mooncake PG (mooncake-pg)
  • Integration (mooncake-integration)
  • P2P Store (mooncake-p2p-store)
  • Python Wheel (mooncake-wheel)
  • Common (mooncake-common)
  • Mooncake RL (mooncake-rl)
  • CI/CD
  • Docs
  • Other

Type of Change

  • Bug fix
  • New feature
  • Refactor
  • Breaking change
  • Documentation update
  • Performance improvement
  • Other

How Has This Been Tested?

Test commands:

python -m pytest mooncake-wheel/tests/test_structured_object_store.py
python -m py_compile mooncake-wheel/mooncake/structured_object_store.py mooncake-wheel/tests/test_structured_object_store.py
clang-format-20 --dry-run --Werror mooncake-wheel/mooncake/_fast_copy.c
SKIP=ruff,ruff-format pre-commit run --files \
  mooncake-wheel/mooncake/_fast_copy.c \
  mooncake-wheel/mooncake/structured_object_store.py \
  mooncake-wheel/tests/test_structured_object_store.py

Test results:

  • Unit tests pass: 106 passed, 27 skipped
  • Integration tests pass: two-node RDMA PUT/GET passed with pixel verification
  • Manual testing done: 30,000 Arrow create/release cycles passed

The skipped unit tests require a working CUDA/Torch runtime, which is unavailable in the test environment. ruff and ruff-format were intentionally not run; all other applicable pre-commit hooks passed.

Checklist

  • I have performed a self-review of my own code
  • I have formatted my code using ./scripts/code_format.sh
  • I have run pre-commit on the files changed in this PR and all hooks pass
  • I have updated the documentation (if applicable)
  • I have added tests to prove my changes are effective
  • For changes >500 LOC: not applicable to the 473-line performance commit; the draft comparison temporarily includes [Bugfix][Wheel] Preserve PIL image state in structured transfer #3412

AI Assistance Disclosure

  • No AI tools were used
  • AI tools were used (specify below)

Codex was used to compare codec designs, review lifetime and fallback paths, run the validation matrix, and prepare this PR description. The submitted implementation and results were manually reviewed.

This branch has not been deployed

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

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant